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

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.integry.integrywmsnative.core.interfaces.ICheckBoxCallback;
import it.integry.integrywmsnative.core.model.secondary.GestioneEnum;
import it.integry.integrywmsnative.core.utility.UtilityDate;

public class OrdineVenditaGroupedInevasoDTO implements Cloneable, Serializable, Parcelable {

    public String codAnagClie;
    public String nomeCliente;
    public List<Ordine> ordini;

    public OrdineVenditaGroupedInevasoDTO clone() throws CloneNotSupportedException {
        return (OrdineVenditaGroupedInevasoDTO) super.clone();
    }


    public static class Ordine implements Parcelable{
        private String data;
        private int numero;
        private String codAnagOrd = "";
        private String ragSocOrd = "";
        private Float pesoTotale;
        private String barcode;
        private String termCons;
        private String dataCons;
        private String rifOrd;
        private String gestione;

        public List<Cliente> clienti;

        public boolean checkBox;
        public List<ICheckBoxCallback> checkBoxCallback = new ArrayList<>();



        public void setCheckbox(boolean newValue){
            checkBox = newValue;
            if(checkBoxCallback != null && checkBoxCallback.size() > 0) {
                for(ICheckBoxCallback callback : checkBoxCallback) callback.onToggle(newValue);
            }
        }


        public Date getData(){
            try {
                return UtilityDate.recognizeDate(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public Ordine setData(String data) {
            this.data = data;
            return this;
        }

        public int getNumero() {
            return numero;
        }

        public Ordine setNumero(int numero) {
            this.numero = numero;
            return this;
        }

        public String getCodAnagOrd() {
            return codAnagOrd;
        }

        public Ordine setCodAnagOrd(String codAnagOrd) {
            this.codAnagOrd = codAnagOrd;
            return this;
        }

        public String getRagSocOrd() {
            return ragSocOrd;
        }

        public Ordine setRagSocOrd(String ragSocOrd) {
            this.ragSocOrd = ragSocOrd;
            return this;
        }

        public Float getPesoTotale() {
            return pesoTotale;
        }

        public Ordine setPesoTotale(Float pesoTotale) {
            this.pesoTotale = pesoTotale;
            return this;
        }

        public String getBarcode() {
            return barcode;
        }

        public Ordine setBarcode(String barcode) {
            this.barcode = barcode;
            return this;
        }

        public String getTermCons() {
            return termCons;
        }

        public Ordine setTermCons(String termCons) {
            this.termCons = termCons;
            return this;
        }

        public String getDataConsS(){
            return dataCons;
        }

        public Date getDataConsD(){
            try {
                return UtilityDate.recognizeDate(dataCons);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

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

        public String getRifOrd() {
            return rifOrd;
        }

        public Ordine setRifOrd(String rifOrd) {
            this.rifOrd = rifOrd;
            return this;
        }

        public String getGestione() {
            return gestione;
        }

        public GestioneEnum getGestioneEnum() {
            return GestioneEnum.fromString(getGestione());
        }

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

        public List<Cliente> getClienti() {
            return clienti;
        }

        public Ordine setClienti(List<Cliente> clienti) {
            this.clienti = clienti;
            return this;
        }

        public boolean isCheckBox() {
            return checkBox;
        }

        public Ordine setCheckBox(boolean checkBox) {
            this.checkBox = checkBox;
            return this;
        }

        public Ordine(){}

        protected Ordine(Parcel in) {
            data = in.readString();
            numero = in.readInt();
            codAnagOrd = in.readString();
            ragSocOrd = in.readString();
            pesoTotale = in.readByte() == 0x00 ? null : in.readFloat();
            barcode = in.readString();
            termCons = in.readString();
            dataCons = in.readString();
            rifOrd = in.readString();
            if (in.readByte() == 0x01) {
                clienti = new ArrayList<>();
                in.readList(clienti, Cliente.class.getClassLoader());
            } else {
                clienti = null;
            }
            gestione = in.readString();
        }

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(data);
            dest.writeInt(numero);
            dest.writeString(codAnagOrd);
            dest.writeString(ragSocOrd);
            if (pesoTotale == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeFloat(pesoTotale);
            }
            dest.writeString(barcode);
            dest.writeString(termCons);
            dest.writeString(dataCons);
            dest.writeString(rifOrd);
            if (clienti == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeList(clienti);
            }
            dest.writeString(gestione);
        }

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

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

    }

    public static class Cliente implements Parcelable{
        public String codJcom;
        public String ragSocCom;
        public int idRiga;
        public String dataCons;
        public Float numCnf;
        public String descrCom;
        public String rifOrd;

        public Date getDataCons(){
            try {
                return UtilityDate.recognizeDate(dataCons);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public Cliente(){}

        protected Cliente(Parcel in) {
            codJcom = in.readString();
            ragSocCom = in.readString();
            idRiga = in.readInt();
            dataCons = in.readString();
            numCnf = in.readByte() == 0x00 ? null : in.readFloat();
            descrCom = in.readString();
            rifOrd = in.readString();
        }

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(codJcom);
            dest.writeString(ragSocCom);
            dest.writeInt(idRiga);
            dest.writeString(dataCons);
            if (numCnf == null) {
                dest.writeByte((byte) (0x00));
            } else {
                dest.writeByte((byte) (0x01));
                dest.writeFloat(numCnf);
            }
            dest.writeString(descrCom);
            dest.writeString(rifOrd);
        }

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

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

    public OrdineVenditaGroupedInevasoDTO(){}

    protected OrdineVenditaGroupedInevasoDTO(Parcel in) {
        codAnagClie = in.readString();
        nomeCliente = in.readString();
        if (in.readByte() == 0x01) {
            ordini = new ArrayList<>();
            in.readList(ordini, Ordine.class.getClassLoader());
        } else {
            ordini = null;
        }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(codAnagClie);
        dest.writeString(nomeCliente);
        if (ordini == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(ordini);
        }
    }

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

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