package com.bonait.bnframework.business;

import com.bonait.bnframework.common.db.QueryDB;
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.utils.ToastUtils;
import com.bonait.bnframework.modules.home.fragment.mode.LocationStatus;
import com.bonait.bnframework.modules.home.fragment.mode.MyStatus;

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

/**
 * 执行配方
 */
public class ExecuteTheRecipe {
    /**
     * 执行配方
     *
     * @param recipe
     * @return
     */
    public static boolean Execute(BPA_GOODSRECIPE recipe) {
        boolean status = false;
        try {
            ToastUtils.info("开始执行:" + recipe.sort + "、" + recipe.processms);
            //1.解析
            if (recipe.materialType == 0)//正常物料
            {
                ExecuteMaterialIssuance(recipe.processvalue);
            } else if (recipe.materialType == 1)//工序模型
            {
                ExecuteOperationSteps(recipe.processname,recipe.processvalue);
            }
        } catch (Exception ex) {
            ToastUtils.error("异常信息:" + ex.getMessage());
            status = false;
        } finally {
            return status;
        }
    }

    /**
     * 液体物料信息-配料
     *
     * @param text
     */
    private static void ExecuteMaterialIssuance(String text) {
        try {
            if(!text.isEmpty())
            {
                //仓号 值
                Map<Integer,Integer> formulation=new IdentityHashMap<>();
                //region 获取仓号和值
                List<String> data=new ArrayList<>();
                if(text.contains("|"))
                {
                    String[] res= text.split("[|]");
                    for (int i=0;i<res.length;i++)
                    {
                        data.add(res[i]);
                    }
                }else
                {
                    data.add(text);
                }
                for(String item:data)
                {
                    if(!item.isEmpty() && item.contains(","))
                    {
                        String[] wl= item.split("[,]");
                        if (wl != null && wl.length == 2)
                        {
                            String name=wl[0];
                            int val=Integer.parseInt(wl[1]);
                            List<BPA_SILOS> bpa_silos= QueryDB.GetSolisByMaterialName(name);
                            if(bpa_silos.size()>0 && val>0)
                            {
                                BPA_SILOS silos= bpa_silos.get(0);
                                formulation.put(silos.num,val);
                            }
                        }
                    }
                }
                //endregion
                //写入PLC
                for (Map.Entry<Integer, Integer> entry : formulation.entrySet()) {
                    Integer key = entry.getKey();
                    Integer value = entry.getValue();
                    Write_PLC_Material(key,value);
                }
            }
        } catch (Exception ex) {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 执行工序步骤
     *
     * @param text
     */
    private static void ExecuteOperationSteps(String processname, String text) {
        try {
            if(!text.isEmpty() && !processname.isEmpty())
            {
                //工序名称和值
                HashMap<String,String> formulation=new HashMap<>();
                //region 获取仓号和值
                List<String> data=new ArrayList<>();
                if(text.contains("|"))
                {
                    String[] res= text.split("[|]");
                    for (int i=0;i<res.length;i++)
                    {
                        data.add(res[i]);
                    }
                }else
                {
                    data.add(text);
                }
                for(String item:data)
                {
                    if(!item.isEmpty() && item.contains(","))
                    {
                        String[] wl= item.split("[,]");
                        if (wl != null && wl.length == 2)
                        {
                            String name=wl[0];
                            String val=wl[1];
                            formulation.put(name,val);
                        }
                    }
                }
                //endregion
                //写入PLC
                if(formulation.size()>0)
                {
                    switch (processname)
                    {
                        case "搅拌":
                            Write_PLC_Stir(formulation);
                            break;
                        case "热油":
                            Write_PLC_Hotoil(formulation);
                            break;
                        case "主料":
                            Write_PLC_Ingredients(formulation);
                            break;
                        case "加热":
                            Write_PLC_Heating(formulation);
                            break;
                        case "延迟":
                            Write_PLC_Delay(formulation);
                            break;
                        case "勾芡":
                            Write_PLC_Gouqiu(formulation);
                            break;
                        case "出菜":
                            Write_PLC_Outdishes(formulation);
                            break;
                        case "热锅":
                            Write_PLC_Hotpan(formulation);
                            break;
                    }
                }
            }
        } catch (Exception ex) {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    //region 物料PLC控制
    /**
     * 写入仓号需求值
     * @param num
     * @param val
     */
    private static void Write_PLC_Material(int num,int val)
    {
        try
        {
            ToastUtils.info("写入PLC仓号需求:" + num+","+val);
            Thread.sleep(1000);
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }
    //endregion

    //region 写PLC工序

    /**
     * 写PLC搅拌
     * @param data
     */
    private static void Write_PLC_Stir(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC热油
     * @param data
     */
    private static void Write_PLC_Hotoil(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC主料
     * @param data
     */
    private static void Write_PLC_Ingredients(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC 加热
     * @param data
     */
    private static void Write_PLC_Heating(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC 延迟
     * @param data
     */
    private static void Write_PLC_Delay(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if(key.contains("延迟"))
                {
                    int val=Integer.parseInt(value);
                    ToastUtils.info("延迟:"+value+"s");
                    Thread.sleep(val*1000);
                }
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC 勾芡
     * @param data
     */
    private static void Write_PLC_Gouqiu(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC 出菜
     * @param data
     */
    private static void Write_PLC_Outdishes(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                int val=Integer.parseInt(value);
                ToastUtils.info("出菜延迟:"+value+"s");
                Thread.sleep(val*1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 写PLC 热锅
     * @param data
     */
    private static void Write_PLC_Hotpan(HashMap<String,String> data)
    {
        try
        {
            for (HashMap.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                ToastUtils.info("写入PLC工序需求:" + key+","+value);
                Thread.sleep(1000);
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }
    //endregion

    //region 主页按钮

    /**
     * 点击清洗按钮
     * @param data
     */
    public static void Write_PLC_Clean(MyStatus data)
    {
        try
        {
            if(data==MyStatus.Start)
            {
                ToastUtils.info("点击启动搅拌清洗按钮");

            }else if(data==MyStatus.Paused)
            {
                ToastUtils.info("点击暂停清洗按钮");

            }else if(data==MyStatus.Stop)
            {
                ToastUtils.info("点击停止清洗按钮");
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 点击清洗按钮
     * @param data
     */
    public static void Write_PLC_Location(LocationStatus data)
    {
        try
        {
            if(data==LocationStatus.InLocation)
            {
                ToastUtils.info("点击炒菜位按钮");

            }else if(data==LocationStatus.OutLocation)
            {
                ToastUtils.info("点击出菜位按钮");

            }else if(data==LocationStatus.PourWaterLocation)
            {
                ToastUtils.info("点击倒水位按钮");
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }

    /**
     * 点击摇欠按钮
     * @param data
     */
    public static void Write_PLC_YaoQian(MyStatus data)
    {
        try
        {
            if(data==MyStatus.Start)
            {
                ToastUtils.info("点击摇浅按钮");

            }else if(data==MyStatus.Stop)
            {
                ToastUtils.info("点击停摇按钮");
            }
        }catch (Exception ex)
        {
            ToastUtils.error("异常信息:" + ex.getMessage());
        }
    }
    //endregion
}
