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

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

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

import it.integry.integrywmsnative.core.model.MtbAart;
import it.integry.integrywmsnative.core.model.MtbColt;

/**
 * Created by GiuseppeS on 06/03/2018.
 */

public class OrdineAccettazioneDTO implements Parcelable {

    public String ragSoc;
    public int numero;
    public String data;
    public String gestione;
    public String codMdep;
    public String codAnag;
    public String codVdes;
    public String termCons;
    public ArrayList<Riga> ordini;
    public String codAlis;
    public String rifOrd;


    public static class Riga implements Parcelable {
        public Float qtaOrd;            //QTA ORDINATA (tutta la qta ordiata)
        public Float qtaRiservate;      //QTA COLLI (tutta la qta già accantonata nei colli
        public Float qtaEvasa;          //QTA EVASA ORDINE (qta con documento)
        //public Float qtaDaEvadere;      //qta_ord - qta_evasa - qta_colli
        public int rigaOrd;
        public String codJcom;
        public String ragSocCom;
        public String descrizioneCommessa;
        public MtbAart mtbAart;
        public PickingObjectDTO[] colliAssociati;
        public Boolean hidden = null;
        public Boolean tempHidden = null;
        public String partitaMag;
        public String codArtFor;
        public String descrizioneEstesa;
        public String codAlis;

        public int numOrd;
        public String dataOrd;
        public String gestioneOrd;

        public String dataCons;

        public BigDecimal getQtaDaEvadere() {
            return this.getQtaOrd().subtract(this.getQtaEvasa()).subtract(this.getQtaRiservate());
        }


        public BigDecimal getQtaOrd() {
            return qtaOrd != null ? new BigDecimal(qtaOrd) : BigDecimal.ZERO;
        }

        public BigDecimal getQtaRiservate() {
            return qtaRiservate != null ? new BigDecimal(qtaRiservate) : BigDecimal.ZERO;
        }

        public BigDecimal getQtaEvasa() {
            return qtaEvasa != null ? new BigDecimal(qtaEvasa) : BigDecimal.ZERO;
        }

        public int getRigaOrd() {
            return rigaOrd;
        }

        public String getCodJcom() {
            return codJcom;
        }

        public String getRagSocCom() {
            return ragSocCom;
        }

        public String getDescrizioneCommessa() {
            return descrizioneCommessa;
        }

        public MtbAart getMtbAart() {
            return mtbAart;
        }

        public PickingObjectDTO[] getColliAssociati() {
            return colliAssociati;
        }

        public Boolean isHidden() {
            return hidden;
        }

        public Boolean isTempHidden() {
            return tempHidden;
        }

        public String getPartitaMag() {
            return partitaMag;
        }

        public String getCodArtFor() {
            return codArtFor;
        }

        public String getDescrizioneEstesa() {
            return descrizioneEstesa;
        }

        public String getCodAlis() {
            return codAlis;
        }

        public int getNumOrd() {
            return numOrd;
        }

        public String getGestioneOrd() {
            return gestioneOrd;
        }

        public String getDataCons() {
            return dataCons;
        }

        public Date getDataOrd() {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Date dataOrd = null;

            try{
                dataOrd = sdf.parse(this.dataOrd);
            } catch (Exception ex) {
                Log.e("Parsing date", ex.getMessage());
            }

            return dataOrd;
        }


        public Riga setQtaOrd(Float qtaOrd) {
            this.qtaOrd = qtaOrd;
            return this;
        }

        public Riga setQtaRiservate(BigDecimal qtaRiservate) {
            this.qtaRiservate = qtaRiservate.floatValue();
            return this;
        }

        public Riga setQtaEvasa(Float qtaEvasa) {
            this.qtaEvasa = qtaEvasa;
            return this;
        }

        public Riga setRigaOrd(int rigaOrd) {
            this.rigaOrd = rigaOrd;
            return this;
        }

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

        public Riga setRagSocCom(String ragSocCom) {
            this.ragSocCom = ragSocCom;
            return this;
        }

