package it.integry.integrywmsnative.core.utility;

import com.annimon.stream.Stream;

import it.integry.integrywmsnative.core.model.MtbDepoPosizione;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.plugins.barcode_base_library.model.BarcodeScanDTO;
import it.integry.plugins.barcode_base_library.model.BarcodeType;

public class UtilityBarcode {

    public static boolean isEtichettaAnonima(BarcodeScanDTO barcodeScanDTO){
        return barcodeScanDTO != null && barcodeScanDTO.getType() == BarcodeType.CODE128 && barcodeScanDTO.getStringValue().startsWith("U");
    }

    public static boolean isEtichetta128(BarcodeScanDTO barcodeScanDTO){
        return barcodeScanDTO != null && (barcodeScanDTO.getType() == BarcodeType.CODE128 || barcodeScanDTO.getType() == BarcodeType.EAN128);
    }

    public static boolean isEtichettaPosizione(BarcodeScanDTO barcodeScanDTO) {
        String barcode = barcodeScanDTO.getStringValue();
        boolean isPosizione = false;

        if(SettingsManager.iDB().getAvailablePosizioni() != null) {
            Stream<MtbDepoPosizione> tmpStream = Stream.of(SettingsManager.iDB().getAvailablePosizioni())
                    .filter(x -> x.getPosizione().equalsIgnoreCase(barcode));

            if(tmpStream.count() > 0){
               isPosizione = true;
            }
        }


        return barcodeScanDTO != null && isPosizione;
    }

    public static boolean isEanPeso(BarcodeScanDTO barcodeScanDTO) {
        return barcodeScanDTO != null && barcodeScanDTO.getType() == BarcodeType.EAN13 && barcodeScanDTO.getStringValue().startsWith("2");
    }



    public static Integer getNumColloFromULAnonima(String barcode) {
        if(barcode != null){
            return Integer.parseInt(barcode.substring(3));
        } else
            return null;
    }



    public static Integer getAnnoColloFromULAnonima(String barcode) {
        if(barcode != null){
            return Integer.parseInt(barcode.substring(1, 3));
        } else
            return null;
    }



    public static String convertITF14toEAN13(String barcodeITF14) {
        String barcodeEAN13 = null;

        if(barcodeITF14.length() == 14) {
            barcodeEAN13 = barcodeITF14.substring(1, barcodeITF14.length() - 1).trim();
            barcodeEAN13 += getEAN13CheckDigit(barcodeEAN13);
        }

        return barcodeEAN13;
    }

    public static String convertITF14toNeutral(String barcodeITF14) {
        String barcodeNeutral = null;

        if(barcodeITF14.length() == 14) {
            barcodeNeutral = barcodeITF14.substring(1, barcodeITF14.length() - 1);
        }

        return barcodeNeutral;
    }


    private static String getEAN13CheckDigit(String ean)  {

        if (ean.length() != 12) {
            UtilityLogger.errorMe(new Exception("Please provide an input string of 12 chars. Current lenght: "+ean.length()));
            return null;
        }
        long tot = 0;

        for (int i = 0; i < 12; i++) {
            tot = tot + (Long.parseLong(String.valueOf(ean.charAt(i))) * (i % 2 == 0 ? 1 : 3));
        }
        return tot % 10 == 0 ? "0" : "" +(10-(tot % 10));
    }

}
