package com.example.bpa.app;

import android.app.Application;
import android.util.Log;


import com.example.bpa.Model.IMessageLogNotify;
import com.example.bpa.config.ConfigName;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.file.DBHelper;
import com.example.bpa.db.mode.BPA_ALERTLOG;
import com.example.bpa.db.mode.BPA_LOG;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.db.mode.BPA_SILOSANDMATERIAL;
import com.example.bpa.db.mode.BPA_SUBORDER;
import com.example.bpa.db.mode.BPA_USER;
import com.example.bpa.helper.AES;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.helper.SdCart;
import com.example.bpa.helper.ToastUtil;
import com.example.bpa.service.BusinessServer;
import com.example.bpa.view.mode.AlertLogEnum;
import com.example.bpa.view.mode.UserLogEnum;

/**
 * 程序启动执行
 * 创建:fengyoufu 20230413
 */
public class ICSApp extends Application {
    private static ICSApp instance;
    @Override
    public void onCreate() {
        super.onCreate();
        //1.设置程序active,初始化Main函数进程,初始化消息日志
        InitMsgLog();ConfigName.getInstance().dishesCon = this;ToastUtil.init(this);
        //2.初始化SD卡,数据库DB
        SdCart.getInstance().initSD();
        //3.初始化数据
        InitDBdata();
        //4.初始化Main
        Main.getInstance();

        //BusinessServer.Get().test();
        //MessageLog.ShowAlertMessage(AlertLogEnum.传感器异常日志,"传感器异常");
        //MessageLog.ShowAlertMessage(AlertLogEnum.异常订单未制作日志,"异常订单未制作日志");
        //MessageLog.ShowAlertMessage(AlertLogEnum.料仓缺料日志,"料仓缺料日志");
    }

    /**
     * 初始化消息日志接收打印
     */
    public void InitMsgLog(){
        MessageLog.MsgNotify = new IMessageLogNotify() {
            @Override
            public void ErrorMsg(String msg) {
                Log.e("Error", msg);
            }
            @Override
            public void InfoMsg(String msg) {
                Log.i("Info", msg);
            }
            @Override
            public void WarnMsg(String msg) {
                Log.w("Warn", msg);
            }
            @Override
            public void UserMsg(UserLogEnum type, String msg) {
                BPA_LOG log = new BPA_LOG();
                log.userID = ConfigName.getInstance().user.userID;
                switch (type.toString())
                {
                    case "登录日志":log.type=1;
                        break;
                    case "角色操作日志":log.type=2;
                        break;
                    case "数据接收":log.type=3;
                        break;
                    case "上传日志":log.type=4;
                        break;
                    case "订单处理日志":log.type=5;
                        break;
                }
                log.text = msg;
                QueryDB.Addlog(log);
            }

            @Override
            public void AlertMsg(AlertLogEnum type, String msg) {
                BPA_ALERTLOG log = new BPA_ALERTLOG();
                log.userID = ConfigName.getInstance().user.userID;
                switch (type.toString())
                {
                    case "异常订单未制作日志":log.type=1;
                        break;
                    case "料仓缺料日志":log.type=2;
                        break;
                    case "传感器异常日志":log.type=3;
                        break;
                    case "其他":log.type=4;
                        break;
                }
                log.text = msg;
                QueryDB.AddAlertlog(log);
            }
        };
    }
    /**
     * 初始化数据库数据(默认数据)
     */
    public void InitDBdata() {
        //1.没有用户数据 默认写一个admin
        if (QueryDB.GetUserALL().size() <= 0) {
            BPA_USER user = new BPA_USER();
            user.id = "20230415-fyf";
            user.name = "管理员";
            user.account = "admin";
            user.pass = "123456";
            user.deviceID = ConfigName.getInstance().DeviceId;
            user.userID = "20230415-fyf";
            user.exp = "管理员";
            QueryDB.AddUser(user);
            //10个料仓+ 11 12 两个果糖料仓
            if (QueryDB.GetSilosALL().size() == 0) {
                for (int i = 0; i < 12; i++) {
                    BPA_SILOS silo = new BPA_SILOS();
                    silo.id = java.util.UUID.randomUUID().toString();
                    silo.num = i + 1;
                    silo.thrsoleValue=ConfigName.getInstance().SiloAlarmThreshold;
                    silo.warningValue=ConfigName.getInstance().SiloFeedThreshold;
                    BPA_SILOSANDMATERIAL sm = new BPA_SILOSANDMATERIAL();
                    sm.silosID = silo.id;
                    sm.materialID = "";
                    QueryDB.AddSilos(silo);
                    QueryDB.AddSilosAndMaterial(sm);
                }
            }
        }
    }

    /**
     * 删除子订单表-重新生成新表
     */
    public  void DBdelete()
    {
        DBHelper.getInstance(ConfigName.getInstance().dishesCon).DeleteCreateTables(BPA_SUBORDER.class,null);
    }
}
