package it.integry.integrywmsnative.gest.vendita.dto;

import android.os.Parcel;
import android.os.Parcelable;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.integry.integrywmsnative.core.REST.model.Ean128Model;
import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.core.utility.UtilityString;

public class PickingObjectDTO implements Parcelable {

    private Integer numCollo;
    private String gestione;
    private String serCollo;
    private String descrizioneEstesa;
    private String codJcom;
    private String codMart;
    private String posizione;
    private BigDecimal qtaCollo;
    private String dataCollo;
    private Integer idViaggio;
    private String codTagl;
    private String codCol;
    private String dataScad;
    private String codArtFor;
    private String partitaMag;
    private BigDecimal qtaOrd;
    private BigDecimal numCnfCollo;
    private BigDecimal numCnf;
    private String codAlis;
    private BigDecimal colliPedana;

    private MtbAart mtbAart;

    private Boolean hidden = null;
    private Boolean tempHidden = null;
    private PickData tempPickData = null;

    private List<MtbColr> withdrawRows = new ArrayList<>();

    protected PickingObjectDTO(Parcel in) {
        if (in.readByte() == 0) {
            numCollo = null;
        } else {
            numCollo = in.readInt();
        }
        gestione = in.readString();
        serCollo = in.readString();
        descrizioneEstesa = in.readString();
        codJcom = in.readString();
        codMart = in.readString();
        posizione = in.readString();
        dataCollo = in.readString();
        if (in.readByte() == 0) {
            idViaggio = null;
        } else {
            idViaggio = in.readInt();
        }
        codTagl = in.readString();
        codCol = in.readString();
        dataScad = in.readString();
        codArtFor = in.readString();
        partitaMag = in.readString();
        codAlis = in.readString();
        hidden = in.readByte() == 0x00 ? null : in.readByte() != 0x00;

        if (in.readByte() == 0) {
            qtaCollo = null;
        } else {
            qtaCollo = new BigDecimal(in.readFloat());
        }
        if (in.readByte() == 0) {
            qtaOrd = null;
        } else {
            qtaOrd = new BigDecimal(in.readFloat());
        }
        if (in.readByte() == 0) {
            numCnf = null;
        } else {
            numCnf = new BigDecimal(in.readFloat());
        }
        if (in.readByte() == 0) {
            numCnfCollo = null;
        } else {
            numCnfCollo = new BigDecimal(in.readFloat());
        }


        mtbAart = (MtbAart) in.readValue(MtbAart.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (numCollo == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(numCollo);
        }
        dest.writeString(gestione);
        dest.writeString(serCollo);
        dest.writeString(descrizioneEstesa);
        dest.writeString(codJcom);
        dest.writeString(codMart);
        dest.writeString(posizione);
        dest.writeString(dataCollo);
        if (idViaggio == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(idViaggio);
        }
        dest.writeString(codTagl);
        dest.writeString(codCol);
        dest.writeString(dataScad);
        dest.writeString(codArtFor);
        dest.writeString(partitaMag);
        dest.writeString(codAlis);
        if (hidden == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeByte((byte) (hidden ? 0x01 : 0x00));
        }

        if (qtaCollo == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeFloat(qtaCollo.floatValue());
        }
        if (qtaOrd == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeFloat(qtaOrd.floatValue());
        }
        if (numCnf == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeFloat(numCnf.floatValue());
        }
        if (numCnfCollo == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeFloat(numCnfCollo.floatValue());
        }

        dest.writeValue(mtbAart);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<PickingObjectDTO> CREATOR = new Creator<PickingObjectDTO>() {
        @Override
        public PickingObjectDTO createFromParcel(Parcel in) {
            return new PickingObjectDTO(in);
        }

        @Override
        public PickingObjectDTO[] newArray(int size) {
            return new PickingObjectDTO[size];
        }
    };

    public Integer getNumCollo() {
        return numCollo;
    }

    public PickingObjectDTO setNumCollo(Integer numCollo) {
        this.numCollo = numCollo;
        return this;
    }

    public String getGestione() {
        return gestione;
    }

    public PickingObjectDTO setGestione(String gestione) {
        this.gestione = gestione;
        return this;
    }

    public String getSerCollo() {
        return serCollo;
    }

    public PickingObjectDTO setSerCollo(String serCollo) {
        this.serCollo = serCollo;
        return this;
    }

    public String getDescrizioneEstesa() {
        return descrizioneEstesa;
    }

    public PickingObjectDTO setDescrizioneEstesa(String descrizioneEstesa) {
        this.descrizioneEstesa = descrizioneEstesa;
        return this;
    }

    public String getCodJcom() {
        return codJcom;
    }

    public PickingObjectDTO setCodJcom(String codJcom) {
        this.codJcom = codJcom;
        return this;
    }

    public String getCodMart() {
        return codMart;
    }

    public PickingObjectDTO setCodMart(String codMart) {
        this.codMart = codMart;
        return this;
    }

    public String getPosizione() {
        return posizione;
    }

    public PickingObjectDTO setPosizione(String posizione) {
        this.posizione = posizione;
        return this;
    }

    public BigDecimal getQtaCollo() {
        return qtaCollo;
    }

    public PickingObjectDTO setQtaCollo(BigDecimal qtaCollo) {
        this.qtaCollo = qtaCollo;
        return this;
    }

    public String getDataColloS() {
        return dataCollo;
    }

    public Date getDataColloD() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        try {
            return sdf.parse(getDataColloS());
        } catch (ParseException e) {
            UtilityLogger.errorMe(e);
            return null;
        }
    }

    public String getDataColloHuman() {
        Date dataColloD = null;
        try {
            dataColloD = getDataColloD();
        } catch (Exception ex){
            UtilityLogger.errorMe(ex);
        }

        if(dataColloD != null){
            return UtilityDate.formatDate(dataColloD, UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN);
        } else return null;
    }

    public PickingObjectDTO setDataCollo(String dataCollo) {
        this.dataCollo = dataCollo;
        return this;
    }

    public Integer getIdViaggio() {
        return idViaggio;
    }

    public PickingObjectDTO setIdViaggio(Integer idViaggio) {
        this.idViaggio = idViaggio;
        return this;
    }

    public String getCodTagl() {
        return codTagl;
    }

    public PickingObjectDTO setCodTagl(String codTagl) {
        this.codTagl = codTagl;
        return this;
    }

    public String getCodCol() {
        return codCol;
    }

    public PickingObjectDTO setCodCol(String codCol) {
        this.codCol = codCol;
        return this;
    }

    public String getDataScadS() {
        return dataScad;
    }

    public Date getDataScadD() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        return sdf.parse(getDataScadS());
    }

