package it.integry.integrywmsnative.gest.vendita_ordine_inevaso;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeCallbackDTO;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.databinding.ActivityVenditaOrdineInevasoBinding;
import it.integry.integrywmsnative.databinding.FragmentArticoliInColloBottomSheetBinding;
import it.integry.integrywmsnative.gest.lista_bancali.ListaBancaliActivity;
import it.integry.integrywmsnative.gest.vendita.dto.OrdineVenditaGroupedInevasoDTO;
import it.integry.integrywmsnative.gest.vendita.dto.PickingObjectDTO;
import it.integry.integrywmsnative.gest.vendita_ordine_inevaso.viewmodel.VenditaOrdineInevasoViewModel;
import it.integry.integrywmsnative.view.bottomsheet.viewmodel.ArticoliInColloBottomSheetViewModel;

public class VenditaOrdineInevasoActivity extends AppCompatActivity {


    public ActivityVenditaOrdineInevasoBinding bindings;
    private VenditaOrdineInevasoViewModel mVenditaOrdineInevasoViewModel;
    private ArticoliInColloBottomSheetViewModel mArticoliInColloBottomSheetViewModel;


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

    private int barcodeScannerIstanceID = -1;

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

        bindings = DataBindingUtil.setContentView(this, R.layout.activity_vendita_ordine_inevaso);
        FragmentArticoliInColloBottomSheetBinding bindings = this.bindings.bottomSheetInclude;
        mArticoliInColloBottomSheetViewModel = new ArticoliInColloBottomSheetViewModel(this, bindings);

        List<PickingObjectDTO> pickingList = (ArrayList<PickingObjectDTO>)getIntent().getSerializableExtra("keyPickingList");
        List<OrdineVenditaGroupedInevasoDTO> testateOrdini = (ArrayList<OrdineVenditaGroupedInevasoDTO>)getIntent().getSerializableExtra("keyTestateOrdini");
        List<MtbColt> colliRegistrati = (ArrayList<MtbColt>) getIntent().getSerializableExtra("keyColliRegistrati");
        mVenditaOrdineInevasoViewModel = new VenditaOrdineInevasoViewModel(
                this, mArticoliInColloBottomSheetViewModel, pickingList, testateOrdini, colliRegistrati);

        setSupportActionBar(this.bindings.toolbar);

        setTitle(R.string.activity_vendita_inevaso_title);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(mVenditaOrdineInevasoViewModel.onScanSuccessfull)
                .setOnScanFailed(ex -> UtilityExceptions.defaultException(this, ex, false)));
    }

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

    @Override
    public void onBackPressed() {
        if(mArticoliInColloBottomSheetViewModel.isExpanded()){
            mArticoliInColloBottomSheetViewModel.collapse();
        } else if(mVenditaOrdineInevasoViewModel.thereIsAnOpenedUL()) {
            mArticoliInColloBottomSheetViewModel.closeCurrentUL();

            BarcodeManager.removeCallback(barcodeScannerIstanceID);
            super.onBackPressed();
        } else {
            BarcodeManager.removeCallback(barcodeScannerIstanceID);
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_vendita_ordine_inevaso, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void startListaBancaliRegistratiActivity(ArrayList<MtbColt> mtbColts){
        Intent myIntent = new Intent(this, ListaBancaliActivity.class);
        myIntent.putExtra("key", mtbColts); //Optional parameters
        myIntent.putExtra(ListaBancaliActivity.Key.CanRecoverUL, true);
        this.startActivityForResult(myIntent, PICK_UL_REQUEST);
    }


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

                MtbColt recoveredMtbColt = data.getParcelableExtra("key");

                mVenditaOrdineInevasoViewModel.recoverUL(recoveredMtbColt);
            }
        }
    }

}
