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

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.add_silos_message;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom;
import com.bonait.bnframework.modules.home.fragment.mode.jingdutiao1;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BunkerSetupActivity extends BaseActivity {

    //region 变量
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.silos1)
    jingdutiao1 silos1;

    @BindView(R.id.silos2)
    jingdutiao1 silos2;

    @BindView(R.id.silos3)
    jingdutiao1 silos3;

    @BindView(R.id.silos4)
    jingdutiao1 silos4;

    @BindView(R.id.quanchangqingxi)
    imagebuttom quanchangqingxi;

    @BindView(R.id.silos_control)
    add_silos_message silos_control;
    public Context context;
    public Activity activity=null;
    //endregion

    //region 界面实例
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bunkersetup);
        ButterKnife.bind(this);
        context = this;
        activity=this;
        initTopBar();
        InitData();
    }
    //endregion

    //region 私有方法

    /**
     * 初始化顶部弹框
     */
    private void initTopBar() {
        mTopBar.setBackgroundColor(ContextCompat.getColor(context, R.color.topbj1));
        mTopBar.setTitle("料仓设置");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExecuteTheRecipe.WritePLC("料仓1手动开关", false, null);
                ExecuteTheRecipe.WritePLC("料仓2手动开关", false, null);
                ExecuteTheRecipe.WritePLC("料仓3手动开关", false, null);
                finish();
            }
        });

        /**
         * 全仓清洗
         */
        quanchangqingxi.mListener = new MyClickListener() {
            @Override
            public void clickListener(View v, Object data) {
                boolean status = (boolean) data;
                ExecuteTheRecipe.WritePLC("料仓1手动开关", status, null);
                ExecuteTheRecipe.WritePLC("料仓2手动开关", status, null);
                ExecuteTheRecipe.WritePLC("料仓3手动开关", status, null);
            }

            @Override
            public void clickListenerNew(View v, int k, Object data) {

            }
        };

        //关闭
        silos_control.mListener = new MyClickListener() {
            @Override
            public void clickListener(View v, Object data) {
                InitData();
                silos_control.setVisibility(View.GONE);
            }

            @Override
            public void clickListenerNew(View v, int k, Object data) {

            }
        };
    }

    /**
     * 初始化数据
     */
    public void InitData() {
        DataBus.getInstance().GetLc();
        for (lcMode item : DataBus.getInstance().lcModes) {
            if (item.num == 1) {
                silos1.SetValue(item, myClickListener);
            } else if (item.num == 2) {
                silos2.SetValue(item, myClickListener);
            } else if (item.num == 3) {
                silos3.SetValue(item, myClickListener);
            }else if (item.num == 4) {
                silos4.SetValue(item, myClickListener);
            }
        }
    }

    /**
     * 内部点击事件
     */
    public MyClickListener myClickListener = new MyClickListener() {
        @Override
        public void clickListener(View v, Object data) {
            if (data != null) {
                lcMode mode = (lcMode) data;
                silos_control.SetData(mode, activity);
                silos_control.setVisibility(View.VISIBLE);
            }
        }
        @Override
        public void clickListenerNew(View v, int k, Object data) {

        }
    };

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.quanliaobuchong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quanliaobuchong:
                for (lcMode item : DataBus.getInstance().lcModes) {
                    QueryDB.UpdateYL(item.id, item.siloszl);
                }
                InitData();
                ToastUtils.info("补充完成！");
                break;
        }
    }
    //endregion
}