package it.integry.integrywmsnative.gest.inventario.bottom_sheet__inventario_row_actions;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ObservableField;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.core.data_store.db.entity.InventarioRowRoomDTO;
import it.integry.integrywmsnative.databinding.BottomSheetInventarioRowActionsViewBinding;

public class BottomSheetInventarioRowActionsView extends BottomSheetDialogFragment {

    @Inject
    BottomSheetInventarioRowActionsViewModel mViewModel;

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

    private BottomSheetInventarioRowActionsViewBinding mBinding;
    private Listener mListener;

    private final InventarioRowRoomDTO inputItem;

    public BottomSheetInventarioRowActionsView(InventarioRowRoomDTO inputItem) {
        this.inputItem = inputItem;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        mViewModel.init(inputItem);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = BottomSheetInventarioRowActionsViewBinding.inflate(inflater, container, true);

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

        mBinding.executePendingBindings();

        return mBinding.getRoot();
    }




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

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

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

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



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

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



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

    public interface Listener {
        void onItemEdit();

        void onItemDelete();
    }
}
