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

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
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.CountDownTimerExt;
import com.bonait.bnframework.common.helper.I.IRunT;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom;
import com.bonait.bnframework.modules.home.fragment.mode.imagebuttom3;
import com.bonait.bnframework.modules.home.fragment.mode.jingdutiao;
import com.bonait.bnframework.modules.home.fragment.mode.newhuoli_control;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

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

public class CookingActivity extends LinearLayout {

    //region 变量
    @BindView(R.id.device_image)
    ImageView device_image;

    @BindView(R.id.touliao_k)
    LinearLayout touliao_k;

    @BindView(R.id.touliao_m)
    LinearLayout touliao_m;

    @BindView(R.id.goodname)
    TextView goodname;

    @BindView(R.id.chaoguo_wendu)
    TextView chaoguo_wendu;

    @BindView(R.id.gongxumiaoshu)
    TextView gongxumiaoshu;//工序描述

    @BindView(R.id.gongxutime)
    TextView gongxutime;//预计等待

    @BindView(R.id.start_goodmake)
    imagebuttom start_goodmake;//开始

    @BindView(R.id.zanting_goodmake)
    public imagebuttom zanting_goodmake;//暂停
    @BindView(R.id.runtime)
    TextView runtime;//计时器 倒计时

    @BindView(R.id.t_jindu)
    jingdutiao t_jindu;//计时器

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;


    @BindView(R.id.huoli)
    newhuoli_control huoli;

    @BindView(R.id.fanchaoshudu)
    imagebuttom3 fanchaoshudu;


    @BindView(R.id.guokoucaoxiang)
    imagebuttom3 guokoucaoxiang;

//    @BindView(R.id.chushui)
//    imagebuttom3 chushui;

    @BindView(R.id.touliao1)
    imagebuttom3 touliao1;

    @BindView(R.id.touliao2)
    imagebuttom3 touliao2;

    @BindView(R.id.touliao3)
    imagebuttom3 touliao3;

    @BindView(R.id.touliao4)
    imagebuttom3 touliao4;

    @BindView(R.id.yeliao1)
    imagebuttom3 yeliao1;
    @BindView(R.id.yeliao2)
    imagebuttom3 yeliao2;
    @BindView(R.id.yeliao3)
    imagebuttom3 yeliao3;
    @BindView(R.id.yeliao4)
    imagebuttom3 yeliao4;

    private Context contextMian;
    private View root;

    public Activity activity = null;
    //endregion

    //region 界面实例
    public CookingActivity(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        contextMian = context;
        root = LayoutInflater.from(context).inflate(R.layout.activity_cooking, this);
        ButterKnife.bind(this, root);
        initTopBar();

        SxUIThread();
    }
    //endregion

    //region 公共方法

    /**
     * 初始化数据
     */
    public void initData(BPA_GOODS goods, Activity ac, MyClickListener listener) {
        //根据商品id查询
        activity = ac;

        yeliao1.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(1)+"");
        yeliao1.Init();

