package br.com.codetec.saudesabor.ui.login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.paypal.checkout.PayPalCheckout;
import com.paypal.checkout.config.CheckoutConfig;
import com.paypal.checkout.config.Environment;
import com.paypal.checkout.config.SettingsConfig;
import com.paypal.checkout.createorder.CurrencyCode;
import com.paypal.checkout.createorder.UserAction;

import br.com.codetec.saudesabor.BuildConfig;
import br.com.codetec.saudesabor.R;
import br.com.codetec.saudesabor.paypalpayment.PaypalclientIDConfigClass;
import br.com.codetec.saudesabor.service.CarrinhoComprasGlobal;
import br.com.codetec.saudesabor.utils.SharedPreferenceUtil;
import br.com.codetec.saudesabor.utils.components.alert.CustomAlert;

public class DashboardActivity extends AppCompatActivity {

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

        CheckoutConfig config = new CheckoutConfig(
                this.getApplication(),
                PaypalclientIDConfigClass.PAYPAL_CLIENT_ID,
                Environment.SANDBOX,
                String.format("%s://paypalpay", BuildConfig.APPLICATION_ID),
                CurrencyCode.JPY,
                UserAction.PAY_NOW,
                new SettingsConfig(
                        true,
                        false
                )
        );
        PayPalCheckout.setConfig(config);

        setContentView(R.layout.activity_dashboard);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
//        setSupportActionBar((Toolbar) findViewById(R.id.my_toolbar));

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_dashboard, R.id.navigation_carrinho)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        new CarrinhoComprasGlobal(DashboardActivity.this).carregarNaSessao();
    }


    @Override
    protected void onResume() {
       super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.bottom_nav_menu, menu);
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.navigation_dashboard :
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {
        CustomAlert.getInstance().show(
                this, getString(R.string.app_name),
                getString(R.string.escolha),
                getString(R.string.encerrar_aplicacao),
                getString(R.string.fazer_logout),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finishAffinity();
                        System.exit(0);
                    }
                },
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferenceUtil.setStringPreference(DashboardActivity.this, SharedPreferenceUtil.LOGIN_ULTIMO,null);
                        SharedPreferenceUtil.setStringPreference(DashboardActivity.this, SharedPreferenceUtil.SENHA_ULTIMO,null);
                        DashboardActivity.super.onBackPressed();
                    }
                }
        );
    }
}
