package org.ts.ddcs.Worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.concurrent.Callable;

@SuppressWarnings("all")
public class DataNotifyProcess implements Callable<Integer> {

    private String rtuCode;

    DataNotifyProcess(String rtuCode) {
        this.rtuCode = rtuCode;
    }

    @Override
    public Integer call() throws Exception {
        try {
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            ListOperations<String, String> opsList = redisTemplate.opsForList();
            if (redisTemplate.hasKey(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_FILE)) {
                if (opsList.size(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_FILE) > 0) {
                    JSONObject body = new JSONObject();
                    body.put(GBConstants.BASE_KEY_RTU, this.rtuCode);
                    if (GBConstants.RELAY_KEY_MQ.equals(DataProcess.config().getReleayStyle())) {
                        HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
                        String master = opsHash.get(rtuCode, GBConstants.BASE_KEY_SERVER_MASTER);
                        Thread.sleep(1000);
                        DataProcess.MQNotifyMsgSender(master, body.toJSONString());
                        LogHelper.info(rtuCode + "***************** by MQ notify   try  download  datagram");
                    } else {
                        String notifyurl = DataProcess.config().getNotifyurl();
                        //DataProcessHolder.newInstance().dataProcess.notifyurl;
                        RestTemplate restTemplate = new RestTemplate();
                        JSONObject response = restTemplate.postForObject(new URI(notifyurl), body, JSONObject.class);
                        if (response != null) {
                            if (response.getString("code").equals("0")) {
                                LogHelper.info("***************** by HTTP  notify   try  download  datagram");
                            }
                        }
                    }
                    return 0;
                }
            }
            if (redisTemplate.hasKey(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_SETTING)) {
                if (opsList.size(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_SETTING) > 0) {
                    JSONObject body = new JSONObject();
                    body.put(GBConstants.BASE_KEY_RTU, this.rtuCode);
                    if (GBConstants.RELAY_KEY_MQ.equals(DataProcess.config().getReleayStyle())) {
                        HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
                        String master = opsHash.get(rtuCode, GBConstants.BASE_KEY_SERVER_MASTER);
                        Thread.sleep(1000);
                        DataProcess.MQNotifyMsgSender(master, body.toJSONString());
                        LogHelper.info(rtuCode + "***************** by MQ notify   try  download  datagram");
                    } else {
                        String notifyurl = DataProcess.config().getNotifyurl();
                        //String notifyurl = DataProcessHolder.newInstance().dataProcess.notifyurl;
                        RestTemplate restTemplate = new RestTemplate();
                        JSONObject response = restTemplate.postForObject(new URI(notifyurl), body, JSONObject.class);
                        if (response != null) {
                            if (response.getString("code").equals("0")) {
                                LogHelper.info("***************** by HTTP  notify   try  download  datagram");
                            }
                        }
                    }
                    return 0;
                }
            }
            if (redisTemplate.hasKey(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_QUERY)) {
                if (opsList.size(rtuCode + ":" + GBConstants.REDIS_KEY_COMMAND_QUERY) > 0) {
                    JSONObject body = new JSONObject();
                    body.put(GBConstants.BASE_KEY_RTU, this.rtuCode);
                    if (GBConstants.RELAY_KEY_MQ.equals(DataProcess.config().getReleayStyle())) {
                        HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
                        String master = opsHash.get(rtuCode, GBConstants.BASE_KEY_SERVER_MASTER);
                        Thread.sleep(1000);
                        DataProcess.MQNotifyMsgSender(master, body.toJSONString());
                        LogHelper.info(rtuCode + "***************** by MQ notify   try  download  datagram");
                    } else {
                        String notifyurl = DataProcess.config().getNotifyurl();
                        // String notifyurl = DataProcessHolder.newInstance().dataProcess.notifyurl;
                        RestTemplate restTemplate = new RestTemplate();
                        JSONObject response = restTemplate.postForObject(new URI(notifyurl), body, JSONObject.class);
                        if (response != null) {
                            if (response.getString("code").equals("0")) {
                                LogHelper.info("***************** by HTTP  notify   try  download  datagram");
                            }
                        }
                    }
                    return 0;
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
