package com.bonait.bnframework.modules.home.fragment.mode;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.mode.BPA_ORDER;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.mode.BPA_SUBORDER;
import com.bonait.bnframework.common.db.res.ResGoodProperty;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.pfsx_adapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class add_manguan_control extends LinearLayout {


    @BindView(R.id.quzhizuo)
    Button quzhizuo;

    @BindView(R.id.close_from_good)
    Button close_from_good;


    @BindView(R.id.image_gz)
    ImageView image_gz;
    @BindView(R.id.jishiqi)
    RelativeLayout jishiqi;
    @BindView(R.id.runtime)
    Chronometer runtime;

    public ArrayList<BPA_SILOS> siloslist=new ArrayList<>();
    /**
     * 点击事件
     */
    public MyClickListener mListener = null;
    private View root;

    private Context contextMian;

    public add_manguan_control(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        contextMian=context;
        root = LayoutInflater.from(context).inflate(R.layout.dialog_add_manguan, this);
        ButterKnife.bind(this, root);
        Init();
    }


    public void Init() {
        //初始化界面控件的事件
        initEvent();
    }

    public  int zongliang=0;
    private void initData() {
        //2.填充规则数据，查询所有属性大类
        siloslist= QueryDB.GetSilosALL();

        zongliang=0;
        for (BPA_SILOS item:siloslist) {
            if (!item.name.equals("果糖仓") && !item.name.equals("粉料仓"))
            {
                zongliang++;
            }
        }
    }

    /**
     * 设置数据
     *
     * @param
     */
    public void SetData() {
        try {
            initData();
        } catch (Exception ex) {

        }
    }


    private void initEvent() {
        quzhizuo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                SaveData();
            }
        });
        close_from_good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.clickListenerNew(v, 0, null);
                }
            }
        });

        //为Chronomter绑定事件监听器,超过一小时自动日内告知
        runtime.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                //如果计时到现在超过了一小时秒 10s
                if (SystemClock.elapsedRealtime() - runtime.getBase() > 20 * 1000) {
                    StopTime();
                }
            }
        });
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if(i>=zongliang)
                    {
                        StopTime();
                        ToastUtils.info("料仓满管完成！");
                    }
                    break;
                case 1:

                    break;
                case 2:

                    break;
            }

        }
    };

    int i=0;
    /**
     * 保存数据
     */
    public boolean SaveData() {
        i=0;
        ToastUtils.info("料仓满管开始！");
        StartTime();
        ExecuteTheRecipe.WritePLC("手自切换", false, null);
        for (BPA_SILOS item:siloslist)
        {
            if(!item.name.equals("果糖仓") &&  !item.name.equals("粉料仓") )
            {
                int time=item.bValue<=0?5:item.bValue;//默认5秒
                ExecuteTheRecipe.WritePLC("手动控制"+item.num,true,null);

                try {
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ExecuteTheRecipe.WritePLC("手动控制"+item.num,false,null);
                            mHandler.sendEmptyMessage(0);
                            i++;
                        }
                    }, time * 1000);
                }catch (Exception ex)
                {
                    Log.d("异常信息", ex.getMessage());
                }
            }else
            {
                i++;
            }
        }

        return false;
    }


    /**
     * 启动定时器
     */
    public void StartTime() {
        jishiqi.setVisibility(View.VISIBLE);
        image_gz.setVisibility(View.GONE);

        runtime.setBase(SystemClock.elapsedRealtime());
        runtime.start();
    }

    /**
     * 停止定时器
     */
    public int StopTime() {
        jishiqi.setVisibility(View.GONE);
        image_gz.setVisibility(View.VISIBLE);
        long elapsedMillis = SystemClock.elapsedRealtime() - runtime.getBase();
        runtime.stop();

        int time=(int) (elapsedMillis / 1000);
        return  time;//当前点击多少秒
    }
}