package it.integry.integrywmsnative.gest.lista_bancali;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
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.data_cache.DataCache;
import it.integry.integrywmsnative.core.expansion.BaseActivity;
import it.integry.integrywmsnative.core.expansion.RunnableArgsWithReturn;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.rest.consumers.PrinterRESTConsumer;
import it.integry.integrywmsnative.databinding.ActivityListaBancaliBinding;
import it.integry.integrywmsnative.gest.contenuto_bancale.ContenutoBancaleActivity;
import it.integry.integrywmsnative.gest.lista_bancali.ui.ListaColliMainListAdapter;
import it.integry.integrywmsnative.ui.SimpleDividerItemDecoration;

public class ListaBancaliActivity extends BaseActivity implements ListaBancaliViewModel.Listener{

    private static class Key {
        private static final String MtbColtsKey = "mtbColtsKey";
        private static final String CanRecoverUL = "canRecoverUL";
        private static final String PrinterType = "printerType";
        private static final String ReportName = "reportName";
        private static final String FlagOnlyResiduo = "flagOnlyResiduo";
    }

    public ActivityListaBancaliBinding mBindings;

    @Inject
    ListaBancaliViewModel mViewModel;

    private final ObservableArrayList<MtbColt> mtbColts = new ObservableArrayList<>();

    private RunnableArgsWithReturn<MtbColt, Boolean> mCanRecoverUl;
    private PrinterRESTConsumer.Type mPrinterType;
    private String mReportName;
    private boolean mFlagOnlyResiduo;

    private static final int PICK_UL_REQUEST = 1;  // The request code



    public static Intent createIntent(Context context, List<MtbColt> items, RunnableArgsWithReturn<MtbColt, Boolean> canRecoverUlAction, PrinterRESTConsumer.Type printerType, String reportName) {
        return createIntent(context, items, canRecoverUlAction, printerType, true, reportName);
    }

    public static Intent createIntent(Context context, List<MtbColt> items, boolean canRecoverUl, PrinterRESTConsumer.Type printerType, boolean onlyResiduo) {
        return createIntent(context, items, input -> canRecoverUl, printerType, onlyResiduo);
    }

    public static Intent createIntent(Context context, List<MtbColt> items, RunnableArgsWithReturn<MtbColt, Boolean> canRecoverUlAction, PrinterRESTConsumer.Type printerType, boolean onlyResiduo) {
        return createIntent(context, items, canRecoverUlAction, printerType, onlyResiduo, null);
    }

    public static Intent createIntent(Context context, List<MtbColt> items, RunnableArgsWithReturn<MtbColt, Boolean> canRecoverUlAction, PrinterRESTConsumer.Type printerType, boolean onlyResiduo, String reportName) {
        Intent myIntent = new Intent(context, ListaBancaliActivity.class);

        String mtbColtsKey = DataCache.addItem(items);
        myIntent.putExtra(Key.MtbColtsKey, mtbColtsKey);

        String canRecoverULKey = DataCache.addItem(canRecoverUlAction);
        myIntent.putExtra(Key.CanRecoverUL, canRecoverULKey);

        String printerTypeKey = DataCache.addItem(printerType);
        myIntent.putExtra(Key.PrinterType, printerTypeKey);

        String flagOnlyResiduoKey = DataCache.addItem(onlyResiduo);
        myIntent.putExtra(Key.FlagOnlyResiduo, flagOnlyResiduoKey);

        String reportNameKey = DataCache.addItem(reportName);
        myIntent.putExtra(Key.ReportName, reportNameKey);

        return myIntent;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBindings = DataBindingUtil.setContentView(this, R.layout.activity_lista_bancali);

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


        mCanRecoverUl = DataCache.retrieveItem(getIntent().getStringExtra(Key.CanRecoverUL));
        mPrinterType = DataCache.retrieveItem(getIntent().getStringExtra(Key.PrinterType));
        mReportName = DataCache.retrieveItem(getIntent().getStringExtra(Key.ReportName));
        mFlagOnlyResiduo = DataCache.retrieveItem(getIntent().getStringExtra(Key.FlagOnlyResiduo));

        mtbColts.addAll(DataCache.retrieveItem(getIntent().getStringExtra(Key.MtbColtsKey)));

        this.initRecyclerView();

        setSupportActionBar(this.mBindings.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    public void initRecyclerView() {
        mBindings.listaColliMainList.setHasFixedSize(true);
        mBindings.listaColliMainList.setLayoutManager(new LinearLayoutManager(this));

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


        ListaColliMainListAdapter adapter = new ListaColliMainListAdapter(mtbColts)
                .setOnItemClickListener(mtbColtSelected -> {
                    this.mViewModel.dispatchMtbColt(mtbColtSelected, mFlagOnlyResiduo, this::startContenutoBancaleActivity);
                });

        adapter.setEmptyView(mBindings.listaBancaliEmptyView);
        mBindings.listaColliMainList.setAdapter(adapter);

        mBindings.elevatedToolbar.setRecyclerView(mBindings.listaColliMainList);
    }


    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    public void startContenutoBancaleActivity(MtbColt item){
        Intent myIntent = ContenutoBancaleActivity.createIntent(this, item, mCanRecoverUl.run(item), mPrinterType, mReportName);
        startActivityForResult(myIntent, PICK_UL_REQUEST);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Check which request we're responding to
        if (requestCode == PICK_UL_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {

                this.setResult(RESULT_OK, data);
                this.finish();
            }
        }
    }
}
