package vip.xiaonuo.coldchain.core.alarm.service.messagepush.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import vip.xiaonuo.coldchain.core.alarm.bean.SensorAlarm;
import vip.xiaonuo.coldchain.core.alarm.bean.SensorAlarmUser;
import vip.xiaonuo.coldchain.core.alarm.service.messagepush.MessagePushService;
import vip.xiaonuo.coldchain.modular.push.param.AlarmPushParam;
import vip.xiaonuo.coldchain.modular.push.param.AlarmRecoverPushParam;
import vip.xiaonuo.coldchain.modular.push.param.OfflinePushParam;
import vip.xiaonuo.coldchain.modular.push.param.OnlinePushParam;
import vip.xiaonuo.coldchain.modular.push.utils.PushUtil;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 微信公众号通知推送服务
 *
 * @author jackzhou
 */
@Service("WECHAT")
public class WechatMessagePushService implements MessagePushService {

    @Override
    public boolean sendAlarmMessage(SensorAlarm alarm/*, SensorAlarmUser user*/) {
        AlarmPushParam pushParam = null;
        if (alarm.getValue() == 0 || alarm.getThreshold() == 0) {
            pushParam = new AlarmPushParam();
            pushParam.setValue("Device Offline or Power Failure");
        } else {
            //数据异常
            pushParam = new AlarmPushParam(alarm.getValue() + "/" + alarm.getThreshold());
        }
        if (alarm.getMessage().length() > 17) {
            pushParam.setContext(alarm.getMessage().substring(0, 17) + "...");
        } else {
            pushParam.setContext(alarm.getMessage());
        }
        // 添加房间名称
        if (StrUtil.isNotBlank(alarm.getRoomName())) {
            pushParam.setDeviceName(alarm.getSource() + "(" + alarm.getRoomName() + ")");
        } else {
            pushParam.setDeviceName(alarm.getSource());
        }
        pushParam.setDeviceName(alarm.getSource());
        pushParam.setNoticeTime(new Date());
        pushParam.setType(alarm.getAlarmType());
        pushParam.setUserIdList(alarm.getAlarmUsers().stream().map(SensorAlarmUser::getOpenId).collect(Collectors.toSet()));
        //推送到公众号
        String pushCode = PushUtil.alarmPush(pushParam);
        alarm.setWeixinRequestCode(pushCode);
        return !StrUtil.isBlank(pushCode);
    }

    @Override
    public boolean sendAlarmRecoverMessage(SensorAlarm alarm, Long firstAlarmTime) {
        AlarmRecoverPushParam pushParam = new AlarmRecoverPushParam();
        pushParam.setDeviceName(alarm.getSource());
        pushParam.setRecoverTime(new Date());
        pushParam.setType(alarm.getAlarmType());
        pushParam.setValue(alarm.getValue() + "/" + alarm.getThreshold());
        Date date = new Date(firstAlarmTime);
        pushParam.setFirstAlarmTime(date);
        pushParam.setUserIdList(alarm.getAlarmUsers().stream().map(SensorAlarmUser::getOpenId).collect(Collectors.toSet()));
        if (alarm.getMessage().length() > 17) {
            pushParam.setRecoverCause(alarm.getMessage().substring(0, 17) + "...");
        } else {
            pushParam.setRecoverCause(alarm.getMessage());
        }
        if (StrUtil.isNotBlank(alarm.getRoomName())) {
            pushParam.setDeviceName(alarm.getSource());
        } else {
            pushParam.setDeviceName(alarm.getSource());
        }
        //推送到公众号
        String pushCode = PushUtil.alarmRecoverPush(pushParam);
        alarm.setWeixinRequestCode(pushCode);
        return !StrUtil.isBlank(pushCode);
    }

    @Override
    public boolean sendOfflineMessage(SensorAlarm alarm) {
        OfflinePushParam pushParam = new OfflinePushParam();
        pushParam.setOfflineTime(new Date());
        pushParam.setDeviceName(alarm.getSource());
        pushParam.setStatus("离线");
        pushParam.setUserIdList(alarm.getAlarmUsers().stream().map(SensorAlarmUser::getOpenId).collect(Collectors.toSet()));
        if (StrUtil.isNotBlank(alarm.getRoomName())) {
            pushParam.setLocation(alarm.getRoomName());
        }
        String pushCode = PushUtil.offlinePush(pushParam);
        alarm.setWeixinRequestCode(pushCode);
        return !StrUtil.isBlank(pushCode);
    }

    @Override
    public boolean sendOnlineMessage(SensorAlarm alarm, Long firstAlarmTime) {
        OnlinePushParam pushParam = new OnlinePushParam();
        pushParam.setOnlineTime(new Date());
        pushParam.setDeviceName(alarm.getSource());
        pushParam.setStatus("上线");
        Date date = new Date(firstAlarmTime);
        pushParam.setFirstAlarmTime(date);
        pushParam.setUserIdList(alarm.getAlarmUsers().stream().map(SensorAlarmUser::getOpenId).collect(Collectors.toSet()));
        if (StrUtil.isNotBlank(alarm.getRoomName())) {
            pushParam.setLocation(alarm.getRoomName());
        }
        String pushCode = PushUtil.onlinePush(pushParam);
        alarm.setWeixinRequestCode(pushCode);
        return !StrUtil.isBlank(pushCode);
    }
}
