package com.huatek.busi.service.impl.curve.helper;

import java.util.Map;

import com.huatek.busi.common.CurveSubType;
import com.huatek.busi.entity.*;
import org.springframework.util.StringUtils;

import com.google.common.collect.Maps;
import com.huatek.busi.common.BusiConstant.EqType;
import com.huatek.busi.common.BusiConstant.GzType;
import com.huatek.busi.common.CurveSubType.CapEquipmentImSubTypeEnum;
import com.huatek.busi.common.CurveSubType.CwgSubTypeEnum;
import com.huatek.busi.common.CurveSubType.GisSf6PressureSubTypeEnum;
import com.huatek.busi.common.CurveSubType.IcgeSubTypeEnum;
import com.huatek.busi.common.CurveSubType.InfraredTmpTypeEnum;
import com.huatek.busi.common.CurveSubType.LightningrodImSubTypeEnum;
import com.huatek.busi.common.CurveSubType.MeteorologicalSubTypeEnum;
import com.huatek.busi.common.CurveSubType.ReadTableTypeEnum;
import com.huatek.busi.common.CurveSubType.RunStateSubTypeEnum;
import com.huatek.busi.common.CurveSubType.RunStatusSubTypeEnum;
import com.huatek.busi.common.CurveSubType.YspGasEnum;
import com.huatek.busi.common.CurveType;
import com.huatek.torch.frame.exception.OwlException;

import lombok.extern.slf4j.Slf4j;

/**
 * 曲线图映射配置
 *
 * @author anker_cheng
 * @date 2019/09/11
 */
@Slf4j
public final class CurveMapping {

    private static final Map<String, CurveType> CHART_MAPPING;
    private static final Map<String, String> TABLE_MAPPING;

    static {
        CHART_MAPPING = Maps.newHashMapWithExpectedSize(10);
        TABLE_MAPPING = Maps.newHashMapWithExpectedSize(16);
        init0();
        init1();
    }

    static void init0() {
        // 油色谱
        put(TransformerEpaData.class, CurveType.newInstance("油色谱", 12).addAll(YspGasEnum.LIST));

        // 变压器运维工况曲线定义
        put(TransformerRunStatus.class, CurveType.newInstance("运维工况", 12).addAll(RunStatusSubTypeEnum.LIST));

        // 测温柜
        put(CwgTemperature.class, CurveType.newInstance("测温柜", 12).addAll(CwgSubTypeEnum.LIST));

        // 红外测温
        put(TransformerInfraredTemperature.class, CurveType.newInstance("红外测温", 12).addAll(InfraredTmpTypeEnum.LIST));

        // 铁芯夹件
        put(TransformerIcge.class, CurveType.newInstance("电流", 1).addAll(IcgeSubTypeEnum.LIST));

        // 变压器工作状态
        put(IecBydworkParam.class, CurveType.newInstance("变压器负荷", 1).addAll(RunStateSubTypeEnum.LIST));

        // 读表——图片数据
        put(TransformerReadTable.class, CurveType.newInstance("变压器图像数据", 1)
                .add(ReadTableTypeEnum.TRANSFORMER_DATAVAL.getValue(), ReadTableTypeEnum.TRANSFORMER_DATAVAL.getName()));
        put(GisReadTable.class, CurveType.newInstance("GIS图像数据", 1).add(ReadTableTypeEnum.GIS_DATAVAL.getValue(),
                ReadTableTypeEnum.GIS_DATAVAL.getName()));
        put(ArresterReadTable.class, CurveType.newInstance("避雷器图像数据", 1)
                .add(ReadTableTypeEnum.ARRESTER_DATAVAL.getValue(), ReadTableTypeEnum.ARRESTER_DATAVAL.getName()));

        //气象
        put(TransformerMeteorological.class, CurveType.newInstance("气象", 1).addAll(MeteorologicalSubTypeEnum.LIST));

        // GIS SF6
        put(GisSf6Pressure.class, CurveType.newInstance("Gis SF6", 1).addAll(GisSf6PressureSubTypeEnum.LIST));

        // 容性设备绝缘监测
        put(CapacitanceEquipmentIm.class, CurveType.newInstance("容性设备", 1).addAll(CapEquipmentImSubTypeEnum.LIST));

        // 避雷器绝缘监测
        put(LightningrodIm.class, CurveType.newInstance("避雷器", 1).addAll(LightningrodImSubTypeEnum.LIST));

        // 其他-电缆环流
        put(BusiCableCirculation.class, CurveType.newInstance("电缆环流", 1).addAll(CurveSubType.CableCirculationSubTypeEnum.LIST));

        //电缆-电缆环流
        put(IBusiCable.class, CurveType.newInstance("电缆环流", 1).addAll(CurveSubType.CableCirculationSubTypeEnum.LIST));

        // 测温装置
        put(CwzzTemperature.class, CurveType.newInstance("测温装置", 12).addAll(CurveSubType.CwzzTmpTypeEnum.LIST));
       //绕组测温
        put(TransformerWindingTemperature.class, CurveType.newInstance("绕组测温", 12).addAll(CurveSubType.RzcwTmpTypeEnum.LIST));
        //SF6环境监测
        put(GisSf6Env.class, CurveType.newInstance("SF6环境监测", 12).addAll(CurveSubType.Sf6EnvTypeEnum.LIST));
        // 发电机
        //振动
        put(BusiGeneratorAmpl.class, CurveType.newInstance("振动装置", 12).addAll(CurveSubType.GenZdTypeEnum.LIST));

        // 压力
        put(BusiGeneratorPressure.class, CurveType.newInstance("压力装置", 12).addAll(CurveSubType.GenYlTypeEnum.LIST));

        put(BusiGeneratorPd.class, CurveType.newInstance("局放装置", 12).addAll(CurveSubType.GenPdTypeEnum.LIST));
    }

