package com.bpa.scalage.app;

import android.util.Log;

import com.bpa.scalage.MainActivity;
import com.bpa.scalage.config.ConfigName;
import com.bpa.scalage.config.MessageName;
import com.bpa.scalage.data.DataBus;
import com.bpa.scalage.helper.ConfigUtil;
import com.bpa.scalage.helper.HttpUtils;
import com.bpa.scalage.helper.OkhttpUtils;

/**
 * 主函数进程
 * 主要进行配置文件-系统类
 */
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.加载配置
        LoadingPZ();
        //0.加载环境配置
        ToggleEnvironment();
        //2.获取组织信息
        GetOrganize();
    }

    /**
     * 切换环境
     */
    public void ToggleEnvironment() {
        if (ConfigName.getInstance().Environment.equals("开发环境")) {
            ConfigName.getInstance().SaasAddress = "https://bpa.black-pa.com:21527";
        } else if (ConfigName.getInstance().Environment.equals("测试环境")) {
            ConfigName.getInstance().SaasAddress = "https://bpa.black-pa.com:21528";
        } else if (ConfigName.getInstance().Environment.equals("正式环境")) {
            ConfigName.getInstance().SaasAddress = "https://witt.black-pa.com";
        }
    }

    /**
     * 加载配置
     */
    public void LoadingPZ()
    {
        ConfigName.getInstance().Environment =ConfigUtil.read(DataBus.getInstance().dishesCon,"Environment", ConfigName.getInstance().Environment);
        //1.店铺配置
        ConfigName.getInstance().Shop_Name =ConfigUtil.read(DataBus.getInstance().dishesCon,"Shop_Name", ConfigName.getInstance().Shop_Name);
        ConfigName.getInstance().AutKey =ConfigUtil.read(DataBus.getInstance().dishesCon,"AutKey", ConfigName.getInstance().AutKey);
        ConfigName.getInstance().Shop_DeviceId =ConfigUtil.read(DataBus.getInstance().dishesCon,"Shop_DeviceId", ConfigName.getInstance().Shop_DeviceId);
        //2.API接口
        ConfigName.getInstance().SaasAddress =ConfigUtil.read(DataBus.getInstance().dishesCon,"SaasAddress", ConfigName.getInstance().SaasAddress);
        //3.菜品放置区端口名称
        ConfigName.getInstance().Dishes_Com_Name =ConfigUtil.read(DataBus.getInstance().dishesCon,"Dishes_Com_Name", ConfigName.getInstance().Dishes_Com_Name);
        ConfigName.getInstance().Dishes_Com_Port =ConfigUtil.read(DataBus.getInstance().dishesCon,"Dishes_Com_Port", ConfigName.getInstance().Dishes_Com_Port);
        //4.餐盘感应区域端口名称
        ConfigName.getInstance().Plate_Com_Name =ConfigUtil.read(DataBus.getInstance().dishesCon,"Plate_Com_Name", ConfigName.getInstance().Plate_Com_Name);
        ConfigName.getInstance().Plate_Com_Port =ConfigUtil.read(DataBus.getInstance().dishesCon,"Plate_Com_Port", ConfigName.getInstance().Plate_Com_Port);
        ConfigName.getInstance().Plate_Com_Format =ConfigUtil.read(DataBus.getInstance().dishesCon,"Plate_Com_Format", ConfigName.getInstance().Plate_Com_Format);
    }

    /**
     * 保存配置
     */
    public void SavePZ()
    {
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Environment", ConfigName.getInstance().Environment);
        //1.店铺配置
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Shop_Name", ConfigName.getInstance().Shop_Name);
        ConfigUtil.write(DataBus.getInstance().dishesCon,"AutKey", ConfigName.getInstance().AutKey);
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Shop_DeviceId", ConfigName.getInstance().Shop_DeviceId);
        //2.API接口
        ConfigUtil.write(DataBus.getInstance().dishesCon,"SaasAddress", ConfigName.getInstance().SaasAddress);
        //3.菜品放置区端口名称
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Dishes_Com_Name", ConfigName.getInstance().Dishes_Com_Name);
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Dishes_Com_Port", ConfigName.getInstance().Dishes_Com_Port);
        //4.餐盘感应区域端口名称
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Plate_Com_Name", ConfigName.getInstance().Plate_Com_Name);
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Plate_Com_Port", ConfigName.getInstance().Plate_Com_Port);
        ConfigUtil.write(DataBus.getInstance().dishesCon,"Plate_Com_Format", ConfigName.getInstance().Plate_Com_Format);

        ToggleEnvironment();
    }

    /**
     * 写配置
     * @param name
     * @param value
     */
    public void write(String name,Object value)
    {
        if(value instanceof Integer)
        {
            ConfigUtil.write(DataBus.getInstance().dishesCon,name,(int)value);
        }else
        {
            ConfigUtil.write(DataBus.getInstance().dishesCon,name,(String) value);
        }
    }

    /**
     * 获取店铺组织信息
     */
    public void GetOrganize()
    {
        HttpUtils.httpGet(MessageName.GetOrganize.name(),ConfigName.getInstance().GetStore+ConfigName.getInstance().AutKey);
    }
    /**
     * 获取菜品
     */
    public void GetCaiPin()
    {
        HttpUtils.httpGet(MessageName.PullTheDishes.name(),ConfigName.getInstance().GetFruitUrl+ConfigName.getInstance().Shop_Id);
    }

    /**
     * 获取用户信息-是否会员信息
     */
    public void GetUser(String OpenId)
    {
        //oh0Xy4v9t2AwutYMpRTB51z6falI
        //String api="https://bpa.black-pa.com:21528/kitchen/api/member-helper/memberinfobywechat/987654321";
        Log.i("查询用户接口", ConfigName.getInstance().GetUserUrl+OpenId);
        HttpUtils.httpPost(MessageName.GetUserInformation.name(),ConfigName.getInstance().GetUserUrl+OpenId,"");
    }

    /**
     * 获取卡与订单关联关系
     */
    public void GetUserAndOrderGX(String CartId)
    {
        HttpUtils.httpGet(MessageName.GetUserAndCartGX.name(),ConfigName.getInstance().GetUserOrCartGX+CartId+"/"+ConfigName.getInstance().Shop_Id);
    }

    /**
     * 发送菜品信息
     * @param message
     */
    public void AddSubOrder(String message)
    {
        HttpUtils.httpPost(MessageName.AddSuOrderUrl.name(),ConfigName.getInstance().AddSuOrderUrl,message);
    }
    //endregion
}
