package com.example.bpa.config;

import android.app.Activity;

import com.example.bpa.Model.OrderA;
import com.example.bpa.Model.OrderStatusChange;
import com.example.bpa.Model.ResultA;
import com.example.bpa.R;
import com.example.bpa.app.Main;
import com.example.bpa.db.mode.BPA_SUBORDER;
import com.example.bpa.service.DeviceData;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_ALERTLOG;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.db.mode.BPA_LOG;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.view.adapter.makegood_adapter;
import com.example.bpa.view.adapter.order_adapter;
import com.example.bpa.view.mode.MakeStatus;
import com.example.bpa.view.mode.ResGoodsMake;
import com.example.bpa.view.mode.ResGoodsRecipe;
import com.example.bpa.view.mode.StatusMode;
import com.example.bpa.view.mode.mainGoods;

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

import dagger.multibindings.ElementsIntoSet;

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

    private DataBus() {
        //默认构造函数私有，防止类外直接new创建对象
        //测试添加图片
        good_picMap.put(0, R.mipmap.milktea_1);
        good_picMap.put(1, R.mipmap.milktea_2);
        good_picMap.put(2,R.mipmap.milktea_3);
        good_picMap.put(3,R.mipmap.milktea_4);
    }

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

    //region 连接状态
    /**
     * Plc是否连接
     */
    public boolean PlcIsConnect=false;
    //endregion

    //region 数据中心
    /**
     * 商品扫码等待制作列表
     */
    public List<ResGoodsMake> GoodsSMMake = new ArrayList<ResGoodsMake>();
    /**
     * 扫码商品订单制作
     */
    public order_adapter orderadapter;
    /**
     * 商品制作列表
     */
    public List<ResGoodsMake> GoodsMake = new ArrayList<ResGoodsMake>();
    /**
     * 商品制作完成列表
     */
    public List<ResGoodsMake> GoodsMakeOver = new ArrayList<ResGoodsMake>();
    /**
     * 商品制作
     */
    public makegood_adapter goodmakeadapter;

    /**
     * 获取是否可以下单
     */
    public boolean GetIsPlaceAnOrder()
    {
        try
        {
            if(GoodsMake.size()<=0)
            {
                return true;
            }else
            {
                boolean issucess=true;
                for (ResGoodsMake item:GoodsMake)
                {
                    if(item.makeStatus!=MakeStatus.制作完成)
                    {
                        issucess=false;
                    }
                }
                return issucess;
            }

        }catch(Exception e){
            return true;
        }
    }
    /**
     * 增加一个订单商品
     * @param suborder
     */
    public void AddGoodsMake(BPA_SUBORDER suborder)
    {
        try
        {
            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.GetGoodsSrecipeList(suborder.goodsID);
            make.good=QueryDB.GetGoodsId(suborder.goodsID);
            make.subOrder=suborder;
            make.makeStatus= MakeStatus.等待制作;
            make.makeMs="";
            make.makeProcess=0;
            GoodsMake.add(make);
            goodmakeadapter.refresh();

            for (ResGoodsMake item:GoodsSMMake)
            {
                if(item.subOrder.id.equals(suborder.id))
                {
                    item.makeStatus=MakeStatus.等待制作;
                    item.makeMs="";
                    item.makeProcess=0;
                    orderadapter.refresh();
                }
            }
        }catch(Exception e){
        }
    }

    /**
     * 根据子订单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);
                    }

                    //
                    for (ResGoodsMake item:GoodsSMMake)
                    {
                        if(item.subOrder.id.equals(GoodsMake.get(m).subOrder.id))
                        {
                            item.makeStatus= MakeStatus.制作中;
                            item.makeMs=makeMs;
                            item.makeProcess=(count/ GoodsMake.get(m).recipes.size())*100;

                            for (ResGoodsRecipe recipe: item.recipes)
                            {
                                if(recipe.sort==key)
                                {
                                    if(bz==false)
                                    {
                                        recipe.makeProcess=20;
                                    }else
                                    {
                                        recipe.makeProcess=100;
                                    }
                                }
                            }
                            orderadapter.refresh();

                        }
                    }

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

                    OrderStatusChange change=new OrderStatusChange();
                    change.SuborderId=GoodsMake.get(m).subOrder.id;
                    change.CookingStatus=4;
                    Main.getInstance().SetOrderChanged(change);
                    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;
                }
            }

            ResGoodsMake sm=null;
            for (ResGoodsMake item:GoodsSMMake)
            {
                if(item.subOrder.id.equals(suborderID))
                {
                    item.makeStatus=MakeStatus.制作完成;
                    item.makeMs="";
                    item.makeProcess=100;
                    item.subOrder.status=2;
                    sm=item;
                }
                orderadapter.refresh();
            }
//            if(sm!=null)
//            {
//                GoodsSMMake.remove(sm);
//                orderadapter.refresh();
//            }

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

                OrderStatusChange change=new OrderStatusChange();
                change.SuborderId=good.subOrder.id;
                change.CookingStatus=6;
                change.GoodName=good.good.name;
                Main.getInstance().SetOrderChanged(change);
            }
        }catch(Exception e){
        }
    }
    /**
     * 根据商品ID检查物料是否充足,是否可制作
     * @param GoodId
     * @return
     */
    public ResultA InspectMaterial(String GoodId)
    {
        ResultA resultA=new ResultA();
        boolean IsMake=true;
        String message="";
        try
        {
            ArrayList<ResGoodsRecipe> goodsRecipes = QueryDB.GetGoodsSrecipeList(GoodId);
            for (ResGoodsRecipe recipe:goodsRecipes)
            {
                BPA_SILOS silos=null;
                List<BPA_SILOS> siloslist = QueryDB.GetSolisByMaterialID(recipe.materialID);
                for (BPA_SILOS si:siloslist)
                {
                    if(DeviceData.Get().getCallCurrentWeight(si.num)>=recipe.value){silos=si;break;}
                }
                if(silos==null)
                {
                    IsMake=false;
                    message+=recipe.materialName+",";
                }
            }
            resultA.IsMake=IsMake;
            resultA.Message="【" +message+"】物料不足,请检查料仓物料!";
            return resultA;
        }catch(Exception e){
            resultA.IsMake=false;
            resultA.Message=e.getMessage();
            return resultA;
        }
    }
    /**
     * 主界面商品列表
     */
    public List<mainGoods> mainGoods = new ArrayList<mainGoods>();
    /**
     * 商品图片
     */
    public Map<Integer,Integer> good_picMap = new HashMap<Integer,Integer>();
    /**
     * 更新商品主界面商品列表
     */
    public void UpdateMainGoods(){
        mainGoods.clear();
        ArrayList<BPA_GOODS> res = QueryDB.GetGoodsStateALL();
        if (res.size()>0){
            for (BPA_GOODS good:res){
                mainGoods newgood = new mainGoods();
                newgood.id = good.id;
                newgood.name = good.name;
                newgood.message = "";
                mainGoods.add(newgood);
            }
        }
    }

    /**
     * 员工操作日志
     */
    public List<BPA_LOG> bpa_logs = new ArrayList<BPA_LOG>();
    /**
     * 订单操作日志
     */
    public List<BPA_ALERTLOG> bpa_alertlogs = new ArrayList<BPA_ALERTLOG>();
    /**
     * 设备状态列表
     */
    public List<StatusMode> statusModes = new ArrayList<StatusMode>();
    /**
     * 扫码订单
     */
    public OrderA OrderSM = new OrderA();
    //endregion
}
