package com.bonait.bnframework.business;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_SYSTEMSET;
import com.bonait.bnframework.common.helper.ConfigUtil;
import com.bonait.bnframework.common.helper.HttpUtils;
import com.bonait.bnframework.common.helper.Json;
import com.bonait.bnframework.common.helper.mode.OrderStatusChange;
import com.bonait.bnframework.common.helper.mode.VersionMode;
import com.bonait.bnframework.common.image.utils.LocalCacheUtils;

import java.util.ArrayList;



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

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

    private ConfigData() { //默认构造函数私有，防止类外直接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(BPA_SYSTEMSET item:bpa_systemsets)
//        {
//            QueryDB.DeleteSystemset(item.id);
//        }
        for (int i = 0; i <5;i++){
            int ty=i;
            BPA_SYSTEMSET set=new BPA_SYSTEMSET();
            set.type=ty;
            switch (ty)
            {
                case 0:
                    set.value=ConfigName.getInstance().Environment;
                    break;
                case 1:
                    set.value=ConfigName.getInstance().Address;
                    break;
                case 2:
                    set.value=String.valueOf(ConfigName.getInstance().Post);
                    break;
                case 3:
                    set.value=ConfigName.getInstance().ClientId;
                    break;
                case 4:
                    set.value=ConfigName.getInstance().DeviceId;
                    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);
            }
        }
        bpa_systemsets= QueryDB.GetSystemsetALL();
        for (BPA_SYSTEMSET item:bpa_systemsets)
        {
            switch (item.type)
            {
                case 0:
                    ConfigName.getInstance().Environment=item.value;
                    break;
                case 1:
                    ConfigName.getInstance().Address=item.value;
                    break;
                case 2:
                    ConfigName.getInstance().Post=Integer.parseInt(item.value);
                    break;
                case 3:
                    ConfigName.getInstance().ClientId=item.value;
                    break;
                case 4:
                    ConfigName.getInstance().DeviceId=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);
        ConfigName.getInstance().IsUserDownOrder = ConfigUtil.read(ConfigName.getInstance().dishesCon, "IsUserDownOrder", ConfigName.getInstance().IsUserDownOrder);
        ConfigName.getInstance().Version = ConfigUtil.read(ConfigName.getInstance().dishesCon, "Version", ConfigName.getInstance().Version);
    }

    /**
     * 保存配置
     */
    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);
        ConfigUtil.write(ConfigName.getInstance().dishesCon,"IsUserDownOrder", ConfigName.getInstance().IsUserDownOrder);
        ConfigUtil.write(ConfigName.getInstance().dishesCon,"IsUserDownOrder", ConfigName.getInstance().IsUserDownOrder);
        ConfigUtil.write(ConfigName.getInstance().dishesCon,"Version", ConfigName.getInstance().Version);
    }

    /**
     * 增加本地图片
     */
    public void AddImage(Context context)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize=2;//宽高压缩为原来的1/2
        Bitmap bitmap1= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image1,options);
        Bitmap bitmap2= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image2,options);
        Bitmap bitmap3= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image3,options);
        Bitmap bitmap4= BitmapFactory.decodeResource(context.getResources(), R.mipmap.image4,options);
        LocalCacheUtils localCacheUtils=new LocalCacheUtils();
        localCacheUtils.setBitmapToLocal("image1.png",bitmap1);
        localCacheUtils.setBitmapToLocal("image2.png",bitmap2);
        localCacheUtils.setBitmapToLocal("image3.png",bitmap3);
        localCacheUtils.setBitmapToLocal("image4.png",bitmap4);
    }

    /**
     * 获取订单信息
     */
    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,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,ConfigName.getInstance().SaasAddress+ConfigName.getInstance().OrderChange,msg);
    }

    /**
     * 更新版本
     */
    public void UpdateVersion()
    {
        VersionMode mode=new VersionMode();
        mode.productCode="1668085292913659904";
        mode.moduleCode="naichaji";
        mode.serverCode="1668175205545807872";
        mode.branchCode="1668801915890962433";
        String msg= new Json<VersionMode>().objectToJson(VersionMode.class, mode);
        HttpUtils.httpPost(MessageName.UpdateVersion,ConfigName.getInstance().UpdateVersion,msg);
    }
    //endregion
}

