package it.integry.integrywmsnative.view.dialogs.ask_position_of_lu;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.databinding.DataBindingUtil;

import com.google.android.material.textfield.TextInputLayout;

import java.util.ArrayList;
import java.util.List;

import it.integry.barcode_base_android_library.model.BarcodeScanDTO;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeCallbackDTO;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.RunnableArgss;
import it.integry.integrywmsnative.core.model.MtbDepoPosizione;
import it.integry.integrywmsnative.core.utility.UtilityBarcode;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityPosizione;
import it.integry.integrywmsnative.databinding.DialogAskPositionOfLuBinding;
import it.integry.integrywmsnative.view.dialogs.DialogConsts;

public class DialogAskPositionOfLU {


    private Context mContext;
    private DialogAskPositionOfLuBinding mBinding;
    private boolean mCheckForLineaProd;

    private MtbDepoPosizione currentMtbDepoPosizione;

    private Dialog mDialog;

    private RunnableArgss<DialogConsts.Results, MtbDepoPosizione> onComplete;
    private RunnableArgs<Exception> onFailed;

    private TextInputLayout livelloTextInputLayout;

    private boolean completedFlow = false;

    public static Dialog makeBase(final Context context, boolean checkForLineaProd, RunnableArgss<DialogConsts.Results, MtbDepoPosizione> onComplete, RunnableArgs<Exception> onFailed) {
        return new DialogAskPositionOfLU(context, checkForLineaProd, onComplete, onFailed).mDialog;
    }

    private DialogAskPositionOfLU(Context context, boolean checkForLineaProd, RunnableArgss<DialogConsts.Results, MtbDepoPosizione> onComplete, RunnableArgs<Exception> onFailed) {
        mContext = context;
        mCheckForLineaProd = checkForLineaProd;

        this.onComplete = onComplete;
        this.onFailed = onFailed;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_ask_position_of_lu, null, false);
        mBinding.setMContext(mContext);

        List<Integer> views = new ArrayList<>();
        views.add(R.layout.dialog_ask_position_of_lu__page1);
        views.add(R.layout.dialog_ask_position_of_lu__page2);

        DialogAskPositionOfLUAdapter adapter = new DialogAskPositionOfLUAdapter(mContext, views);
        mBinding.viewpager.setAdapter(adapter);
        mBinding.viewpager.beginFakeDrag();


        mDialog = new Dialog(mContext);
        mDialog.setContentView(mBinding.getRoot());
        mDialog.setCanceledOnTouchOutside(false);
//        mDialog.setCancelable(false);
        mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        UtilityDialog.setTo90PercentWidth(mContext, mDialog);

        mDialog.setOnShowListener(dialog -> {
            ((AppCompatTextView) adapter.getPage(0).findViewById(R.id.description_text)).setText(mCheckForLineaProd ? R.string.ask_production_line_of_lu_message : R.string.ask_position_of_lu_message);

            adapter.getPage(1).findViewById(R.id.button_confirm).setOnClickListener(v -> {
                String levelNumber = ((TextView)adapter.getPage(1).findViewById(R.id.level_number)).getText().toString();
                onLevelConfirm(levelNumber);
            });

            adapter.getPage(1).findViewById(R.id.button_abort).setOnClickListener(v -> {
                onLevelAbort();
            });

            livelloTextInputLayout = adapter.getPage(1).findViewById(R.id.level_number_layout);

            livelloTextInputLayout.getEditText().addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(livelloTextInputLayout.getEditText().getText() != null) {
                        validateLevel(livelloTextInputLayout.getEditText().getText().toString());
                    } else resetLevelError();
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        });

        setupBarcode();
    }


    private void askLivello() {
        mBinding.viewpager.setCurrentItem(1, true);
    }

    private boolean isOnLivelloPage() {
        return mBinding.viewpager.getCurrentItem() == 1;
    }

    private void setupBarcode() {

        int barcodeIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(onScanSuccessfull)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(mContext, ex, false)));

        mDialog.setOnDismissListener(dialog -> {
            BarcodeManager.removeCallback(barcodeIstanceID);
            if(!completedFlow) onComplete.run(DialogConsts.Results.ABORT, null);
        });

        BarcodeManager.enable();

    }


    private final RunnableArgs<BarcodeScanDTO> onScanSuccessfull = data -> {

        BarcodeManager.disable();

        if(!isOnLivelloPage()) {

            if (UtilityBarcode.isEtichettaPosizione(data)) {

                MtbDepoPosizione foundPosizione = UtilityPosizione.getFromCache(data.getStringValue());

                currentMtbDepoPosizione = foundPosizione;

                if (!mCheckForLineaProd && !currentMtbDepoPosizione.isFlagLineaProduzione() && UtilityPosizione.isPosizioneWithLivello(foundPosizione)) {
                    askLivello();
                    BarcodeManager.enable();
                } else {
                    completedFlow = true;
                    if(onComplete != null) onComplete.run(DialogConsts.Results.YES, foundPosizione);

                    mDialog.dismiss();
                    BarcodeManager.enable();
                }

            } else {
                BarcodeManager.enable();
            }
        } else {
            BarcodeManager.enable();
        }
    };


    private void onLevelConfirm(String levelString) {

        String newPosizione = currentMtbDepoPosizione.getPosizione().substring(0, currentMtbDepoPosizione.getPosizione().length()-1);
        newPosizione += levelString;

        MtbDepoPosizione foundPosizione = UtilityPosizione.getFromCache(newPosizione);

        if(foundPosizione != null) {
            resetLevelError();

            completedFlow = true;
            if(onComplete != null) onComplete.run(DialogConsts.Results.YES, foundPosizione);
            mDialog.dismiss();

        } else {
            setLevelError(mContext.getResources().getString(R.string.not_valid));
        }
    }

    private void validateLevel(String levelString) {
        String newPosizione = currentMtbDepoPosizione.getPosizione().substring(0, currentMtbDepoPosizione.getPosizione().length()-1);
        newPosizione += levelString;

        if(UtilityPosizione.getFromCache(newPosizione) != null) {
            resetLevelError();
        } else setLevelError(mContext.getResources().getString(R.string.not_valid));
    }

    private void setLevelError(String message){
        livelloTextInputLayout.setErrorEnabled(true);
        livelloTextInputLayout.setError(message);
        livelloTextInputLayout.setErrorIconDrawable(null);
    }

    private void resetLevelError() {
        livelloTextInputLayout.setError(null);
    }


    private void onLevelAbort() {
        completedFlow = true;
        onComplete.run(DialogConsts.Results.ABORT, null);
        mDialog.dismiss();
    }

}
