package com.usky.vpp.util;

import com.usky.vpp.domain.VppCustomer;
import com.usky.vpp.domain.VppCustomerContact;
import com.usky.vpp.domain.VppDrEvent;
import com.usky.vpp.domain.VppDrInvitation;

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;

/**
 * 邀约短信模板参数构建
 * <p>
 * 阿里云模板变量需与控制台一致：name、responsetype、time、number、operationtype
 * <pre>
 * 尊敬的${name}，您已通过电力需求响应审核。 响应类型：${responsetype} 响应时间：${time}
 * 邀约规模： ${number}kW 请在规定时段内${operationtype}
 * </pre>
 */
public final class VppSmsTemplateHelper {

    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private VppSmsTemplateHelper() {
    }

    public static String buildInvitationParam(VppDrInvitation invitation,
                                            VppDrEvent event,
                                            VppCustomer customer,
                                            VppCustomerContact contact) {
        String name = customer != null && customer.getCustomerName() != null
                ? customer.getCustomerName()
                : "";

        Integer responseType = invitation != null && invitation.getResponseType() != null
                ? invitation.getResponseType()
                : (event != null ? event.getResponseType() : null);
        String responsetype = VppCapabilityEvalHelper.responseTypeLabel(responseType);

        String time = buildResponseTime(invitation, event);

        BigDecimal capacity = invitation != null && invitation.getDemandCapacityKw() != null
                ? invitation.getDemandCapacityKw()
                : (event != null ? event.getTargetCapacityKw() : null);
        String number = formatNumber(capacity);

        Integer eventType = invitation != null && invitation.getTransactionType() != null
                ? invitation.getTransactionType()
                : (event != null ? event.getEventType() : null);
        String operationtype = buildOperationType(eventType);

        return "{\"name\":\"" + escapeJson(name)
                + "\",\"responsetype\":\"" + escapeJson(responsetype)
                + "\",\"time\":\"" + escapeJson(time)
                + "\",\"number\":\"" + escapeJson(number)
                + "\",\"operationtype\":\"" + escapeJson(operationtype) + "\"}";
    }

    private static String buildResponseTime(VppDrInvitation invitation, VppDrEvent event) {
        if (event != null && event.getStartTime() != null) {
            if (event.getEndTime() != null) {
                return TIME_FORMAT.format(event.getStartTime()) + "~" + TIME_FORMAT.format(event.getEndTime());
            }
            return TIME_FORMAT.format(event.getStartTime());
        }
        if (invitation != null && invitation.getExecuteStartDate() != null) {
            if (invitation.getExecuteEndDate() != null
                    && !invitation.getExecuteStartDate().equals(invitation.getExecuteEndDate())) {
                return invitation.getExecuteStartDate() + "~" + invitation.getExecuteEndDate();
            }
            return invitation.getExecuteStartDate().toString();
        }
        if (invitation != null && invitation.getReplyDeadline() != null) {
            return TIME_FORMAT.format(invitation.getReplyDeadline());
        }
        return "";
    }

    private static String buildOperationType(Integer eventType) {
        if (eventType == null) {
            return "完成响应";
        }
        if (eventType == 1) {
            return "参与削峰";
        }
        if (eventType == 2) {
            return "参与填谷";
        }
        return "完成响应";
    }

    private static String formatNumber(BigDecimal value) {
        if (value == null) {
            return "0";
        }
        return value.stripTrailingZeros().toPlainString();
    }

    private static String escapeJson(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("\\", "\\\\").replace("\"", "\\\"");
    }
}
