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

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

import androidx.databinding.ViewDataBinding;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.rest.consumers.ISimpleOperationCallback;
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
import it.integry.integrywmsnative.core.utility.UtilityDB;
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 List<DialogAskClienteDestinatarioDTO> availableDestinatari;

    private String mCodAnag;

    public DialogAskCliente_Page2ViewModel() {

    }

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

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

    @Override
    public void onShow() {
        String sql = "SELECT cod_anag, cod_vdes, destinatario, indirizzo, cap, citta, prov, nazione " +
                "FROM vtb_dest " +
                "WHERE cod_anag = " + UtilityDB.valueToString(mCodAnag);

        Type typeOfObjectsList = new TypeToken<ArrayList<DialogAskClienteDestinatarioDTO>>() {
        }.getType();
        SystemRESTConsumer.processSqlStatic(sql, typeOfObjectsList, new ISimpleOperationCallback<ArrayList<DialogAskClienteDestinatarioDTO>>() {
            @Override
            public void onSuccess(ArrayList<DialogAskClienteDestinatarioDTO> value) {
                availableDestinatari = value;
                initializeAdapter(value);

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

            @Override
            public void onFailed(Exception ex) {

            }
        });
    }

    @Override
    public void onDismiss() {
    }

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

        });
    }

    @Override
    public void setOnAbortClickListener(Runnable onAbort) {
        this.mBinding.buttonNo.setOnClickListener(v -> {
            if (onAbort != null) onAbort.run();
        });
    }

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


    private void initializeAdapter(ArrayList<DialogAskClienteDestinatarioDTO> items) {

        DialogAskCliente_Page2_ArrayAdapter adapter = new DialogAskCliente_Page2_ArrayAdapter(mContext, items);


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


    private boolean validateDestinatario() {
        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);
    }
}
