package com.xhpc.evcs.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.xhpc.evcs.domain.EtOrderMapping;
import com.xhpc.evcs.domain.XhpcStatisticsTimeInterval;
import com.xhpc.evcs.utils.DateUtil;
import com.xhpc.order.domain.XhpcHistoryOrder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.persistence.Column;
import javax.persistence.Id;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

//@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "StartChargeSeq",
        "ConnectorID",
        "StartTime",
        "EndTime",
        "ChargeModel",
        "TotalPower",
        "TotalElecMoney",
        "TotalSeviceMoney",
        "TotalMoney",
        "StopReason",
        "SumPeriod",
        "ChargeDetails",
        "UserName",
        "StationID",
        "EquipmentID",
        "ConnectorPower",
        "ChargeLast",
        "MeterValueStart",
        "MeterValueEnd"
})
public class CDChargeOrderInfo4BonusReq {

    @Id
    @JsonProperty("StartChargeSeq")
    private String startChargeSeq;
    @JsonProperty("ConnectorID")
    private String connectorID;
    @JsonProperty("StartTime")
    private String startTime;
    @JsonProperty("ChargeModel")
    private Integer chargeModel;
    @JsonProperty("ChargeLast")
    private Integer chargeLast;
    @JsonProperty("EndTime")
    private String endTime;
    @JsonProperty("UserName")
    private String userName;
    @JsonProperty("StationID")
    private String stationId;
    @JsonProperty("EquipmentID")
    private String equipmentId;
    @JsonProperty("TotalPower")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalPower = 0.0;
    @JsonProperty("MeterValueStart")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double meterValueStart = 0.0;
    @JsonProperty("MeterValueEnd")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double meterValueEnd = 0.0;
    @JsonProperty("TotalElecMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalElecMoney = 0.0;
    @JsonProperty("TotalSeviceMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalSeviceMoney = 0.0;
    @JsonProperty("ConnectorPower")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double connectorPower = 0.0;
    @JsonProperty("TotalMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalMoney = 0.0;
    @JsonProperty("StopReason")
    private Integer stopReason;
    @JsonProperty("SumPeriod")
    private Integer sumPeriod;
    @JsonProperty("ChargeDetails")
    private ChargeDetails[] chargeDetails;

    public CDChargeOrderInfo4BonusReq(XhpcHistoryOrder xhpcHistoryOrder, EtOrderMapping etOrderMapping) {
        this.startChargeSeq = etOrderMapping.getEvcsOrderNo();
        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.endTime = DateUtil.date2String(xhpcHistoryOrder.getEndTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.startTime = DateUtil.date2String(xhpcHistoryOrder.getStartTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.chargeModel = xhpcHistoryOrder.getChargeModelEvcs();
        this.totalPower = xhpcHistoryOrder.getTotalPower();
        this.totalElecMoney = xhpcHistoryOrder.getTotalPrice().doubleValue();
        this.totalSeviceMoney = xhpcHistoryOrder.getServicePriceTotal().doubleValue();
        this.totalMoney = xhpcHistoryOrder.getTotalPrice().doubleValue();
        if(xhpcHistoryOrder.getStopReasonEvcs() !=null){
            String stopReasonEvcs = xhpcHistoryOrder.getStopReasonEvcs();
            if("40".equals(stopReasonEvcs)||"0".equals(stopReasonEvcs)||"00".equals(stopReasonEvcs)||"45".equals(stopReasonEvcs)||"APP 远程停止".equals(stopReasonEvcs)
                    ||"72".equals(stopReasonEvcs)){
                this.stopReason =0;
            }else if("41".equals(stopReasonEvcs) ||"42".equals(stopReasonEvcs)||"43".equals(stopReasonEvcs)||"44".equals(stopReasonEvcs)||"4E".equals(stopReasonEvcs)||"平台停止".equals(stopReasonEvcs)){
                this.stopReason =1;
            }else if("4A".equals(stopReasonEvcs) ||"4B".equals(stopReasonEvcs)||"4C".equals(stopReasonEvcs)||"4D".equals(stopReasonEvcs)||"4F".equals(stopReasonEvcs) ||
                    "55".equals(stopReasonEvcs)||"57".equals(stopReasonEvcs)||"62".equals(stopReasonEvcs)||"63".equals(stopReasonEvcs)||"74".equals(stopReasonEvcs)||"75".equals(stopReasonEvcs)
                    ||"78".equals(stopReasonEvcs)||"79".equals(stopReasonEvcs)||"7A".equals(stopReasonEvcs)||"7B".equals(stopReasonEvcs)||"7C".equals(stopReasonEvcs)||"7D".equals(stopReasonEvcs)
                    ||"7E".equals(stopReasonEvcs)||"7F".equals(stopReasonEvcs)||"83".equals(stopReasonEvcs)){
                this.stopReason =3;
            }else if("5A".equals(stopReasonEvcs)||"82".equals(stopReasonEvcs)){
                this.stopReason =2;
            }else if("6B".equals(stopReasonEvcs) ||"6D".equals(stopReasonEvcs)||"5E".equals(stopReasonEvcs)){
                this.stopReason =4;
            }else{
                this.stopReason =0;
            }
        }else{
            this.stopReason =0;
        }
        this.sumPeriod = xhpcHistoryOrder.getXhpcStatisticsTimeIntervalList().size();
        this.chargeDetails = translate(xhpcHistoryOrder.getXhpcStatisticsTimeIntervalList());
        this.userName = xhpcHistoryOrder.getUserNameEvcs();
        this.stationId = xhpcHistoryOrder.getChargingStationId().toString();
        this.equipmentId = xhpcHistoryOrder.getSerialNumber().substring(0, 14);
        this.connectorPower = xhpcHistoryOrder.getConnectorPowerEvcs();
        Date starttime = xhpcHistoryOrder.getStartTime();
        Date endtime = xhpcHistoryOrder.getEndTime();
        int cl = 0;
        if (starttime != null && endtime != null) {
            cl = Math.toIntExact((endtime.getTime() - starttime.getTime()) / 1000);
        }
        this.chargeLast = Math.abs(cl);
        if(xhpcHistoryOrder.getMeterValueStartEvcs() !=null && xhpcHistoryOrder.getMeterValueStartEvcs()>0){
            double v = xhpcHistoryOrder.getMeterValueStartEvcs() / 10000;
            DecimalFormat df = new DecimalFormat("#.000");
            this.meterValueStart =Double.parseDouble(df.format(v));
        }else{
            this.meterValueStart = xhpcHistoryOrder.getMeterValueStartEvcs();
        }
       if(xhpcHistoryOrder.getMeterValueEndEvcs() !=null && xhpcHistoryOrder.getMeterValueEndEvcs()>0){
           double v = xhpcHistoryOrder.getMeterValueEndEvcs() / 10000;
           DecimalFormat df = new DecimalFormat("#.000");
           this.meterValueEnd =Double.parseDouble(df.format(v));
       } else{
           this.meterValueEnd = xhpcHistoryOrder.getMeterValueEndEvcs();
       }

    }

    private ChargeDetails[] translate(List<XhpcStatisticsTimeInterval> xhpcStatisticsTimeIntervalList) {

        List<ChargeDetails> details = new LinkedList<>();
        for (XhpcStatisticsTimeInterval statistics : xhpcStatisticsTimeIntervalList) {
            details.add(new ChargeDetails(statistics));
        }
        ChargeDetails[] a = new ChargeDetails[details.size()];
        return details.toArray(a);
    }

    public CDChargeOrderInfo4BonusReq() {

        super();
    }

    @Override
    public String toString() {

        return new ToStringBuilder(this)
                .append("startChargeSeq", startChargeSeq)
                .append("connectorID", connectorID)
                .append("startTime", startTime)
                .append("chargeModel", chargeModel)
                .append("chargeLast", chargeLast)
                .append("endTime", endTime)
                .append("userName", userName)
                .append("stationId", stationId)
                .append("equipmentId", equipmentId)
                .append("totalPower", totalPower)
                .append("meterValueStart", meterValueStart)
                .append("meterValueEnd", meterValueEnd)
                .append("totalElecMoney", totalElecMoney)
                .append("totalSeviceMoney", totalSeviceMoney)
                .append("connectorPower", connectorPower)
                .append("totalMoney", totalMoney)
                .append("stopReason", stopReason)
                .append("sumPeriod", sumPeriod)
                .append("chargeDetails", chargeDetails)
                .toString();
    }

    public String getStartChargeSeq() {
        return startChargeSeq;
    }
}
