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

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.utility.UtilityDialog;
import it.integry.integrywmsnative.databinding.DialogChooseArtFromListaArtBinding;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;

public class DialogChooseArtFromListaMtbColrView {


    private Context currentContext;
    private Dialog mDialog;

    private DialogChooseArtFromListaMtbColrAdapter currentAdapter;

    private RunnableArgs<MtbColr> mOnItemChoosed;

    public static Dialog make(final Context context, List<MtbColr> mtbColrList, RunnableArgs<MtbColr> onItemChoosed) {
        return new DialogChooseArtFromListaMtbColrView(context, mtbColrList, onItemChoosed).mDialog;
    }


    private DialogChooseArtFromListaMtbColrView(Context context, List<MtbColr> mtbColrList, RunnableArgs<MtbColr> onItemChoosed) {
        currentContext = context;
        mOnItemChoosed = onItemChoosed;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        DialogChooseArtFromListaArtBinding bindings = DataBindingUtil.inflate(inflater, R.layout.dialog_choose_art_from_lista_art, null, false);

        mDialog = new Dialog(context);

        mDialog.setContentView(bindings.getRoot());

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

        UtilityDialog.setTo90PercentWidth(context, mDialog);

        initRecyclerView(bindings, mtbColrList);
    }

    private void initRecyclerView(DialogChooseArtFromListaArtBinding binding, List<MtbColr> dataset) {
        binding.dialogChooseArtFromListaArtMainList.setNestedScrollingEnabled(false);

        binding.dialogChooseArtFromListaArtMainList.setHasFixedSize(true);

        binding.dialogChooseArtFromListaArtMainList.setLayoutManager(new LinearLayoutManager(currentContext));

        SimpleDividerItemDecoration itemDecorator = new SimpleDividerItemDecoration(currentContext, SimpleDividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(currentContext, R.drawable.divider));
        binding.dialogChooseArtFromListaArtMainList.addItemDecoration(itemDecorator);

        currentAdapter = new DialogChooseArtFromListaMtbColrAdapter(currentContext, dataset, onItemClicked);
        binding.dialogChooseArtFromListaArtMainList.setAdapter(currentAdapter);
    }

    private IRecyclerItemClicked<MtbColr> onItemClicked = (item, position) -> {
        if(mOnItemChoosed != null) {
            mOnItemChoosed.run(item);
        }

        mDialog.dismiss();
    };

}
