package org.dromara.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.service.ISysConfigService;
import org.springframework.context.annotation.Import;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Import(cn.hutool.extra.spring.SpringUtil.class)
@Slf4j
public class MessageUtils {
    public static void sendLed(String devicecode,String message) throws UnsupportedEncodingException {
        if(StringUtils.isNotEmpty(devicecode)&&StringUtils.isNotEmpty(message)) {
            Map<String, String> data = new HashMap<>();
            data.put("devtype", "led");
            data.put("devcode", devicecode);
            final CamelDataUtil camelDataUtil = SpringUtil.getBean("camelDataUtil");
            camelDataUtil.SendData("direct:sendMessage", StringUtils.format("{\"val\":\"{}\",\"id\":\"{}\"}", message,devicecode), "mqheader", data);
        }else {
            log.error("------------------");
            log.error("devicecode:"+devicecode.toString()+"     message:"+message.toString());
            log.error("------------------");
        }

    }
    public static void sendTTs(String devicecode,String message){
        if(StringUtils.isNotEmpty(devicecode)&&StringUtils.isNotEmpty(message)) {
            Map<String, String> data = new HashMap<>();
            data.put("devtype", "tts");
            data.put("devcode", devicecode);
            int vol = 1;
            try {
                String vols = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.tts.vol");
                JSONObject volso =JSONUtil.parseObj(vols);
                int hour = DateUtil.date().hour(true);
                vol = volso.getInt(hour<10?"0"+hour:hour+"");
            }catch (Exception e){}

            final CamelDataUtil camelDataUtil = SpringUtil.getBean("camelDataUtil");
            camelDataUtil.SendData("direct:sendMessage", StringUtils.format("{\"val\":\"{}\",\"vol\":"+vol+",\"id\":\"{}\"}", message,devicecode), "mqheader", data);
        }else{
            log.error("------------------");
            log.error("devicecode:"+devicecode.toString()+"     message:"+message.toString());
            log.error("------------------");
        }
    }
}