    public PickingObjectDTO setDataScad(String dataScad) {
        this.dataScad = dataScad;
        return this;
    }

    public String getCodArtFor() {
        return codArtFor;
    }

    public PickingObjectDTO setCodArtFor(String codArtFor) {
        this.codArtFor = codArtFor;
        return this;
    }

    public String getPartitaMag() {
        return partitaMag;
    }

    public PickingObjectDTO setPartitaMag(String partitaMag) {
        this.partitaMag = partitaMag;
        return this;
    }

    public BigDecimal getQtaOrd() {
        return qtaOrd;
    }

    public PickingObjectDTO setQtaOrd(BigDecimal qtaOrd) {
        this.qtaOrd = qtaOrd;
        return this;
    }

    public BigDecimal getNumCnfCollo() {
        return numCnfCollo;
    }

    public PickingObjectDTO setNumCnfCollo(BigDecimal numCnfCollo) {
        this.numCnfCollo = numCnfCollo;
        return this;
    }

    public BigDecimal getNumCnf() {
        return numCnf;
    }

    public PickingObjectDTO setNumCnf(BigDecimal numCnf) {
        this.numCnf = numCnf;
        return this;
    }

    public String getCodAlis() {
        return codAlis;
    }

    public PickingObjectDTO setCodAlis(String codAlis) {
        this.codAlis = codAlis;
        return this;
    }

    public BigDecimal getColliPedana() {
        return colliPedana;
    }

    public PickingObjectDTO setColliPedana(BigDecimal colliPedana) {
        this.colliPedana = colliPedana;
        return this;
    }

