package com.yeejoin.amos.boot.module.jg.biz.reminder.biz.newEquip;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.yeejoin.amos.boot.module.jg.biz.edit.process.equip.CommonEquipDataProcessService;
import com.yeejoin.amos.boot.module.jg.biz.reminder.dto.MatchItemDto;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.IdxBizJgRegisterInfoServiceImpl;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.IdxBizJgUseInfoServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.entity.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.yeejoin.amos.boot.module.jg.biz.service.impl.IdxBizJgRegisterInfoServiceImpl.*;

@Component
@RequiredArgsConstructor
@Getter
public class NewEquipParseService {
    private final IdxBizJgUseInfoServiceImpl idxBizJgUseInfoService;

    private final IdxBizJgRegisterInfoServiceImpl idxBizJgRegisterInfoService;

    private final CommonEquipDataProcessService equipDataProcessService;

    public String getRegTypeForEdit(String record) {
        IdxBizJgUseInfo useInfo = idxBizJgUseInfoService.getOneData(record);
        String dataSource = useInfo.getDataSource();
        if (dataSource.contains("jg_his_black")) {
            return "2";
        } else if (dataSource.contains("jg_his")) {
            return "1";
        } else {
            return "0";
        }
    }

    public String getRegTypeForInsert(Map<String, Object> formData) {
        String dataSource = formData.get("DATA_SOURCE").toString();
        return "new".equals(dataSource) ? "0" : "his".equals(dataSource) ? "1" : "2";
    }

    public void setEquCateInfoForInsert(Map<String, Object> formData, MatchItemDto matchItemDto) {
        LinkedHashMap equipmentClassForm = (LinkedHashMap) checkAndCast(formData.get(EQUIP_CLASS_FORM_ID));
        String equListCode = (String) equipmentClassForm.get("EQU_LIST");
        String equCategoryCode = (String) equipmentClassForm.get("EQU_CATEGORY");
        String equDefineCode = (String) equipmentClassForm.get("EQU_DEFINE");
        matchItemDto.setEquList(equListCode);
        matchItemDto.setEquCategory(equCategoryCode);
        matchItemDto.setEquDefine(equDefineCode);
    }

    public void setEquCateInfoForEdit(String record, MatchItemDto matchItemDto) {
        IdxBizJgRegisterInfo registerInfo = idxBizJgRegisterInfoService.getOneData(record);
        matchItemDto.setEquList(registerInfo.getEquList());
        matchItemDto.setEquCategory(registerInfo.getEquCategory());
        matchItemDto.setEquDefine(registerInfo.getEquDefine());
    }

    private Object checkAndCast(Object obj) {
        if (obj instanceof LinkedHashMap) {
            return obj;
        } else {
            throw new ClassCastException("对象类型不匹配");
        }
    }

    public Map<String, Object> getEquipDetailByRecord(String record) {
        return equipDataProcessService.getEquipDetailByRecord(record);
    }

    public IdxBizJgRegisterInfo getRegisterInfoFromJson(Map<String, Object> formData) {
        LinkedHashMap equipmentInfoForm = (LinkedHashMap) checkAndCast(formData.get(EQUIP_INFO_FORM_ID));
        return JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgRegisterInfo.class);
    }

    public Map<String, Object> getDetailInfoFromJson(Map<String, Object> formData) {
        Map<String, Object> re = new HashMap<>();
        LinkedHashMap equipmentInfoForm = (LinkedHashMap) checkAndCast(formData.get(EQUIP_INFO_FORM_ID));
        LinkedHashMap equipmentParamsForm = (LinkedHashMap) checkAndCast(formData.get(EQUIP_PARAMS_FORM_ID));
        // 使用信息
        IdxBizJgUseInfo useInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgUseInfo.class);
        // 设计信息
        IdxBizJgDesignInfo designInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgDesignInfo.class);
        // 制造信息
        IdxBizJgFactoryInfo factoryInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgFactoryInfo.class);
        // 施工信息
        IdxBizJgConstructionInfo constructionInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgConstructionInfo.class);
        // 注册登记信息
        IdxBizJgRegisterInfo registerInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgRegisterInfo.class);
        // 监督管理
        IdxBizJgSupervisionInfo supervisionInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgSupervisionInfo.class);
        // 其他信息
        IdxBizJgOtherInfo otherInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgOtherInfo.class);
        IdxBizJgInspectionDetectionInfo inspectionDetectionInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgInspectionDetectionInfo.class);
        IdxBizJgMaintenanceRecordInfo idxBizJgMaintenanceRecordInfo = JSON.parseObject(JSON.toJSONString(equipmentInfoForm), IdxBizJgMaintenanceRecordInfo.class);
        re.putAll(BeanUtil.beanToMap(useInfo));
        re.putAll(BeanUtil.beanToMap(registerInfo));
        re.putAll(BeanUtil.beanToMap(factoryInfo));
        re.putAll(BeanUtil.beanToMap(otherInfo));
        re.putAll(BeanUtil.beanToMap(designInfo));
        re.putAll(BeanUtil.beanToMap(supervisionInfo));
        re.putAll(BeanUtil.beanToMap(inspectionDetectionInfo));
        re.putAll(BeanUtil.beanToMap(idxBizJgMaintenanceRecordInfo));
        re.putAll(BeanUtil.beanToMap(constructionInfo));
        re.putAll(equipmentParamsForm);
        return re;
    }

}
