package it.integry.integrywmsnative.gest.accettazione_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.ActivityAccettazioneOrdineInevasoBinding;
import it.integry.integrywmsnative.databinding.FragmentArticoliInColloBottomSheetBinding;
import it.integry.integrywmsnative.gest.accettazione.dto.OrdineAccettazioneDTO;
import it.integry.integrywmsnative.gest.accettazione_ordine_inevaso.viewmodel.AccettazioneOnOrdineAccettazioneInevasoViewModel;
import it.integry.integrywmsnative.gest.lista_bancali.ListaBancaliActivity;
import it.integry.integrywmsnative.view.bottomsheet.viewmodel.ArticoliInColloBottomSheetViewModel;

public class AccettazioneOrdineInevasoActivity extends AppCompatActivity {

    public ActivityAccettazioneOrdineInevasoBinding bindings;
    private AccettazioneOnOrdineAccettazioneInevasoViewModel mAccettazioneOrdineInevasoViewModel;
    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_accettazione_ordine_inevaso);
        FragmentArticoliInColloBottomSheetBinding bindings = this.bindings.bottomSheetInclude;
        mArticoliInColloBottomSheetViewModel = new ArticoliInColloBottomSheetViewModel(this, bindings);

        List<OrdineAccettazioneDTO> orders = (ArrayList<OrdineAccettazioneDTO>)getIntent().getSerializableExtra("key");
        mAccettazioneOrdineInevasoViewModel = new AccettazioneOnOrdineAccettazioneInevasoViewModel(
                this, mArticoliInColloBottomSheetViewModel, orders);

        setSupportActionBar(this.bindings.toolbar);

        setTitle(orders);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        barcodeScannerIstanceID = BarcodeManager.addCallback(new BarcodeCallbackDTO()
                .setOnScanSuccessfull(mAccettazioneOrdineInevasoViewModel.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(mAccettazioneOrdineInevasoViewModel.thereIsAnOpenedUL()) {
            mArticoliInColloBottomSheetViewModel.closeCurrentUL();
        } 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_accettazione_ordine_inevaso, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_filter:
                mAccettazioneOrdineInevasoViewModel.showOrderDialog();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    private void setTitle(List<OrdineAccettazioneDTO> ordini){
        if(ordini.size() > 0){
            if(ordini.size() == 1){
                setTitle(ordini.get(0).ragSoc);
            } else {
                setTitle(R.string.accettazione_ordine_inevaso_title_multiple_order);
            }
        } else {
            setTitle(R.string.accettazione_ordine_inevaso_title_empty);
        }
    }



    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");

                mAccettazioneOrdineInevasoViewModel.recoverUL(recoveredMtbColt);
            }
        }
    }



}
