package com.example.bpa.app;


import android.util.Log;

import com.example.bpa.Model.OrderStatusChange;
import com.example.bpa.Model.ReceiveOrder;
import com.example.bpa.config.ConfigName;
import com.example.bpa.config.DataBus;
import com.example.bpa.config.MessageName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_SYSTEMSET;
import com.example.bpa.db.mode.BPA_USER;
import com.example.bpa.helper.AES;
import com.example.bpa.helper.ConfigUtil;
import com.example.bpa.helper.HttpUtils;
import com.example.bpa.helper.Json;
import com.example.bpa.helper.T;
import com.example.bpa.helper.UpdateManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 主函数进程
 * 主要进行配置文件-系统类
 */
public class Main {
    //region 私有
    private static Main mInstance; //实例变量设置私有，防止直接通过类名访问

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

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

    //region 外部调用事件

    /**
     * 初始化
     */
    public void Init() {
        //1.加载系统设置
        LoadSystem();
        ToggleEnvironment();
        //2.加载配置
        LoadingDPPZ();
        LoadingPZ();
        //3.初始化业务
        //4.初始化日志上报adad
    }

    /**
     * 加载系统配置
     */
    public void LoadSystem()
    {
        ArrayList<BPA_SYSTEMSET> bpa_systemsets= QueryDB.GetSystemsetALL();
        for (int i = 0; i <12;i++){
            int ty=i+1;
            BPA_SYSTEMSET set=new BPA_SYSTEMSET();
            set.type=ty;
            switch (ty)
            {
                case 1:
                    set.value=String.valueOf(ConfigName.getInstance().OrderPushTime);
                    break;
                case 2:
                    set.value=String.valueOf(ConfigName.getInstance().SilosValuePushTime);
                    break;
                case 3:
                    set.value=String.valueOf(ConfigName.getInstance().OtherValuePushTime);
                    break;
                case 4:
                    set.value=String.valueOf(ConfigName.getInstance().PipeDrainCycleTime);
                    break;
                case 5:
                    set.value=String.valueOf(ConfigName.getInstance().OrderTimeout);
                    break;
                case 6:
                    set.value=String.valueOf(ConfigName.getInstance().SiloFeedThreshold);
                    break;
                case 7:
                    set.value=String.valueOf(ConfigName.getInstance().SiloAlarmThreshold);
                    break;
                case 8:
                    set.value=String.valueOf(ConfigName.getInstance().AICleanTime);
                    break;
                case 9:
                    set.value=ConfigName.getInstance().AutKey;
                    break;
                case 10:
                    set.value=ConfigName.getInstance().Address;
                    break;
                case 11:
                    set.value=String.valueOf(ConfigName.getInstance().Post);
                    break;
                case 12:
                    set.value=ConfigName.getInstance().Environment;
                    break;
            }
            set.deviceID = ConfigName.getInstance().DeviceId;
            set.userID = ConfigName.getInstance().user.userID;

            BPA_SYSTEMSET find=null;
            for (BPA_SYSTEMSET item:bpa_systemsets)
            {
                if(item.type==ty)
                {
                    find=item;
                    break;
                }
            }
            if(find==null)
            {
                QueryDB.AddSystemset(set);
            }
        }

        for (BPA_SYSTEMSET item:bpa_systemsets)
        {
            switch (item.type)
            {
                case 1:
                    ConfigName.getInstance().OrderPushTime=Integer.parseInt(item.value);
                    break;
                case 2:
                    ConfigName.getInstance().SilosValuePushTime=Integer.parseInt(item.value);
                    break;
                case 3:
                    ConfigName.getInstance().OtherValuePushTime=Integer.parseInt(item.value);
                    break;
                case 4:
                    ConfigName.getInstance().PipeDrainCycleTime=Integer.parseInt(item.value);
                    break;
                case 5:
                    ConfigName.getInstance().OrderTimeout=Integer.parseInt(item.value);
                    break;
                case 6:
                    ConfigName.getInstance().SiloFeedThreshold=Integer.parseInt(item.value);
                    break;
                case 7:
                    ConfigName.getInstance().SiloAlarmThreshold=Integer.parseInt(item.value);
                    break;
                case 8:
                    ConfigName.getInstance().AICleanTime=Integer.parseInt(item.value);
                    break;
                case 9:
                    ConfigName.getInstance().AutKey=item.value;
                    break;
                case 10:
                    ConfigName.getInstance().Address=item.value;
                    break;
                case 11:
                    ConfigName.getInstance().Post=Integer.parseInt(item.value);
                    break;
                case 12:
                    ConfigName.getInstance().Environment=item.value;
                    break;
            }

        }
    }

