package com.sckw.slope.detection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sckw.core.model.enums.AlarmTitleEnum;
import com.sckw.core.web.response.HttpResult;
import com.sckw.slope.detection.common.config.SmsTemplateConfig;
import com.sckw.slope.detection.model.param.SmsMessageParam;
import com.sckw.slope.detection.model.vo.QueryDictTypePageReqVo;
import com.sckw.slope.detection.service.CommonService;
import com.sckw.slope.detection.service.api.DetectionApiService;
import com.sckw.slope.detection.utils.MailUtil;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfdc
 * @description 公共字典查询
 * @date 2023-11-02 17:11:09
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    CommonService commonService;

    @RequestMapping(value = "/getDict", method = RequestMethod.POST)
    public HttpResult getDict(@RequestBody @Valid QueryDictTypePageReqVo queryDictTypePageReqVo) {
        return HttpResult.ok(commonService.getDict(queryDictTypePageReqVo));
    }


    @Autowired
    SmsTemplateConfig smsTemplateConfig;

    //
//    @RequestMapping(value = "/pushSmsMessageByDataAlarm",method = RequestMethod.GET)
//    public HttpResult pushSmsMessageByDataAlarm(){
//
//        String phones = "18215677925,18215677925";
//        List<String> pushToPhone = com.sckw.core.utils.StringUtils.splitStrToList(phones, String.class);
//        Long ts=1700970600L;
//        LocalDateTime localDateTime = Instant.ofEpochSecond(ts).atZone(ZoneId.systemDefault()).toLocalDateTime();
//        String time = DateUtil.format(localDateTime, "yyyy-MM-dd HH:mm:ss");
//        Map<String, String> templateParam = new HashMap<>();
//        templateParam.put("time", time);
//        templateParam.put("level", "1");
//        templateParam.put("name", "device.getName()");
//        templateParam.put("value", "itemValueAndUnit");
//        commonService.pushSmsMessage(pushToPhone
//                , smsTemplateConfig.getDataAlarm().getSmsCode()
//                , smsTemplateConfig.getDataAlarm().getSignName()
//                , templateParam);
//        return  HttpResult.ok();
//    }
//
//    @RequestMapping(value = "/pushSmsMessageByDeviceAlarm",method = RequestMethod.GET)
//    public HttpResult pushSmsMessageByDeviceAlarm(){
//        String phones = "18215677925,18215677925";
//        List<String> pushToPhone = com.sckw.core.utils.StringUtils.splitStrToList(phones, String.class);
//        Long ts=1700970600L;
//        LocalDateTime localDateTime = Instant.ofEpochSecond(ts).atZone(ZoneId.systemDefault()).toLocalDateTime();
//        String time = DateUtil.format(localDateTime, "yyyy-MM-dd HH:mm:ss");
//        Map<String, String> templateParam = new HashMap<>();
//        templateParam.put("time", time);
//        templateParam.put("level", "1");
//        templateParam.put("name", "device.getName()");
//        templateParam.put("value", "itemValueAndUnit");
//        commonService.pushSmsMessage(pushToPhone
//                , smsTemplateConfig.getDeviceAlarm().getSmsCode()
//                , smsTemplateConfig.getDeviceAlarm().getSignName()
//                , templateParam);
//        return  HttpResult.ok();
//    }
//    @RequestMapping(value = "/pushEmailMessage", method = RequestMethod.GET)
//    public HttpResult pushEmailMessage() {
//        String key = PasswordUtils.md5("topic" + "alarm" + "payload");
//        String alarmKey = String.format(RedisConstant.MQ_CONSUMER_ALARM_KEY, key);
//        boolean flag = RedissonUtils.tryLock(alarmKey, 3L, 5L);
//        System.out.println("flag"+flag);
//        Map<String, Object> templateParam = new HashMap<>();
//        templateParam.put("company", smsTemplateConfig.getDataAlarm().getSignName());
//        templateParam.put("level", "一级");
//        templateParam.put("name", "设备名称");
//        templateParam.put("time", "2023-12-05 15:00:00");
//        templateParam.put("project", "第一个项目");
//        templateParam.put("value", "29.5645E");
//        List<String> emailAddress = new ArrayList<>();
////        emailAddress.add("1573035851@qq.com");
//        emailAddress.add("lengfaqiang96@163.com");
////        commonService.pushEmailMessage(emailAddress
////                , templateParam);
//        return HttpResult.ok();
//    }

    @Value("${sms.url}")
    private String smsUrl;

    @Autowired
    DetectionApiService detectionApiService;

    @RequestMapping(value = "/pushEmailMessageDemo", method = RequestMethod.GET)
    public HttpResult pushEmailMessageDemo() throws IOException {
        Map<String, String> templateParam = new HashMap<>();
        templateParam.put("company", smsTemplateConfig.getDataAlarm().getSignName());
        templateParam.put("level", "一级");
        templateParam.put("name", "设备名称");
        templateParam.put("time", "2023-12-05 15:00:00");
        templateParam.put("project", "第一个项目");
//                            templateParam.put("value", itemValue);
        templateParam.put("value", "29.5645E");
        templateParam.put("element", "element");
        templateParam.put("type", AlarmTitleEnum.ALARM_TITLE_TWO.getName() + "告警");
        String path = CommonController.class.getResource("/template/Email.html").getPath();
        String htmlContent = MailUtil.readResourceFile(path);
        // 创建一个Map来存储要替换的值
        // 替换HTML中的占位符
        String replacedHtml = MailUtil.replacePlaceholders(htmlContent, templateParam);
        List<String> emailAddress = new ArrayList<>();
        emailAddress.add("1573035851@qq.com");
        SmsMessageParam param = new SmsMessageParam();
        param.setCode("");
        param.setType(4);
        param.setPushTo(emailAddress);
        SmsMessageParam.Content content = new SmsMessageParam.Content();
        content.setSubject("设备告警通知邮件");
        content.setText(replacedHtml);
        content.setComplexFlag(true);
        param.setContent(content);
        JSONObject jsonObject = detectionApiService.pushEmailMessage(smsUrl, JSONObject.toJSONString(param));
        return HttpResult.ok();
    }
}
