package cz.senslog.connector.fetch.fofr;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@JacksonXmlRootElement(localName = "trackandtrace")
public class ParcelTrackingStatus implements Serializable {

    private static final long serialVersionUID = 1L;

    public static class Fofr {

        @JacksonXmlProperty(isAttribute = true, localName = "ID")
        private String id;
        @JacksonXmlProperty(isAttribute = true, localName = "CentreOrigin")
        private String warehouseOrigin;
        @JacksonXmlProperty(isAttribute = true, localName = "CentreFrom")
        private String CentreFrom;
        @JacksonXmlProperty(isAttribute = true, localName = "CentreTo")
        private String warehouseDestination;
        @JacksonXmlProperty(isAttribute = true, localName = "Code")
        private String Code;
        @JacksonXmlProperty(isAttribute = true, localName = "Colli")
        private String Colli;
        @JacksonXmlProperty(isAttribute = true, localName = "Weight")
        private String Weight;
        @JacksonXmlProperty(isAttribute = true, localName = "Volume")
        private String Volume;

        @JacksonXmlElementWrapper(localName = "Statuses")
        @JacksonXmlProperty(localName = "Status")
        List<Status> statuses;
    }

    public static class Status {
        @JacksonXmlProperty(isAttribute = true, localName = "StCode")
        private String code;

        @JacksonXmlProperty(isAttribute = true, localName = "StDate")
        private String timestamp;

        @JacksonXmlProperty(isAttribute = true, localName = "Depo")
        private String warehouseCode;

        @JacksonXmlProperty(isAttribute = true, localName = "Kontakt")
        private String contact;

        @JacksonXmlProperty(isAttribute = true, localName = "StInfo")
        private String info;

        public int getCode() {
            return Integer.parseInt(code);
        }

        public LocalDateTime getTimestamp() {
            return LocalDateTime.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-M-d H:m"));
        }

        public String getWarehouseCode() {
            return warehouseCode;
        }
    }

    @JacksonXmlElementWrapper(localName = "FOFR")
    private Fofr parcelInfo;

    public long getId() {
        return Long.parseLong(parcelInfo.id);
    }

    public String getWarehouseOrigin() {
        return parcelInfo.warehouseOrigin;
    }

    public String getWarehouseDestination() {
        return parcelInfo.warehouseDestination;
    }

    public List<Status> getStatuses() {
        return parcelInfo.statuses;
    }
}