    public MtbAart getMtbAart() {
        return mtbAart;
    }

    public PickingObjectDTO setMtbAart(MtbAart mtbAart) {
        this.mtbAart = mtbAart;
        return this;
    }

    public Boolean isHidden() {
        return hidden;
    }

    public PickingObjectDTO setHidden(boolean hidden) {
        this.hidden = hidden;
        return this;
    }

    public Boolean isTempHidden() {
        return tempHidden;
    }

    public PickingObjectDTO setTempHidden(Boolean tempHidden) {
        this.tempHidden = tempHidden;
        return this;
    }

    public PickData getTempPickData() {
        return tempPickData;
    }

    public PickingObjectDTO setTempPickData(PickData tempPickData) {
        this.tempPickData = tempPickData;
        return this;
    }

    public List<MtbColr> getWithdrawRows() {
        return withdrawRows;
    }

    public PickingObjectDTO setWithdrawRows(List<MtbColr> withdrawRows) {
        this.withdrawRows = withdrawRows;
        return this;
    }

    public static class PickData {
        private String batchLot;
        private BigDecimal qtaTot;
        private BigDecimal qtaCnf;
        private BigDecimal numCnf;
        private Date dataScad;
        private MtbColt sourceMtbColt;


        public String getBatchLot() {
            return batchLot;
        }

        public PickData setBatchLot(String batchLot) {
            this.batchLot = batchLot;
            return this;
        }

        public BigDecimal getQtaTot() {
            return qtaTot;
        }

        public PickData setQtaTot(BigDecimal qtaTot) {
            this.qtaTot = qtaTot;
            return this;
        }

        public BigDecimal getQtaCnf() {
            return qtaCnf;
        }

        public PickData setQtaCnf(BigDecimal qtaCnf) {
            this.qtaCnf = qtaCnf;
            return this;
        }

        public BigDecimal getNumCnf() {
            return numCnf;
        }

        public PickData setNumCnf(BigDecimal numCnf) {
            this.numCnf = numCnf;
            return this;
        }

        public Date getDataScad() {
            return dataScad;
        }

        public PickData setDataScad(Date dataScad) {
            this.dataScad = dataScad;
            return this;
        }

        public MtbColt getSourceMtbColt() {
            return sourceMtbColt;
        }

        public PickData setSourceMtbColt(MtbColt sourceMtbColt) {
            this.sourceMtbColt = sourceMtbColt;
            return this;
        }

        public static PickData fromEan128(Ean128Model ean128Model) {
            PickingObjectDTO.PickData pickData = null;

            if(ean128Model != null){
                pickData = new PickingObjectDTO.PickData();

                if(!UtilityString.isNullOrEmpty(ean128Model.BatchLot)) {
                    pickData.setBatchLot(ean128Model.BatchLot);
                }

                try {
                    if(!UtilityString.isNullOrEmpty(ean128Model.BestBefore)){
                        pickData.setDataScad(UtilityDate.recognizeDate(ean128Model.BestBefore));
                    } else if(!UtilityString.isNullOrEmpty(ean128Model.Expiry)) {
                        pickData.setDataScad(UtilityDate.recognizeDate(ean128Model.Expiry));
                    }

                } catch (Exception e) {
                    UtilityLogger.errorMe(e);
                }


                if(ean128Model.Count != null && ean128Model.Count > 0) {
                    //if(!UtilityString.isNullOrEmpty(ean128Model.Sscc)) {
                    pickData.setNumCnf(new BigDecimal(ean128Model.Count));
                    //} else {
                    //    dto.setQtaTot(new BigDecimal(ean128Model.Count));
                    //}
                }

                if (ean128Model.NetWeightKg != null && ean128Model.NetWeightKg > 0) {
                    pickData.setQtaTot(new BigDecimal(ean128Model.NetWeightKg));
                }

                if(pickData.getQtaTot() != null && pickData.getQtaTot().floatValue() > 0 && pickData.getNumCnf() != null && pickData.getNumCnf().floatValue() > 0) {
                    pickData.setQtaCnf(pickData.getQtaTot().divide(pickData.getNumCnf()));
                }

            }

            return pickData;
        }
    }
}
