package it.integry.integrywmsnative.view.bottom_sheet__item_edit;

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

import androidx.databinding.ObservableField;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.databinding.BottomSheetItemEditViewBinding;
import it.integry.integrywmsnative.view.bottom_sheet__base.BottomSheetFragmentBaseView;

public class BottomSheetItemEditView extends BottomSheetFragmentBaseView {


    @Inject
    BottomSheetItemEditViewModel mViewModel;

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

    private final BottomSheetItemEditViewBinding mBinding;
    private Listener mListener;

    public BottomSheetItemEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBinding = BottomSheetItemEditViewBinding.inflate(LayoutInflater.from(context), this, true);

        MainApplication.appComponent
                .bottomSheetItemEditComponent()
                .create()
                .inject(this);



        mBinding.setViewModel(mViewModel);
        mBinding.setView(this);
    }

    public BottomSheetItemEditView setItem(BottomSheetItemEditDTO item) {
        this.mBinding.invalidateAll();

        this.mViewModel.init(item);

        this.mBinding.executePendingBindings();
        this.parentView.requestLayout();

        return this;
    }

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

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

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

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



    public void editItem() {
        if(this.mListener != null) this.mListener.onItemEdit();
        this.collapse();
    }

    public void deleteItem() {
        if(this.mListener != null) this.mListener.onItemDelete();
        this.collapse();
    }



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

    @Override
    protected void onSlideCallback(float slideOffset) {

    }


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

    public interface Listener {
        void onItemEdit();

        void onItemDelete();
    }
}
