package it.integry.integrywmsnative.gest.accettazione.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.utility.UtilityDate;

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

public class OrdineAccettazioneGroupedInevasoDTO implements Cloneable, Serializable, Parcelable {

    public String codAnagForn;
    public String nomeFornitore;
    public List<Ordine> ordini;

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


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

        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 List<Cliente> clienti;


        public Date getData(){
            try {
                return UtilityDate.recognizeDate(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        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<Cliente>();
                in.readList(clienti, Cliente.class.getClassLoader());
            } else {
                clienti = null;
            }
        }

        @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);
            }
        }

        @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 Integer 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.readInt();
            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.writeInt(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 OrdineAccettazioneGroupedInevasoDTO(){}

    protected OrdineAccettazioneGroupedInevasoDTO(Parcel in) {
        codAnagForn = in.readString();
        nomeFornitore = in.readString();
        if (in.readByte() == 0x01) {
            ordini = new ArrayList<Ordine>();
            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(codAnagForn);
        dest.writeString(nomeFornitore);
        if (ordini == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(ordini);
        }
    }

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

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

}
