package it.integry.integrywmsnative.view.bottom_sheet__mtb_colr_edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.utility.UtilityLogger;
import it.integry.integrywmsnative.databinding.BottomSheetMtbColrEditBinding;
import it.integry.integrywmsnative.view.bottom_sheet__base.BottomSheetFragmentBaseView;
import it.integry.integrywmsnative.view.bottom_sheet__mtb_colr_edit.exceptions.NullMtbColrException;

public class BottomSheetMtbColrEditView extends BottomSheetFragmentBaseView {

    private BottomSheetMtbColrEditBinding mBinding;
    private BottomSheetMtbColrEditViewModel mViewModel;

    public ObservableField<Boolean> enabledEditButton = new ObservableField<>(true);
    public ObservableField<Boolean> enabledDeleteButton = new ObservableField<>(true);

    private Listener mListener;

    public BottomSheetMtbColrEditView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.bottom_sheet__mtb_colr_edit, this, true);


        mViewModel = new BottomSheetMtbColrEditViewModel();
        mBinding.setViewModel(mViewModel);
        mBinding.setView(this);
    }

    public void setMtbColr(MtbColr mtbColr) {
        this.mBinding.invalidateAll();
        this.mViewModel.setMtbColr(mtbColr);
        this.mBinding.executePendingBindings();

        this.parentView.requestLayout();
    }

    public void editMtbColr() {
        if(this.mListener != null) this.mListener.onMtbColrEdit(this.mViewModel.getMtbColr());
        this.collapse();
    }

    public void deleteMtbColr() {
        if(this.mListener != null) this.mListener.onMtbColrDelete(this.mViewModel.getMtbColr());
        this.collapse();
    }





    @Override
    public void expand() {
        if(this.mViewModel.getMtbColr() == null) {
            UtilityLogger.errorMe(new NullMtbColrException());
            return;
        }

        super.expand();
    }

    @Override
    protected void onStateChangedCallback(State newState) {
        if(newState == State.COLLAPSED) this.mViewModel.setMtbColr(null);
    }

    @Override
    protected void onSlideCallback(float slideOffset) {

    }

    public void enableEditButton() {
        this.enabledEditButton.set(true);
    }

    public void disableEditButton() {
        this.enabledEditButton.set(false);
    }

    public void enableDeleteButton() {
        this.enabledDeleteButton.set(true);
    }

    public void disableDeleteButton() {
        this.enabledDeleteButton.set(false);
    }


    public BottomSheetMtbColrEditViewModel getViewModelInstance() {
        return this.mViewModel;
    }


    public BottomSheetMtbColrEditView setListener(Listener listener) {
        this.mListener = listener;
        return this;
    }

    public interface Listener {

        void onMtbColrEdit(MtbColr mtbColr);

        void onMtbColrDelete(MtbColr mtbColr);

    }

}
