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.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();
        this.stopReason = xhpcHistoryOrder.getStopReasonEvcs();
        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 = cl;
        this.meterValueStart = xhpcHistoryOrder.getMeterValueStartEvcs();
        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();
    }

}
