package com.bonait.bnframework.common.constant;

import android.app.Activity;
import android.content.Intent;
import android.widget.LinearLayout;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG;
import com.bonait.bnframework.common.db.mode.BPA_GOODPROPERTY;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_LOG;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.mode.BPA_SUBORDER;
import com.bonait.bnframework.common.db.mode.BPA_SUGAR;
import com.bonait.bnframework.common.db.res.MakeStatus;
import com.bonait.bnframework.common.db.res.ResGoodProperty;
import com.bonait.bnframework.common.db.res.ResGoodsMake;
import com.bonait.bnframework.common.db.res.ResGoodsRecipe;
import com.bonait.bnframework.common.db.res.Res_PLCADDRESS1;
import com.bonait.bnframework.common.db.res.SilosLsjyMode;
import com.bonait.bnframework.common.db.res.StatusMode;
import com.bonait.bnframework.common.db.res.UserLogEnum;
import com.bonait.bnframework.common.db.res.lcMode;
import com.bonait.bnframework.common.db.res.wdszMode;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.helper.MessageLog;
import com.bonait.bnframework.common.helper.mode.OrderA;
import com.bonait.bnframework.common.helper.mode.ResultA;
import com.bonait.bnframework.common.linktab.LinkMode;
import com.bonait.bnframework.modules.home.adapter.loadinggood_adapter;
import com.bonait.bnframework.modules.home.adapter.pfsx_adapter;
import com.bonait.bnframework.modules.home.adapter.wdsz_adapter;
import com.bonait.bnframework.modules.home.adapter.zdqxchid_adapter;
import com.bonait.bnframework.modules.home.fragment.mode.sdkz_control;
import com.bonait.bnframework.modules.home.fragment.mode.tab_control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 单例模式
 * 数据中心
 */
public class DataBus {
    //region 单例模式
    private static DataBus  mInstance; //实例变量设置私有，防止直接通过类名访问

    private DataBus() {
        //默认构造函数私有，防止类外直接new创建对象
    }

    public static synchronized DataBus getInstance() { //静态同步方法作为唯一的实例对象获取方式
        if (mInstance==null) {
            mInstance = new DataBus();
        }
        return mInstance;
    }
    //endregion

    //region 数据中心
    /**
     * 料仓数据
     */
    public ArrayList<LinkMode> SilosData = new ArrayList<>();
    /**
     * 获取料仓数据
     */
    public void RefreshSilos()
    {
        ArrayList<lcMode> lcModes = DataBus.getInstance().ResGetLc();
        SilosData.clear();
        LinkedHashMap<String, List<Object>> map = new LinkedHashMap<>();
        for (lcMode item : lcModes) {
            if (map.containsKey(item.name)) {
                map.get(item.name).add(item);
            } else {
                List<Object> lc = new ArrayList<>();
                lc.add(item);
                map.put(item.name, lc);
            }
        }

        /**
         * 编例HashMap中所有key跟value的值
         */
        int i = 0;
        for (Object key : map.keySet()) {
            List<Object> value = map.get(key);
            LinkMode linkMode = new LinkMode(key.toString(), value, i == 0 ? true : false);
            SilosData.add(linkMode);
            i++;
        }
    }

