package com.example.acer.agendita_mysql_c;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import librerias.Parametros;

public class PrincipalActivity extends AppCompatActivity {


    FragmentManager manager;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    manager.beginTransaction().replace(R.id.contenedorFragmentos, new PortadaFragment()).commit();
                    return true;
                case R.id.navigation_altas:

                        manager.beginTransaction().replace(R.id.contenedorFragmentos, new AltasFragment()).commit();
                    return true;
                case R.id.navigation_bajas:

                    manager.beginTransaction().replace(R.id.contenedorFragmentos, new BajasFragment()).commit();
                    return true;
                case R.id.navigation_cambios:

                    manager.beginTransaction().replace(R.id.contenedorFragmentos, new CambiosFragment()).commit();
                    return true;
                case R.id.navigation_consultas:
                    manager.beginTransaction().replace(R.id.contenedorFragmentos, new ConsultasFragment()).commit();
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.contenedorFragmentos, new PortadaFragment()).commit();
            if(Parametros.tipoUsuario.equals("Limitado")){
                navigation.getMenu().findItem(R.id.navigation_altas).setVisible(false);
                navigation.getMenu().findItem(R.id.navigation_bajas).setVisible(false);
                navigation.getMenu().findItem(R.id.navigation_cambios).setVisible(false);

            }
    }
    public boolean onCreateOptionsMenu(Menu menu){
        if(Parametros.tipoUsuario.equals("Admin")) {
            getMenuInflater().inflate(R.menu.options_menu, menu);
        }
            return true;

    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.MenuNuevoUsuario:
                manager.beginTransaction().replace(R.id.contenedorFragmentos, new NuevoUsuarioFragment()).commit();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void onBackPressed(){
        //super.onBackPressed();

        AlertDialog.Builder dialogo=
                new AlertDialog.Builder(PrincipalActivity.this );
        dialogo.setTitle("Salir de la Aplicación");
        dialogo.setMessage("¿Deseas Salir de la Aplicación o Cerrar Sesión?");
        dialogo.setCancelable(false);
        dialogo.setPositiveButton("Salir App", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        dialogo.setNegativeButton("Cerrar Sesion", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent miIntento=new Intent(getApplicationContext(),InicioActivity.class);
                startActivity(miIntento);
                finish();
            }
        });
        AlertDialog dialogos=dialogo.create();
        dialogos.show();
    }
}
