package com.qmth.paper.library.common.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.paper.library.common.base.BaseEntity;
import com.qmth.paper.library.common.bean.dto.syssetting.SettingObject;
import com.qmth.paper.library.common.bean.dto.syssetting.SimpleObject;
import com.qmth.paper.library.common.bean.dto.syssetting.SimpleObjectOptions;
import com.qmth.paper.library.common.contant.SysSettingConstant;
import com.qmth.paper.library.common.enums.RecognitionTypeEnum;
import com.qmth.paper.library.common.enums.StoreTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统设置表
 * </p>
 */
@TableName("sys_setting")
public class SysSetting extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "学校id")
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("school_id")
    private Long schoolId;


    /**
     * 是否开启全局匹配
     */
    @TableField("parameter_object")
    private String parameterObject;

    public Long getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Long schoolId) {
        this.schoolId = schoolId;
    }

    public String getParameterObject() {
        return parameterObject;
    }

    public void setParameterObject(String parameterObject) {
        this.parameterObject = parameterObject;
    }

    /**
     * 初始化数据
     */
    public static SysSetting init(Long schoolId) {
        List<SettingObject> settingObjects = new ArrayList<>();
//        List<SimpleObject> simpleObjects1 = new ArrayList<>();
//        List<SimpleObjectOptions> trueOrFalseOptions = JSON.parseArray("[{\"name\":\"开启\",\"value\":true},{\"name\":\"关闭\",\"value\":false}]", SimpleObjectOptions.class);
//        simpleObjects1.add(new SimpleObject(SysSettingConstant.SYS_VERIFICATION_CODE_LOGIN_ENABLE, "验证码登录", "false", 2, true, trueOrFalseOptions));
//        simpleObjects1.add(new SimpleObject(SysSettingConstant.SYS_SMS_NOTIFICATION_ENABLE, "短信提醒功能", "false", 3, true, trueOrFalseOptions));
//
//        settingObjects.add(new SettingObject("系统设置", 1, simpleObjects1));

        List<SimpleObject> simpleObjects2 = new ArrayList<>();
        List<SimpleObjectOptions> recognizeOptions = Arrays.stream(RecognitionTypeEnum.values()).map(m-> new SimpleObjectOptions(m.getDesc(), m.name())).collect(Collectors.toList());
        String recognizeRemark = "1、如果开启OCR识别或条码识别，必须导入考生信息；\n" +
                "2、该设置修改后，仅对后续扫描结果生效。";
        simpleObjects2.add(new SimpleObject(SysSettingConstant.RECOGNITION_TYPE, "识别模式", "", 1, true, recognizeOptions, recognizeRemark));
        List<SimpleObjectOptions> storeOptions = Arrays.stream(StoreTypeEnum.values()).map(m-> new SimpleObjectOptions(m.getDesc(), m.name())).collect(Collectors.toList());
        String storeRemark = "1、文件存储规则：学期/考试/课程/\n" +
                "2、选择按考场/班级存储，则考生数据必须包含考场及学生信息；存储规则：学期/考试/课程/考场(班级)";
        simpleObjects2.add(new SimpleObject(SysSettingConstant.STORE_TYPE, "文件存储设置", "", 1, true, storeOptions, storeRemark));
        settingObjects.add(new SettingObject("识别设置", 2, simpleObjects2));

        SysSetting sysSetting = new SysSetting();
        sysSetting.setSchoolId(schoolId);
        sysSetting.setParameterObject(JSON.toJSONString(settingObjects));
        return sysSetting;
    }

    public static String parseParameterObject(SysSetting sysSetting, String key) {
        if (sysSetting != null && StringUtils.isNotBlank(sysSetting.getParameterObject())) {
            List<SettingObject> settingObjects = JSON.parseArray(sysSetting.getParameterObject(), SettingObject.class);
            for (SettingObject settingObject : settingObjects) {
                for (SimpleObject simpleObject : settingObject.getData()) {
                    if (simpleObject.getCode().equals(key)) {
                        return simpleObject.getValue();
                    }
                }
            }
        }
        return null;
    }
}
