package it.integry.integrywmsnative.view.dialogs.ask_cliente.viewmodel;

import android.content.Context;
import android.widget.AutoCompleteTextView;

import androidx.databinding.ViewDataBinding;

import com.annimon.stream.Stream;

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

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.databinding.DialogAskClientePage2Binding;
import it.integry.integrywmsnative.view.dialogs.ask_cliente.dto.DialogAskClienteDestinatarioDTO;

public class DialogAskCliente_Page2ViewModel implements IDialogAskClienteViewModel {

    private DialogAskClientePage2Binding mBinding;
    private Context mContext;

    private Runnable mOnConfirm;
    private Runnable mOnAbort;

    private ArrayList<DialogAskClienteDestinatarioDTO> availableDestinatari;

    private String mCodAnag;

    public DialogAskCliente_Page2ViewModel() {

    }

    @Override
    public void setBinding(ViewDataBinding binding) {
        this.mBinding = (DialogAskClientePage2Binding) binding;
    }

    public void setAvailableDestinatari(ArrayList<DialogAskClienteDestinatarioDTO> availableDestinatari) {
        this.availableDestinatari = availableDestinatari;
    }

    @Override
    public void setContext(Context context) {
        this.mContext = context;
    }

    @Override
    public void onShow() {
        var filteredDestinatari = availableDestinatari != null ? Stream.of(availableDestinatari)
                .filter(x -> x.getCodAnag().equalsIgnoreCase(mCodAnag))
                .toList()
                : null;


        if (filteredDestinatari == null || filteredDestinatari.size() == 0) {
            mOnConfirm.run();
            return;
        }

        if(SettingsManager.iDB().isFlagPickLiberoAllowEmptyDest()) {
            filteredDestinatari.add(new DialogAskClienteDestinatarioDTO()
                    .setCodAnag(null)
                    .setDestinatario("Nessuno"));
        }

        initializeAdapter(filteredDestinatari);


        this.mBinding.buttonYes.setOnClickListener(v -> {
            if (validateDestinatario()) {
                this.resetClienteError();
                if (mOnConfirm != null) mOnConfirm.run();
            } else {
                this.setClienteError(mContext.getResources().getText(R.string.not_valid_recipient_error).toString());
            }
        });


        this.mBinding.buttonNo.setOnClickListener(v -> {
            if (mOnAbort != null) mOnAbort.run();
        });
    }

    @Override
    public void onDismiss() {
    }

    @Override
    public void setOnConfirmClickListener(Runnable onConfirm) {
        this.mOnConfirm = onConfirm;
    }

    @Override
    public void setOnAbortClickListener(Runnable onAbort) {
        this.mOnAbort = onAbort;
    }

    public void setCodAnag(String codAnag) {
        this.mCodAnag = codAnag;
    }


    private void initializeAdapter(List<DialogAskClienteDestinatarioDTO> items) {
        var orderedList = Stream.of(items)
                .sortBy(x -> x.getCodAnag() != null ? x.getCodAnag() : "")
                .toList();

        DialogAskCliente_Page2_ArrayAdapter adapter = new DialogAskCliente_Page2_ArrayAdapter(mContext, orderedList);


        AutoCompleteTextView editTextFilledExposedDropdown = mBinding.dropdownDestinatario;
        editTextFilledExposedDropdown.setThreshold(0);
        editTextFilledExposedDropdown.setAdapter(adapter);
    }


    private boolean validateDestinatario() {
        if (SettingsManager.iDB().isFlagPickLiberoAllowEmptyDest() && getCurrentDestinatario() == null) {
            return true;
        } else return getCurrentDestinatario() != null;
    }

    public DialogAskClienteDestinatarioDTO getCurrentDestinatario() {
        for (DialogAskClienteDestinatarioDTO destinatarioDTO : availableDestinatari) {
            if (destinatarioDTO.toString().equalsIgnoreCase(mBinding.inputDestinatario.getEditText().getText().toString())) {
                return destinatarioDTO;
            }
        }

        return null;
    }


    private void setClienteError(String message) {
        mBinding.inputDestinatario.setErrorEnabled(true);
        mBinding.inputDestinatario.setError(message);
        mBinding.inputDestinatario.setErrorIconDrawable(null);
    }

    private void resetClienteError() {
        mBinding.inputDestinatario.setError(null);
    }
}