    /**
     * 获取料仓数据
     * @return
     */
    public ArrayList<lcMode> ResGetLc()
    {
        ArrayList<lcMode> lc=new ArrayList<>();
        try {
            //获取料仓数据
            ArrayList<BPA_SILOS> bpa_silos = QueryDB.GetSilosALL();
            //加载materials数据
            if (bpa_silos.size() > 0) {
                for (BPA_SILOS silo : bpa_silos) {
                    List<BPA_MATERIAL> m = QueryDB.GetMaterialBySilosID(silo.id);
                    lcMode lcMode=new lcMode();
                    lcMode.id= silo.id;
                    lcMode.exp= silo.exp;
                    lcMode.num= silo.num;
                    lcMode.name= silo.name;
                    lcMode.warningValue= silo.warningValue;
                    lcMode.thrsoleValue= silo.thrsoleValue;
                    lcMode.plcValue= silo.plcValue;
                    lcMode.bValue= silo.bValue;
                    lcMode.jValue= silo.jValue;
                    lcMode.status= silo.status;
                    lcMode.Sort= silo.Sort;
                    lcMode.silosmargin= silo.silosmargin;
                    lcMode.siloszl= silo.siloszl;


                    lcMode.jTime= silo.jTime;
                    lcMode.fTime= silo.fTime;
                    lcMode.isenablebcsf= silo.isenablebcsf;
                    lcMode.minweight= silo.minweight;
                    lcMode.maxweight= silo.maxweight;
                    lcMode.jfrequency= silo.jfrequency;
                    lcMode.bcsfValue= silo.bcsfValue;


                    lcMode.isOpenbcsf= silo.isOpenbcsf;
                    lcMode.Gxs= silo.Gxs;
                    lcMode.Lxs= silo.Lxs;
                    lcMode.tdclsj= silo.tdclsj;


                    if (m.size() > 0) {
                        lcMode.materialName= m.get(0).name;
                        lcMode.materialId= m.get(0).id;
                        lcMode.materiallx= m.get(0).type;
                    } else {
                        lcMode.materialName= "未设置";
                        lcMode.materialId= "";
                        lcMode.materiallx= 0;
                    }
                    lc.add(lcMode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lc;
    }

    /**
     * 商品制作列表
     */
    public ArrayList<ResGoodsMake> GoodsMake = new ArrayList<ResGoodsMake>();

    public loadinggood_adapter loadinggoodAdapter=null;
    public void GetTodayNoMake()
    {
        GoodsMake.clear();
        ArrayList<BPA_SUBORDER> suborders=  QueryDB.GetSubOrdersTodayNoMake();
        for (BPA_SUBORDER item:suborders)
        {
            if(QueryDB.GetGoodsId(item.goodsID)!=null) // ---> // --->
            {
                AddGoodsMake(item);
            }
        }

        ExecuteTheRecipe.MakeGoodCount= QueryDB.GetSuOrderTodanCount();
    }

    /**
     * 增加一个订单商品
     * @param suborder
     */
    public void AddGoodsMake(BPA_SUBORDER suborder)
    {
        try
        {
            ExecuteTheRecipe.MakeGoodCount++;
            ArrayList<String> suorderlist=new ArrayList<>();
            for (ResGoodsMake item:GoodsMake)
            {
                if(item.makeStatus==MakeStatus.制作完成)
                {
                    suorderlist.add(item.subOrder.id);
                }
            }
            for (String item:suorderlist)
            {
                DeleteGoodsMake(item);
            }

            ResGoodsMake make=new ResGoodsMake();
            make.recipes=QueryDB.GetGoodsSreciperecipeList(suborder.recipeID);
            make.good=QueryDB.GetGoodsId(suborder.goodsID);
            make.goodsrecipename=QueryDB.GetGoodsRecipeNameId(suborder.recipeID);

            if (!ConfigName.getInstance().versionSelectionEnum.equals("配料吧台"))
            {
                make.Gt1=false;
                make.Gt2=false;
                for (ResGoodsRecipe recipe:make.recipes)
                {
                    BPA_SILOS silos = null;
                    List<BPA_SILOS> siloslist = QueryDB.GetSolisByMaterialID(recipe.materialID);
                    for (BPA_SILOS item : siloslist) {
                        if(silos==null)
                            silos = item;
                    }

                    if(silos!=null && silos.num==11)
                    {
                        make.Gt1=true;
                    }else if(silos!=null && silos.num==12)
                    {
                        make.Gt2=true;
                    }
                }
            }else
            {
                make.Gt1=false;
                make.Gt2=false;
            }

            make.subOrder=suborder;
            make.makeStatus= MakeStatus.等待中;
            make.makeMs="等待下发物料！";
            make.makeProcess=0;

            if(make.good!=null)
            {
                GoodsMake.add(make);
            }
           // MessageLog.ShowUserMessage(UserLogEnum.订单处理日志, "[" + make.good.name + "]准备配料!");

            if(loadinggoodAdapter!=null)
            {
                loadinggoodAdapter.Speak(R.raw.plks);
                loadinggoodAdapter.refresh();
            }
        }catch(Exception e){
            MessageLog.ShowInfo( "增加订单异常！"+e.getMessage());
        }
    }

    /**
     * 根据子订单id,物料id修改制作进度
     * @param suborderID
     * @param makeMs
     * @param count
     */
    public void UpdateGoodsMake(String suborderID,String makeMs,int count,int key,Boolean bz)
    {
        try
        {
            for (int m=0;m<GoodsMake.size();m++)
            {
                if(GoodsMake.get(m).subOrder.id.equals(suborderID))
                {
                    if(GoodsMake.get(m).subOrder.status==0)
                    {
                        GoodsMake.get(m).subOrder.status=1;
                        QueryDB.UpdateSubOrder(GoodsMake.get(m).subOrder);
                    }

                    GoodsMake.get(m).makeStatus= MakeStatus.制作中;
                    GoodsMake.get(m).makeMs=makeMs;
                    GoodsMake.get(m).makeProcess=(int)(((count*1.00)/ GoodsMake.get(m).recipes.size())*100);

                    if(loadinggoodAdapter!=null) {
                        loadinggoodAdapter.Speak(R.raw.plzqsh);
                        loadinggoodAdapter.refresh();
                    }
                    return;
                }
            }
        }catch(Exception e){
        }
    }
    /**
     * 根据子订单id,设置订单制作完成
     */
    public void DeleteGoodsMake(String suborderID)
    {
        try
        {
            ResGoodsMake good=null;
            for (ResGoodsMake item:GoodsMake)
            {
                if(item.subOrder.id.equals(suborderID))
                {
                    item.makeStatus=MakeStatus.制作完成;
                    item.makeMs="";
                    item.makeProcess=100;
                    item.subOrder.status=2;
                    QueryDB.UpdateSubOrder(item.subOrder);
                    good=item;
                }
            }

            if(good!=null)
            {
                GoodsMake.remove(good);
            }

            if(loadinggoodAdapter!=null)
            {
                loadinggoodAdapter.Speak(R.raw.plwc);
                loadinggoodAdapter.refresh();
            }

        }catch(Exception e){
        }
    }
    //endregion

    //region 配方数据
    /**
     * 详细配方
     */
    public ArrayList<ResGoodsRecipe> Recipe=new ArrayList<>();
    /**
     * 属性数据
     */
    public ArrayList<ResGoodProperty> bpa_goodproperties = new ArrayList<>();
    public pfsx_adapter sxadapter=null;
    /**
     * 加载商品配方
     */
    public void GetMainGoodProperty()
    {
        bpa_goodproperties.clear();
        ArrayList<BPA_GOODPROPERTY> da = QueryDB.GetGoodsPropertyALL("0");
        for (BPA_GOODPROPERTY item : da) {
            ResGoodProperty re = new ResGoodProperty();
            re.id = item.id;
            re.name = item.name;

            ArrayList<BPA_GOODPROPERTY> zsx = QueryDB.GetGoodsPropertyALL(item.id);
            re.child = new ArrayList<>();


            ArrayList<ResGoodProperty> _chid = new ArrayList<>();
            for (BPA_GOODPROPERTY k : zsx) {
                ResGoodProperty m = new ResGoodProperty();
                m.id = k.id;
                m.name = k.name;
                m.isSelect = false;
                m.sort=k.sort;
                m.foreignKeyRe=k.foreignKeyRe;
                m.parentid=k.parentid;
                _chid.add(m);
            }
            re.child = _chid;
            re.GoodsTypeId=item.GoodsTypeId;
            re.sort= item.sort;
            bpa_goodproperties.add(re);
        }

        for (ResGoodProperty item : bpa_goodproperties) {
            int i = 0;
            for (ResGoodProperty c : item.child) {
                if (i == 0) {
                    c.isSelect = true;
                }
                i++;
            }
        }
    }
    //endregion

    //region 温度设置
    public ArrayList<wdszMode> wdszModes = new ArrayList<wdszMode>();
    public wdsz_adapter wdszAdapter=null;
    /**
     * 获取温度设置
     */
    public void GetWdsz()
    {
        try {
            wdszModes=new ArrayList<>();

            List<String> names=new ArrayList<>();
            if (ConfigName.getInstance().versionSelectionEnum.equals("配料吧台")) {
                names.add("水池温度");
                names.add("猪油管道保温");
                names.add("牛油管道保温");
            }else if (ConfigName.getInstance().versionSelectionEnum.equals("奶茶机"))
            {
                names.add("水池温度");
                names.add("果糖左温度");
                names.add("果糖右温度");
            }
            for (String item:names)
            {
                wdszMode mode=new wdszMode();
                mode.name=item;
                mode.temp=0;
                Object sx=ExecuteTheRecipe.ReadPLC(item+"设置");
                mode.tempup=sx==null?0:(int)sx;
                mode.autoheat=false;
                if(item.equals("水池温度"))
                {
                    mode.autoheat=ConfigName.getInstance().AutomaticHeating;
                } else if (item.equals("果糖左温度")) {
                    mode.autoheat=ConfigName.getInstance().GT1_AutomaticHeating;
                }else if (item.equals("果糖右温度")) {
                    mode.autoheat=ConfigName.getInstance().GT2_AutomaticHeating;
                }
                wdszModes.add(mode);
            }


            if (wdszAdapter!=null)
            {
                wdszAdapter.refresh();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前温度
     */
    public void GetWD()
    {
        for (wdszMode item:wdszModes)
        {
            if(item.name.contains("保温"))
            {
                Object sx=ExecuteTheRecipe.ReadPLC(item.name);
                item.autoheat=sx==null?false:(boolean)sx;
            }else
            {
                Object sx=ExecuteTheRecipe.ReadPLC(item.name+"设置");
                item.tempup=sx==null?0:(int)sx;
            }
        }
        if (wdszAdapter!=null)
        {
            wdszAdapter.refresh();
        }
    }
    //endregion

    //region 自动清洗
    public boolean ZDQX_IsStart = false;
    public ArrayList<BPA_SUGAR> zdqx = new ArrayList<BPA_SUGAR>();
    public zdqxchid_adapter zdqxchidAdapter=null;
    public void GetZDQXChid()
    {
        try {
            DataBus.getInstance().ZDQX_IsStart=false;
            zdqx.clear();
            ArrayList<BPA_SUGAR> data= QueryDB.GetSugarALL();

            for(BPA_SUGAR item:data)
            {
                zdqx.add(item);
            }
            if (zdqxchidAdapter!=null)
            {
                zdqxchidAdapter.refresh();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //endregion

    //region 实时监控
    /**
     * 设备状态列表,实时监控
     */
    public ArrayList<StatusMode> statusModes = new ArrayList<StatusMode>();
    //endregion
}