        yeliao2.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(2)+"");
        yeliao2.Init();

        yeliao3.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(3)+"");
        yeliao3.Init();

        yeliao4.setTag(ConfigName.getInstance().SilosDisplayMaterial.get(4)+"");
        yeliao4.Init();

        ConcurrentHashMap<Integer,String> resultData= DataBus.getInstance().GetMainDisplayText(goods.id);
        touliao1.setTag(resultData.get(1)+"-按钮");
        touliao1.Init();

        touliao2.setTag(resultData.get(2)+"-按钮");
        touliao2.Init();

        touliao3.setTag(resultData.get(3)+"-按钮");
        touliao3.Init();

        touliao4.setTag(resultData.get(4)+"-按钮");
        touliao4.Init();

        goodname.setText(goods.name + "");
        start_goodmake.mListener = listener;
        zanting_goodmake.mListener = listener;
        SetReset();
        SetRest1();
        SetProcess(goods.maketime, 0);
        SetMiaoShu("等待开始...");
        runtime.setText(formatTime(goods.maketime));

        if (ConfigName.getInstance().versionSelectionEnum.equals("大炒版本") || ConfigName.getInstance().versionSelectionEnum.contains("一拖")) {
            touliao_k.setVisibility(View.GONE);
            touliao_m.setVisibility(View.GONE);
            device_image.setImageResource(R.mipmap.device00);
        }else
        {
            touliao_k.setVisibility(View.VISIBLE);
            touliao_m.setVisibility(View.VISIBLE);
            device_image.setImageResource(R.mipmap.device1);
        }
    }

    /**
     * 设置描述
     *
     * @param text
     */
    public void SetMiaoShu(String text) {
        gongxumiaoshu.setText(text + "");
    }

    /**
     * 设置描述时间
     *
     * @param
     */
    public void SetMiaoShuTime(int time) {
        gongxutime.setText("预计等待:"+time+"s");
    }

    /**
     * 设置进度条
     *
     * @param
     */
    public void SetProcess(int alltime, int usertime) {
        try {
            t_jindu.SetValue((usertime * 100) / alltime);
            runtime.setText(formatTime((alltime - usertime)));
        } catch (Exception ex) {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 复位
     */
    public void SetReset() {
        SetMiaoShu("等待开始...");
        zanting_goodmake.SetStatus(false);
        zanting_goodmake.setVisibility(View.GONE);
    }

    /**
     * 复位
     */
    public void SetZanTing(boolean data) {
        zanting_goodmake.setVisibility(data ? View.VISIBLE : View.GONE);
    }

    public void SetRest1() {
        start_goodmake.SetStatus(false);
        if(flicker_buttom!=null)
        {
            flicker_buttom.setVisibility(View.VISIBLE);
            flicker_buttom.SetStatusKK(false);
            flicker_buttom=null;
        }
        huoli.SetLevel(0);
    }

    public void SetStartStaus(boolean i)
    {
        start_goodmake.SetStatus(i);
    }
    //endregion

    //region 私有方法

    /**
     * 将毫秒转化为 分钟：秒 的格式    100* 1000
     *
     * @param millisecond 毫秒
     * @return
     */
    public String formatTime(long millisecond) {
        int minute;//分钟
        int second;//秒数
        minute = (int) ((millisecond / 1) / 60);
        second = (int) ((millisecond / 1) % 60);
        if (minute < 10) {
            if (second < 10) {
                return "0" + minute + " : " + "0" + second;
            } else {
                return "0" + minute + " : " + second;
            }
        } else {
            if (second < 10) {
                return minute + " : " + "0" + second;
            } else {
                return minute + " : " + second;
            }
        }
    }

    /**
     * 初始化顶部弹框
     */
    private void initTopBar() {
        mTopBar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.topbj1));
        mTopBar.setTitle("菜谱烹饪");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ExecuteTheRecipe.IsStart) {
                    ToastUtils.warning("请等待制作完成后退出...");
                    return;
                }
                setVisibility(View.GONE);
            }
        });
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.kancaipu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kancaipu://看菜谱
                MessageManager.getInstance().sendMessage(MessageName.SelectZY, 0);
                break;
        }
    }

    /**
     * 闪烁buttom
     */
    public imagebuttom3 flicker_buttom=null;
    /**
     * 刷新UI界面
     */
    public void SxUIThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {
                        if (activity != null && getVisibility() == View.VISIBLE) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Object val_wd = ExecuteTheRecipe.getListingValue("当前锅底温度");
                                    if (val_wd != null) {
                                        chaoguo_wendu.setText(val_wd + "°C");
                                    } else {
                                        chaoguo_wendu.setText("??°C");
                                    }

                                    //闪烁
                                    if (flicker_buttom != null) {
                                        flicker_buttom.setVisibility(flicker_buttom.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
                                    }
                                }
                            });
                        }
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        ToastUtils.info("异常信息:" + e.getMessage());
                    }
                }
            }
        }).start();

        ExecuteTheRecipe.ExecuteCurrentOperation=new IRunT() {
            @Override
            public void Run(Object o) {
                String data=(String)o;
                if (activity != null && getVisibility() == View.VISIBLE) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(data.contains("|"))
                            {
                                String[] K= data.split("[|]");
                                if (K.length>=2)
                                {
                                    String name=K[0];
                                    String val=K[1];
                                    if(flicker_buttom!=null)
                                    {
                                        flicker_buttom.setVisibility(View.VISIBLE);
                                        flicker_buttom.SetStatusKK(false);
                                    }
                                    flicker_buttom=null;
                                    switch (name) {
                                        case "搅拌":
                                            flicker_buttom=fanchaoshudu;
                                            flicker_buttom.SetStatusKK(true);
                                            break;
                                        case "位置":
                                            flicker_buttom=guokoucaoxiang;
                                            flicker_buttom.SetStatusKK(true);
                                            break;
                                        case "加热":
                                            huoli.SetLevel(Integer.parseInt(val));
                                            break;
                                        case "主料":
                                            if(val.equals("1号位"))
                                            {
                                                flicker_buttom=touliao1;
                                            }else if(val.equals("2号位"))
                                            {
                                                flicker_buttom=touliao2;
                                            }else if(val.equals("3号位"))
                                            {
                                                flicker_buttom=touliao3;
                                            }else if(val.equals("4号位"))
                                            {
                                                flicker_buttom=touliao4;
                                            }
                                            flicker_buttom.SetStatusKK(true);
                                            break;
                                        case "加水":
//                                            flicker_buttom=chushui;
//                                            flicker_buttom.SetStatusKK(true);
                                            break;
                                        case "液体料":
                                            if(val.equals("1"))
                                            {
                                                flicker_buttom=yeliao1;
                                                flicker_buttom.SetStatusKK(true);
                                            }else if(val.equals("2"))
                                            {
                                                flicker_buttom=yeliao2;
                                                flicker_buttom.SetStatusKK(true);
                                            }else if(val.equals("3"))
                                            {
                                                flicker_buttom=yeliao3;
                                                flicker_buttom.SetStatusKK(true);
                                            }else if(val.equals("4"))
                                            {
                                                flicker_buttom=yeliao4;
                                                flicker_buttom.SetStatusKK(true);
                                            }
                                            break;
                                    }
                                }
                            }
                        }
                    });
                }
            }
        };
    }
    //endregion

    //region 外部调用
    //endregion

}