package it.integry.integrywmsnative.gest.contenuto_bancale.viewmodel;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import androidx.databinding.ObservableField;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.SpannableString;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.REST.consumers.DepositoRESTConsumer;
import it.integry.integrywmsnative.core.REST.consumers.ISimpleOperationCallback;
import it.integry.integrywmsnative.core.REST.consumers.PrinterRESTConsumer;
import it.integry.integrywmsnative.core.di.BindableString;
import it.integry.integrywmsnative.core.interfaces.IRecyclerItemClicked;
import it.integry.integrywmsnative.core.model.MtbColr;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.report.ReportManager;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.gest.contenuto_bancale.ContenutoBancaleActivity;
import it.integry.integrywmsnative.gest.contenuto_bancale.core.ContenutoBancaleListAdapter;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;

import static android.app.Activity.RESULT_OK;

public class ContenutoBancaleViewModel implements IRecyclerItemClicked<MtbColr> {

    private ContenutoBancaleActivity mContext;

    public ObservableField<MtbColt> mtbColt;
    public BindableString descrizioneDepo = new BindableString();

    public ObservableField<Boolean> isFabVisible = new ObservableField<>();
    public ObservableField<Boolean> isRecoverFabMenuVisible = new ObservableField<>();

    private ContenutoBancaleListAdapter mAdapter;

    public ContenutoBancaleViewModel(ContenutoBancaleActivity context, MtbColt mtbColt, boolean canRecoverUL){
        mContext = context;
        this.mtbColt = new ObservableField<>(mtbColt);
        this.isFabVisible.set(true);
        this.isRecoverFabMenuVisible.set(canRecoverUL);

        mContext.bindings.setViewModel(this);

        initRecyclerView();

        initColloInfo();
    }


    private void initRecyclerView() {
        mContext.bindings.contenutoBancaleMainList.setNestedScrollingEnabled(false);

        mContext.bindings.contenutoBancaleMainList.setHasFixedSize(true);

        mContext.bindings.contenutoBancaleMainList.setLayoutManager(new LinearLayoutManager(mContext));

        DividerItemDecoration itemDecorator = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecorator.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.divider));
        mContext.bindings.contenutoBancaleMainList.addItemDecoration(itemDecorator);

        mAdapter = new ContenutoBancaleListAdapter(mContext, mtbColt.get().getMtbColr(), this);
        mContext.bindings.contenutoBancaleMainList.setAdapter(mAdapter);
    }


    private void initColloInfo() {
        DepositoRESTConsumer.getDepoByCodMdep(mtbColt.get().getCodMdep(), depo -> {
            descrizioneDepo.set("(" + depo.getDescrizione() + ")");
        });
    }

    @Override
    public void onItemClick(MtbColr item, int position) {
    }



    public void recoverUL(){
        Intent data = new Intent();
        data.putExtra("key", mtbColt.get());

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


    public void printUL() {
        mContext.bindings.contenutoBancaleFab.close(true);

        final ProgressDialog progress = ProgressDialog.show(mContext, mContext.getText(R.string.waiting),
                mContext.getText(R.string.loading) + " ...", true);

        PrinterRESTConsumer.getAvailablePrinters(SettingsManager.i().userSession.depo.getCodMdep(), new ISimpleOperationCallback<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {

                if(value.size() > 0) {

                    try {
                        ReportManager.getRightReportNameByMtbColt(mtbColt.get(), reportName -> {

                            PrinterRESTConsumer.printCollo(value.get(0),
                                    mtbColt.get(),
                                    1,
                                    reportName,
                                    () -> {
                                        progress.dismiss();

                                        Resources res = mContext.getResources();
                                        String errorMessage = res.getText(R.string.alert_print_completed_message).toString();
                                        DialogSimpleMessageHelper.makeSuccessDialog(mContext, res.getText(R.string.completed).toString(), new SpannableString(errorMessage), null, null).show();
                                    }, ex -> UtilityExceptions.defaultException(mContext,ex, progress));

                        }, ex -> UtilityExceptions.defaultException(mContext, ex, progress)
                        );
                    } catch (Exception ex){
                        UtilityExceptions.defaultException(mContext, ex, progress);
                    }

                } else {
                    progress.dismiss();
                    String errorMessage = "Nessuna stampante configurata";
                    DialogSimpleMessageHelper.makeWarningDialog(mContext, new SpannableString(errorMessage), null, null).show();
                }
            }

            @Override
            public void onFailed(Exception ex) {
                UtilityExceptions.defaultException(mContext, ex, progress);
            }
        });

    }

}
