package com.zksy.pole.factory;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zksy.pole.domain.po.*;
import com.zksy.pole.factory.impl.*;
import com.zksy.pole.utils.LockUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class HandleCmdFactory {
    @Autowired
    private HandleDgEquipmentAlarmCmd handleDgEquipmentAlarmCmd;
    @Autowired
    private HandleDgEquipmentStatusCmd handleDgEquipmentStatusCmd;
    @Autowired
    private HandleDgGatewayRegistrationCmd handleDgGatewayRegistrationCmd;
    @Autowired
    private HandleDgGatewayStrategyCmd handleDgGatewayStrategyCmd;
    @Autowired
    private HandleDgLampControlledHeartbeatCmd handleDgLampControlledHeartbeatCmd;
    @Autowired
    private HandleDgRelayControlCmd handleDgRelayControlCmd;
    @Autowired
    private HandleDgRelayTimingStrategyCmd handleDgRelayTimingStrategyCmd;
    @Autowired
    private HandleDgReportEnvironmentalDataCmd handleDgReportEnvironmentalDataCmd;
    @Autowired
    private HandleDgTimelyControlCmd handleDgTimelyControlCmd;
    @Autowired
    private LockUtils lockUtils;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public String HandleCmd(String data) {
        JSONObject jsonObject = JSONUtil.parseObj(data);
        int cmd = jsonObject.getInt("cmd");
        if (jsonObject.getInt("timestamp") != null) {
            Long timestamp = jsonObject.getLong("timestamp") * 1000;
            Date date = new Date(timestamp);
            jsonObject.set("date_time", DateUtil.formatDateTime(date));
        }
        String[] paramNames = {"params", "params2", "params3"};
        for (String paramName : paramNames) {
            convertToJsonString(jsonObject, paramName);
        }
        lockUtils.lock("lock:cmd:" + cmd);
        lockUtils.forceUnlock("lock:cmd:" + cmd);
        redisTemplate.delete("lock:cmd:" + cmd);
        redisTemplate.opsForValue().set("cmd:"+cmd,jsonObject.toString(),20, TimeUnit.SECONDS);
        String s;
        switch (cmd) {
            case 6031:
                s = handleDgEquipmentAlarmCmd.HandleCmdMethod(jsonObject.toBean(DgEquipmentAlarm.class));
                return s;
            case 6012:
                s = handleDgEquipmentStatusCmd.HandleCmdMethod(jsonObject.toBean(DgEquipmentStatus.class));
                return s;
            case 1111:
                s = handleDgGatewayRegistrationCmd.HandleCmdMethod(jsonObject.toBean(DgGatewayRegistration.class));
                return s;
            case 6022:
                s = handleDgGatewayStrategyCmd.HandleCmdMethod(jsonObject.toBean(DgGatewayStrategy.class));
                return s;
            case 6010:
                s = handleDgLampControlledHeartbeatCmd.HandleCmdMethod(jsonObject.toBean(DgLampControlledHeartbeat.class));
                return s;
            case 6014:
                s = handleDgRelayControlCmd.HandleCmdMethod(jsonObject.toBean(DgRelayControl.class));
                return s;
            case 6015:
                s = handleDgRelayTimingStrategyCmd.HandleCmdMethod(jsonObject.toBean(DgRelayTimingStrategy.class));
                return s;
            case 2000:
                s = handleDgReportEnvironmentalDataCmd.HandleCmdMethod(jsonObject.toBean(DgReportEnvironmentalData.class));
                return s;
            case 6011:
                s = handleDgTimelyControlCmd.HandleCmdMethod(jsonObject.toBean(DgTimelyControl.class));
                return s;
            default:
                log.info("cmd有误或者没有对应的cmd处理");
                return null;
//                throw new CommonException("cmd有误或者没有对应的cmd处理", 500);
        }
    }
    private void convertToJsonString(JSONObject jsonObject, String paramName) {
        Object value = jsonObject.get(paramName);
        if (value != null) {
            jsonObject.set(paramName, value.toString());
        }
    }
}
