package it.integry.integrywmsnative.core.model.dto;

import java.math.BigDecimal;

import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.model.MtbPartitaMag;
import it.integry.integrywmsnative.core.rest.model.Ean128Model;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityString;

public class PickDataDTO {

    private MtbColt refMtbColt;
    private MtbColt sourceMtbColt;
    private ManualPickDTO manualPickDTO;

    public MtbColt getRefMtbColt() {
        return refMtbColt;
    }

    public PickDataDTO setRefMtbColt(MtbColt refMtbColt) {
        this.refMtbColt = refMtbColt;
        return this;
    }

    public MtbColt getSourceMtbColt() {
        return sourceMtbColt;
    }

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

    public ManualPickDTO getManualPickDTO() {
        return manualPickDTO;
    }

    public PickDataDTO setManualPickDTO(ManualPickDTO manualPickDTO) {
        this.manualPickDTO = manualPickDTO;
        return this;
    }

    public static PickDataDTO fromEan128(Ean128Model ean128Model) {
        if (ean128Model == null) return null;

        PickDataDTO pickDataDTO = new PickDataDTO();
        ManualPickDTO manualPickDTO = new ManualPickDTO();
        pickDataDTO.setManualPickDTO(manualPickDTO);

        String partitaMag = ean128Model.BatchLot;
        String dataScad = !UtilityString.isNullOrEmpty(ean128Model.BestBefore) ? ean128Model.BestBefore : ean128Model.Expiry;

        if (!UtilityString.isNullOrEmpty(partitaMag)) {
            MtbPartitaMag mtbPartitaMag = new MtbPartitaMag()
                    .setPartitaMag(partitaMag)
                    .setDataScad(UtilityDate.recognizeLocalDateWithExceptionHandler(dataScad));

            manualPickDTO.setMtbPartitaMag(mtbPartitaMag);
        }

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

        if (ean128Model.NetWeightKg != null && ean128Model.NetWeightKg > 0 && (ean128Model.Count == null || ean128Model.Count == 0)) {
            manualPickDTO.setQtaTot(new BigDecimal(ean128Model.NetWeightKg));
            manualPickDTO.setEanPeso(true);
        }

        if(ean128Model.NetWeightKg != null && ean128Model.NetWeightKg > 0 && ean128Model.Count != null && ean128Model.Count > 0) {
            manualPickDTO.setNumCnf(new BigDecimal(ean128Model.Count));
            manualPickDTO.setQtaTot(new BigDecimal(ean128Model.NetWeightKg));
            manualPickDTO.setEanPeso(true);
        }

        return pickDataDTO;
    }

    public static class ManualPickDTO {

        private MtbPartitaMag mtbPartitaMag;
        private BigDecimal numCnf;
        private BigDecimal qtaTot;
        private boolean eanPeso;

        public MtbPartitaMag getMtbPartitaMag() {
            return mtbPartitaMag;
        }

        public ManualPickDTO setMtbPartitaMag(MtbPartitaMag mtbPartitaMag) {
            this.mtbPartitaMag = mtbPartitaMag;
            return this;
        }

        public BigDecimal getNumCnf() {
            return numCnf;
        }

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

        public BigDecimal getQtaTot() {
            return qtaTot;
        }

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

        public boolean isEanPeso() {
            return eanPeso;
        }

        public ManualPickDTO setEanPeso(boolean eanPeso) {
            this.eanPeso = eanPeso;
            return this;
        }
    }
}
