package com.xhpc.evcs.dto;

import com.fasterxml.jackson.annotation.*;
import com.xhpc.common.core.utils.DateUtils;
import com.xhpc.evcs.domain.XhpcStatisticsTimeInterval;
import com.xhpc.evcs.utils.DateUtil;
import com.xhpc.order.domain.XhpcHistoryOrder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.*;

import static com.xhpc.common.core.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;

//@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "StartChargeSeq",
        "ConnectorID",
        "StartTime",
        "EndTime",
        "TotalPower",
        "TotalElecMoney",
        "TotalSeviceMoney",
        "TotalMoney",
        "StopReason",
        "SumPeriod",
        "ChargeDetails"
})
@Setter
@Getter
public class ChargeOrderInfo {

    @Id
    @JsonProperty("StartChargeSeq")
    private String startChargeSeq;
    @JsonIgnore
    private Integer startChargeSeqStat;
    @JsonIgnore
    private Integer failReason;
    @JsonIgnore
    private String identCode;
    @JsonIgnore
    @Column(updatable = false)
    private String infraOperatorId;
    @JsonIgnore
    @Column(updatable = false)
    private String billerOperatorId;//biller
    @JsonProperty("ConnectorID")
    private String connectorID;
    @JsonProperty("StartTime")
    private String startTime;
    @JsonProperty("ChargeModel")
    private Integer chargeModel;
    @JsonProperty("Vin")
    private String vin;
    @JsonProperty("EndTime")
    private String endTime;
    @JsonProperty("TotalPower")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalPower = 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("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;
    @JsonIgnore
    @Transient
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public ChargeOrderInfo(XhpcHistoryOrder xhpcHistoryOrder, ChargeDetails[] calcemChargeDetails) {

        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.startChargeSeq = xhpcHistoryOrder.getInternetSerialNumber();
        this.endTime = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, xhpcHistoryOrder.getEndTime());
        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.getPowerPriceTotal().doubleValue();
        this.totalSeviceMoney = xhpcHistoryOrder.getServicePriceTotal().doubleValue();
        this.totalMoney = xhpcHistoryOrder.getTotalPrice().doubleValue();
        this.stopReason = xhpcHistoryOrder.getStopReasonEvcs();
        this.sumPeriod = xhpcHistoryOrder.getXhpcStatisticsTimeIntervalList().size();
        this.chargeDetails = calcemChargeDetails;//translate(xhpcHistoryOrder.getXhpcStatisticsTimeIntervalList());
        Date starttime = xhpcHistoryOrder.getStartTime();
        Date endtime = xhpcHistoryOrder.getEndTime();
        int cl = 0;
        if (starttime != null && endtime != null) {
            cl = Math.toIntExact((endtime.getTime() - starttime.getTime()) / 1000);
        }
    }

    public ChargeOrderInfo(XhpcHistoryOrder xhpcHistoryOrder) {

        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.startChargeSeq = xhpcHistoryOrder.getInternetSerialNumber();
        this.endTime = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, xhpcHistoryOrder.getEndTime());
        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.getPowerPriceTotal().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());
        Date starttime = xhpcHistoryOrder.getStartTime();
        Date endtime = xhpcHistoryOrder.getEndTime();
        int cl = 0;
        if (starttime != null && endtime != null) {
            cl = Math.toIntExact((endtime.getTime() - starttime.getTime()) / 1000);
        }
    }

    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 ChargeOrderInfo() {

    }

    @JsonProperty("StartChargeSeq")
    public String getStartChargeSeq() {

        return startChargeSeq;
    }

    @JsonProperty("StartChargeSeq")
    public void setStartChargeSeq(String startChargeSeq) {

        this.startChargeSeq = startChargeSeq;
    }

    public ChargeOrderInfo setStartChargeSeqAndReturn(String startChargeSeq) {

        this.startChargeSeq = startChargeSeq;
        return this;
    }

    @JsonProperty("ConnectorID")
    public String getConnectorID() {

        return connectorID;
    }

    @JsonProperty("ConnectorID")
    public void setConnectorID(String connectorID) {

        this.connectorID = connectorID;
    }

    @JsonProperty("StartTime")
    public String getStartTime() {

        return startTime;
    }

    @JsonProperty("StartTime")
    public void setStartTime(String startTime) {

        this.startTime = startTime;
    }

    @JsonProperty("EndTime")
    public String getEndTime() {

        return endTime;
    }

    @JsonProperty("EndTime")
    public void setEndTime(String endTime) {

        this.endTime = endTime;
    }

    @JsonProperty("TotalPower")
    public Double getTotalPower() {

        return totalPower;
    }

    @JsonProperty("TotalPower")
    public void setTotalPower(Double totalPower) {

        this.totalPower = totalPower;
    }

    @JsonProperty("TotalElecMoney")
    public Double getTotalElecMoney() {

        return totalElecMoney;
    }

    @JsonProperty("TotalElecMoney")
    public void setTotalElecMoney(Double totalElecMoney) {

        this.totalElecMoney = totalElecMoney;
    }

    @JsonProperty("TotalSeviceMoney")
    public Double getTotalSeviceMoney() {

        return totalSeviceMoney;
    }

    @JsonProperty("TotalSeviceMoney")
    public void setTotalSeviceMoney(Double totalSeviceMoney) {

        this.totalSeviceMoney = totalSeviceMoney;
    }

    @JsonProperty("TotalMoney")
    public Double getTotalMoney() {

        return totalMoney;
    }

    @JsonProperty("TotalMoney")
    public void setTotalMoney(Double totalMoney) {

        this.totalMoney = totalMoney;
    }

    @JsonProperty("StopReason")
    public Integer getStopReason() {

        return stopReason;
    }

    @JsonProperty("StopReason")
    public void setStopReason(Integer stopReason) {

        this.stopReason = stopReason;
    }

    @JsonProperty("SumPeriod")
    public Integer getSumPeriod() {

        return sumPeriod;
    }

    @JsonProperty("SumPeriod")
    public void setSumPeriod(Integer sumPeriod) {

        this.sumPeriod = sumPeriod;
    }

    @JsonAnyGetter
    @Transient
    public Map<String, Object> getAdditionalProperties() {

        return this.additionalProperties;
    }

    @JsonAnySetter
    @Transient
    public void setAdditionalProperty(String name, Object value) {

        this.additionalProperties.put(name, value);
    }

    @Override
    public String toString() {

        return new ToStringBuilder(this).append("startChargeSeq", startChargeSeq).append("connectorID", connectorID).append(
                "startTime", startTime).append("endTime", endTime).append("totalPower", totalPower).append("totalElecMoney",
                totalElecMoney).append("totalSeviceMoney", totalSeviceMoney).append("totalMoney", totalMoney).append(
                        "stopReason", stopReason).append("sumPeriod", sumPeriod).append("additionalProperties",
                additionalProperties).toString();
    }
}
