package it.integry.integrywmsnative.view.dialogs.choose_batch_lot;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
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.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import javax.inject.Inject;

import it.integry.integrywmsnative.MainApplication;
import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.BaseDialogFragment;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbPartitaMag;
import it.integry.integrywmsnative.databinding.DialogChooseBatchLotBinding;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;

public class DialogChooseBatchLotView extends BaseDialogFragment {

    @Inject
    DialogChooseBatchLotViewModel mViewModel;

    private final List<MtbPartitaMag> mAvailableBatchLot;
    private final RunnableArgs<MtbPartitaMag> mOnItemSelected;

    private Context mContext;
    private DialogChooseBatchLotBinding mBindings;

    private MtbPartitaMag mSelectedItem;

    public static DialogChooseBatchLotView newInstance(List<MtbPartitaMag> availableBatchLot, RunnableArgs<MtbPartitaMag> onItemSelected) {
        return new DialogChooseBatchLotView(availableBatchLot, onItemSelected);
    }

    private DialogChooseBatchLotView(List<MtbPartitaMag> availableBatchLot, RunnableArgs<MtbPartitaMag> onItemSelected) {
        super();

        this.mAvailableBatchLot = availableBatchLot;
        this.mOnItemSelected = onItemSelected;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mContext = getActivity();

        mBindings = DataBindingUtil.inflate(inflater, R.layout.dialog_choose_batch_lot, container, false);

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

        mBindings.setView(this);
        mBindings.setViewmodel(this.mViewModel);

        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        return mBindings.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();

        this.initRecyclerView();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

        if(mOnItemSelected != null) {
            this.mOnItemSelected.run(mSelectedItem);
        }
    }

    private void initRecyclerView() {
        mBindings.mainList.setNestedScrollingEnabled(false);
        mBindings.mainList.setHasFixedSize(true);
        mBindings.mainList.setLayoutManager(new LinearLayoutManager(mContext));

        SimpleDividerItemDecoration itemDecorator = new SimpleDividerItemDecoration(mContext, SimpleDividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.divider));
        mBindings.mainList.addItemDecoration(itemDecorator);

        DialogChooseBatchLotAdapter mAdapter = new DialogChooseBatchLotAdapter(mAvailableBatchLot, onItemClicked);
        mBindings.mainList.setAdapter(mAdapter);
    }

    private final IRecyclerItemClicked<MtbPartitaMag> onItemClicked = (item, position) -> {
        mSelectedItem = item;
        dismiss();
    };

}
