package com.nokia.alarm.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.hl.opnc.AbilityResponse;
import com.hl.opnc.ResponseBody;
import com.hl.opnc.invoke.AbilityInvokeManager;
import com.hl.opnc.invoke.InvokeManager;
import com.nokia.alarm.entity.MessageCfg;
import com.nokia.alarm.entity.ResultInfo;
import com.nokia.alarm.entity.ResultMap;
import com.nokia.alarm.util.SDKUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ApiCallService {

    @Value("${httpurl:http://192.168.70.125:12090/esb/HEB_AlarmCustomQuery/0?authcode=SEVCX1VDSUFQI0BhdXRoQCNTWnFsVk9QeA}")
    private String methodUrl;
    @Value("${appId:0f9bf497549343e68ca36b4caffbc8c4}")
    private String appId;
    @Value("${secret:087b1ceb698140cd8c65543f5ace0490}")
    private String secret;

    @Autowired
    private SmsSendService smsService;

    public List<ResultMap> getNewAlarm(Date date) {
        log.info("查询新增告警开始...");
        InvokeManager manager = new AbilityInvokeManager(appId, secret);
        MessageCfg cfg = SDKUtil.getBody(date);
        AbilityResponse response = manager.post(methodUrl, null, JSON.toJSONString(cfg));
        try {
            String responseStr = response.getRespStr();
            ResponseBody resObj;
            try {
                resObj = JSON.parseObject(responseStr, ResponseBody.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                // 发送短信
                smsService.send("13231899751", date + " 查询新增告警 返回消息第一层json解析错误");
                return null;
            }
            String body = (String) resObj.getUNI_NET_BODY();
            // log.info(body);
            body = body.replace("null", "\"\"");
            ResultInfo info;
            try {
                info = JSON.parseObject(body, ResultInfo.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                // 发送短信
                smsService.send("13231899751", date + " 查询新增告警 返回消息第二层json解析错误");
                return null;
            }
            int rows = info.getTotal();
            log.info("新增告警返回数据{}条", rows);
            return info.getResult();
        } catch (Exception e) {
            log.error("查询新增告警出错: {}", e.getMessage());
            // 发送短信
            smsService.send("13231899751", date + " 查询新增告警出错" + e.getMessage());
            e.printStackTrace();
            return null;
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public List<ResultMap> getCanceledAlarm(Date date) {
        log.info("查询消除告警开始...");
        InvokeManager manager = new AbilityInvokeManager(appId, secret);
        MessageCfg cfg = SDKUtil.gethistoryBody(date);
        AbilityResponse response = manager.post(methodUrl, null, JSON.toJSONString(cfg));
        try {
            String responseStr = response.getRespStr();
            ResponseBody resObj;
            try {
                resObj = JSON.parseObject(responseStr, ResponseBody.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                // 发送短信
                smsService.send("13231899751", date + " 查询消除告警 返回消息第一层json解析错误");
                return null;
            }
            String body = (String) resObj.getUNI_NET_BODY();
            body = body.replace("null", "\"\"");
            ResultInfo info;
            try {
                info = JSON.parseObject(body, ResultInfo.class);
            } catch (Exception e) {
                log.error("返回消息json解析错误: {}", responseStr);
                e.printStackTrace();
                // 发送短信
                smsService.send("13231899751", date + " 查询消除告警 返回消息第二层json解析错误");
                return null;
            }
            int rows = info.getTotal();
            log.info("消除告警返回数据{}条", rows);
            return info.getResult();
        } catch (Exception e) {
            log.error("查询消除告警出错: {}...", e.getMessage());
            e.printStackTrace();
            // 发送短信
            smsService.send("13231899751", date + " 查询消除告警出错" + e.getMessage());
            return null;
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
