package com.ruoyi.common.core.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.ruoyi.common.config.properties.SmsProperties;
import com.ruoyi.common.core.domain.model.SmsResult;
import com.ruoyi.common.exception.sms.SmsException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云短信工具类工具类
 *
 * @author daixd
 **/
@Component
@Slf4j
public class AliyunSmsUtils {

    private static Client client;

    @Autowired
    private static SmsProperties properties;

    private static String orderCreateSuccessTemplateId = "SMS_475766020";
    private static String bcshOrderComplete = "SMS_475776102";

    private static String poOrderTodayStartService = "SMS_478300164";

    private static String bcshOrderBeginToday = "SMS_475865917";
    private static String bcshOrderBeginNextDay = "SMS_475736087";
    private static String bcshHasOrder = "SMS_475905981";
    private static String bcshJoinSuccess = "SMS_475796041";
    private static String bcshJoinFail = "SMS_475925946";
    private static String poOrderComplete = "SMS_475811073";
    private static String poStartService = "SMS_475920915";
    private static String poCreatePetAndService = "SMS_475980895";

    // 【猫妈狗爸】订单编号：【订单编号字段】长时间无人接单，请处理！
    private static String longTimeNoOrderAccepted = "SMS_488875138";

    @SneakyThrows(Exception.class)
    public AliyunSmsUtils(SmsProperties smsProperties) {
        if (client == null) {
            this.properties = smsProperties;
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(properties.getAccessKeyId())
                    // 您的AccessKey Secret
                    .setAccessKeySecret(properties.getAccessKeySecret())
                    // 访问的域名
                    .setEndpoint(properties.getEndpoint());
            this.client = new Client(config);
        }
    }

    public static SmsResult send(String phones, String templateId, Map<String, String> param) {
        log.info("开始发送短信，模版ID：【" + templateId + "】，接收手机号：" + phones + "，参数：" + JsonUtils.toJsonString(param));

        if (StringUtils.isBlank(phones)) {
            throw new SmsException("手机号不能为空");
        }
        if (StringUtils.isBlank(templateId)) {
            throw new SmsException("模板ID不能为空");
        }
        SendSmsRequest req = new SendSmsRequest()
                .setPhoneNumbers(phones)
                .setSignName(properties.getSignName())
                .setTemplateCode(templateId)
                .setTemplateParam(JsonUtils.toJsonString(param));
        try {
            SendSmsResponse resp = client.sendSms(req);
            log.info("发送短信成功，code【" + resp.getBody().getCode() + "】，发送结果：" + resp.getBody().getMessage());
            return SmsResult.builder()
                    .isSuccess("OK".equals(resp.getBody().getCode()))
                    .message(resp.getBody().getMessage())
                    .response(JsonUtils.toJsonString(resp))
                    .build();
        } catch (Exception e) {
            throw new SmsException(e.getMessage());
        }
    }

    public static SmsResult sendOrderSuccessSMS(String phones, String startTime, String endTime) {
        log.info("【短信发送】开始发送短信消费者：客户成功下单");
        Map<String, String> param = new HashMap<>();
        param.put("startTime", startTime);
        param.put("endTime", endTime);

        return send(phones, orderCreateSuccessTemplateId, param);
    }

    public static SmsResult sendPoCreatePetAndServiceSMS(String phones) {
        log.info("【短信发送】开始发送短信消费者：宠物档案和服务档案未创建提醒");
        Map<String, String> param = new HashMap<>();
        return send(phones, poCreatePetAndService, param);
    }

    public static SmsResult sendPoStartServiceSMS(String phones) {
        log.info("【短信发送】开始发送短信消费者：消费者-服务即将开始提醒");
        Map<String, String> param = new HashMap<>();
        return send(phones, poStartService, param);
    }

    public static SmsResult sendPoTodayStartServiceSMS(String phones) {
        log.info("【短信发送】开始发送短信消费者：消费者-服务即将开始提醒");
        Map<String, String> param = new HashMap<>();
        return send(phones, poOrderTodayStartService, param);
    }

    public static SmsResult sendPoOrderCompleteSMS(String phones, String amount, String level, String discount) {
        log.info("【短信发送】开始发送短信消费者：消费者-订单服务即将结束提醒");
        Map<String, String> param = new HashMap<>();
        param.put("amount", amount);
        param.put("level", level);
        param.put("discount", discount);

        return send(phones, poOrderComplete, param);
    }

    public static SmsResult sendBcshJoinFailSMS(String phones) {
        log.info("【短信发送】开始发送短信消费者：伴宠师提交审核信息未通过提醒");
        Map<String, String> param = new HashMap<>();
        return send(phones, bcshJoinFail, param);
    }

    public static SmsResult sendBcshJoinSuccessSMS(String phones) {
        log.info("【短信发送】开始发送短信消费者：伴宠师认证通过提醒");
        Map<String, String> param = new HashMap<>();
        return send(phones, bcshJoinSuccess, param);
    }

    public static SmsResult sendBcshHasOrderSMS(String phones) {
        try {
            log.info("【短信发送】开始发送短信消费者：伴宠师订单抢单通知");
            Map<String, String> param = new HashMap<>();
            return send(phones, bcshHasOrder, param);
        }catch (Exception e){
            log.error("短信发生失败：", e);
        }
        return null;
    }

    public static SmsResult sendBcshOrderBeginNextDaySMS(String phones, String address, String petName) {
        log.info("【短信发送】开始发送短信消费者：伴宠师订单服务开始通知");
        Map<String, String> param = new HashMap<>();
        param.put("address", address);
        param.put("petName", petName);

        return send(phones, bcshOrderBeginNextDay, param);
    }

    public static SmsResult sendBcshOrderBeginTodaySMS(String phones, String address, String petName) {
        log.info("【短信发送】开始发送短信消费者：伴宠师-订单服务开始通知");
        Map<String, String> param = new HashMap<>();
        param.put("address", address);
        param.put("petName", petName);

        return send(phones, bcshOrderBeginToday, param);
    }

    public static SmsResult sendBcshOrderCompleteSMS(String phones, String orderId, String amount) {
        log.info("【短信发送】开始发送短信消费者：伴宠师订单完成通知");
        Map<String, String> param = new HashMap<>();
        param.put("orderId", orderId);
        param.put("amount", amount);

        return send(phones, bcshOrderComplete, param);
    }

    public static SmsResult sendLongTimeNoOrderAccepted(String phones, String orderId) {
        try {
            log.info("【短信发送】通知管理员长时间无人接单，请处理！");
            Map<String, String> param = new HashMap<>();
            param.put("order_id", orderId);
            return send(phones, longTimeNoOrderAccepted, param);
        }catch (Exception e){
            log.error("短信发生失败：", e);
        }
        return null;
    }

    public static SmsResult sendOrderUpdate(String phones, String type, String orderId) {
//        try {
//            log.info("【短信发送】通知管理员长时间无人接单，请处理！");
//            Map<String, String> param = new HashMap<>();
////            param.put("type", type);//取消订单/修改订单
//            param.put("order_id", orderId);
//            return send(phones, longTimeNoOrderAccepted, param);
//        }catch (Exception e){
//            log.error("短信发生失败：", e);
//        }
        return null;
    }
}