        public Riga setDescrizioneCommessa(String descrizioneCommessa) {
            this.descrizioneCommessa = descrizioneCommessa;
            return this;
        }

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

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

        public Riga setTempHidden(boolean hidden) {
            this.tempHidden = hidden;
            return this;
        }

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

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

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

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

        public Riga setNumOrd(int numOrd) {
            this.numOrd = numOrd;
            return this;
        }

        public Riga setDataOrd(String dataOrd) {
            this.dataOrd = dataOrd;
            return this;
        }

        public Riga setGestioneOrd(String gestioneOrd) {
            this.gestioneOrd = gestioneOrd;
            return this;
        }

        public Riga setDataCons(String dataCons) {
            this.dataCons = dataCons;
            return this;
        }

        public Riga(){}

        protected Riga(Parcel in) {
            qtaOrd = in.readByte() == 0x00 ? null : in.readFloat();
            qtaRiservate = in.readByte() == 0x00 ? null : in.readFloat();
            rigaOrd = in.readInt();
            codJcom = in.readString();
            ragSocCom = in.readString();
            descrizioneCommessa = in.readString();
            mtbAart = (MtbAart) in.readValue(MtbAart.class.getClassLoader());
            hidden = in.readByte() == 0x00 ? null : in.readByte() != 0x00;
            partitaMag = in.readString();
            codArtFor = in.readString();
            descrizioneEstesa = in.readString();
            codAlis = in.readString();
            numOrd = in.readInt();
            dataOrd = in.readString();
            gestioneOrd = in.readString();
            dataCons = in.readString();
        }

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            if (qtaOrd == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeFloat(qtaOrd);
            }
            if (qtaRiservate == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeFloat(qtaRiservate);
            }
            dest.writeInt(rigaOrd);
            dest.writeString(codJcom);
            dest.writeString(ragSocCom);
            dest.writeString(descrizioneCommessa);
            dest.writeValue(mtbAart);
            if (hidden == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeByte((byte) (hidden ? 0x01 : 0x00));
            }
            dest.writeString(partitaMag);
            dest.writeString(codArtFor);
            dest.writeString(descrizioneEstesa);
            dest.writeString(codAlis);
            dest.writeInt(numOrd);
            dest.writeString(dataOrd);
            dest.writeString(gestioneOrd);
            dest.writeString(dataCons);
        }

        @SuppressWarnings("unused")
        public static final Parcelable.Creator<Riga> CREATOR = new Parcelable.Creator<Riga>() {
            @Override
            public Riga createFromParcel(Parcel in) {
                return new Riga(in);
            }

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

    }

    public static class PickingObjectDTO extends MtbColt{
        public boolean hidden = false;
    }

    public OrdineAccettazioneDTO() {}

    protected OrdineAccettazioneDTO(Parcel in) {
        ragSoc = in.readString();
        numero = in.readInt();
        data = in.readString();
        gestione = in.readString();
        codMdep = in.readString();
        codAnag = in.readString();
        codVdes = in.readString();
        termCons = in.readString();
        codAlis = in.readString();
        rifOrd = in.readString();
        if (in.readByte() == 0x01) {
            ordini = new ArrayList<Riga>();
            in.readList(ordini, Riga.class.getClassLoader());
        } else {
            ordini = null;
        }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(ragSoc);
        dest.writeInt(numero);
        dest.writeString(data);
        dest.writeString(gestione);
        dest.writeString(codMdep);
        dest.writeString(codAnag);
        dest.writeString(codVdes);
        dest.writeString(termCons);
        dest.writeString(codAlis);
        dest.writeString(rifOrd);
        if (ordini == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(ordini);
        }
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<OrdineAccettazioneDTO> CREATOR = new Parcelable.Creator<OrdineAccettazioneDTO>() {
        @Override
        public OrdineAccettazioneDTO createFromParcel(Parcel in) {
            return new OrdineAccettazioneDTO(in);
        }

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



}