    /**
     * 切换环境
     */
    public void ToggleEnvironment()
    {
        if(ConfigName.getInstance().Environment.equals("开发环境"))
        {
            ConfigName.getInstance().SaasAddress="https://bpa.black-pa.com:21527/";
            ConfigName.getInstance().mqtt_userName="emqx_u_block";
            ConfigName.getInstance().mqtt_passWord="emqx_p_admin8765490789";
            ConfigName.getInstance().mqtt_ip="10.2.1.21";
            ConfigName.getInstance().mqtt_post=1883;
        }else if(ConfigName.getInstance().Environment.equals("测试环境"))
        {
            ConfigName.getInstance().SaasAddress="https://bpa.black-pa.com:21528/";
            ConfigName.getInstance().mqtt_userName="emqx_u_block";
            ConfigName.getInstance().mqtt_passWord="emqx_p_admin8456765490789";
            ConfigName.getInstance().mqtt_ip="111.9.47.105";
            ConfigName.getInstance().mqtt_post=18889;
        }else if(ConfigName.getInstance().Environment.equals("正式环境"))
        {
            ConfigName.getInstance().SaasAddress="https://witt.black-pa.com/";
            ConfigName.getInstance().mqtt_userName="emqx_u_block";
            ConfigName.getInstance().mqtt_passWord="emqx_p_admin3658765490789";
            ConfigName.getInstance().mqtt_ip="47.108.65.220";
            ConfigName.getInstance().mqtt_post=1883;
        }
    }
    /**
     * 加载店铺配置
     */
    public void LoadingDPPZ() {
        //ConfigName.getInstance().AutKey
        ConfigName.getInstance().Shop_Name="黑菠萝奶茶机";
        ConfigName.getInstance().DeviceId="20020415-test";
    }
    /**
     * 加载配置
     */
    public void LoadingPZ() {
        //1.店铺配置
//        ConfigName.getInstance().Shop_Name = ConfigUtil.read(ConfigName.getInstance().dishesCon, "Shop_Name", ConfigName.getInstance().Shop_Name);
//        ConfigName.getInstance().AutKey = ConfigUtil.read(ConfigName.getInstance().dishesCon, "AutKey", ConfigName.getInstance().AutKey);
        //2.用户名称
        ConfigName.getInstance().user.account = ConfigUtil.read(ConfigName.getInstance().dishesCon, "username", ConfigName.getInstance().user.account);
        ConfigName.getInstance().user.pass = ConfigUtil.read(ConfigName.getInstance().dishesCon, "password", ConfigName.getInstance().user.pass);
    }

    /**
     * 保存配置
     */
    public void SavePZ()
    {
        //1.店铺配置
//        ConfigUtil.write(ConfigName.getInstance().dishesCon,"Shop_Name", ConfigName.getInstance().Shop_Name);
//        ConfigUtil.write(ConfigName.getInstance().dishesCon,"AutKey", ConfigName.getInstance().AutKey);
        //2.用户信息
        ConfigUtil.write(ConfigName.getInstance().dishesCon,"username", ConfigName.getInstance().user.account);
        ConfigUtil.write(ConfigName.getInstance().dishesCon,"password", ConfigName.getInstance().user.pass);
    }

    /**
     * 获取订单信息
     */
    public void GetOrder(String orderNumber)
    {
        //https://bpa.black-pa.com:21527/order/order/fullorderbynumber/202305111151562071
       // orderNumber="454228787056480256";
        //202202211310398967
        //String api="https://bpa.black-pa.com:21528/kitchen/api/member-helper/memberinfobywechat/987654321";
        Log.i("查询用户接口", ConfigName.getInstance().SaasAddress+ConfigName.getInstance().GetOrder+orderNumber);
        HttpUtils.httpGet(MessageName.GetOrderInformation.name(),ConfigName.getInstance().SaasAddress+ConfigName.getInstance().GetOrder+orderNumber);
    }

    public void SetOrderChanged(OrderStatusChange data)
    {
        //Authorize
//        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());;
//        String header = ConfigName.getInstance().SaasAddress+ConfigName.getInstance().OrderChange+"_["+ Date..Ticks+"]";
//        header=AES.encrypt(header);
        //密码加密
        //String pwdAES = AESCBCUtils.encrypt_AES(AESKey, loginPassword.getText().toString(), AESIv);

        String msg= new Json<OrderStatusChange>().objectToJson(OrderStatusChange.class, data);
        HttpUtils.httpPost(MessageName.UpdateOrderStatus.name(),ConfigName.getInstance().SaasAddress+ConfigName.getInstance().OrderChange,msg);
    }
    //endregion
}