    static void init1() {
        // 变压器
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE1, TransformerEpaData.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE2, TransformerPd.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE3, TransformerIcge.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE4, TransformerInfraredTemperature.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE8, TransformerIcge.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE9, TransformerRunStatus.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE11, TransformerReadTable.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE12, CapacitanceEquipmentIm.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE27, TransformerMeteorological.class);
        // 变压器套管
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE13, null);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE21, CwzzTemperature.class);
        put(EqType.EQ_TYPE1, GzType.GZ_TYPE20, TransformerWindingTemperature.class);
        // GIS
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE2, TransformerPd.class);
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE4, TransformerInfraredTemperature.class);
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE5, GisSf6Pressure.class);
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE11, GisReadTable.class);
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE22, GisSf6Env.class);
        put(EqType.EQ_TYPE2, GzType.GZ_TYPE27, TransformerMeteorological.class);
        // 避雷器
        put(EqType.EQ_TYPE3, GzType.GZ_TYPE4, TransformerInfraredTemperature.class);
        put(EqType.EQ_TYPE3, GzType.GZ_TYPE6, LightningrodIm.class);
        put(EqType.EQ_TYPE3, GzType.GZ_TYPE11, ArresterReadTable.class);
        put(EqType.EQ_TYPE3, GzType.GZ_TYPE12, CapacitanceEquipmentIm.class);
        put(EqType.EQ_TYPE3, GzType.GZ_TYPE27, TransformerMeteorological.class);

        // 测温柜
        put(EqType.EQ_TYPE4, GzType.GZ_TYPE7, CwgTemperature.class);
        put(EqType.EQ_TYPE4, GzType.GZ_TYPE10, CwgTemperature.class);
        put(EqType.EQ_TYPE4, GzType.GZ_TYPE11, null);
        put(EqType.EQ_TYPE4, GzType.GZ_TYPE21, CwgTemperature.class);

        // 其他-电缆环流
          put(EqType.EQ_TYPE5, GzType.GZ_TYPE17,BusiCableCirculation.class);

        //电缆
        put(EqType.EQ_TYPE7, GzType.GZ_TYPE17,IBusiCable.class); //电缆环流
        //电缆测温装置
        put(EqType.EQ_TYPE7, GzType.GZ_TYPE21, CwzzTemperature.class);
        //电缆红外测量
        put(EqType.EQ_TYPE7, GzType.GZ_TYPE4, TransformerInfraredTemperature.class);

        // 发电机
        // 振动
        put(EqType.EQ_TYPE6, GzType.GZ_TYPE23, BusiGeneratorAmpl.class);
        // 压力
        put(EqType.EQ_TYPE6, GzType.GZ_TYPE24, BusiGeneratorPd.class);
        put(EqType.EQ_TYPE6, GzType.GZ_TYPE26, BusiGeneratorPressure.class);
    }

    synchronized static void put(Class<?> clazz, CurveType curveType) {
        CHART_MAPPING.put(clazz.getName(), curveType);
    }

    synchronized static void put(String eqType, String gzType, Class<?> clazz) {
        if (clazz == null) {
            log.warn("table mapping is null, eqType: {}, gzType: {}", eqType, gzType);
            return;
        }
        TABLE_MAPPING.put(eqType + "-" + gzType, clazz.getName());
    }

    public static CurveType getCurveMapping(String key) {
        CurveType curveType = CHART_MAPPING.get(key);
//        if (curveType == null) {
//            throw new OwlException("未知配置：" + key);
//        }
        return curveType;
    }

    public static String getTableMapping(String eqType, String gzType) {
        String table = TABLE_MAPPING.get(eqType + "-" + gzType);
        if (StringUtils.isEmpty(table)) {
            throw new OwlException("未知配置：eqType = " + eqType + ", gzType = " + gzType);
        }
        return table;
    }

    public static CurveType getCurveMapping(String eqType, String gzType) {
        return getCurveMapping(getTableMapping(eqType, gzType));
    }
}
