package com.momrach.basicnavigationdrawer.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;

import com.google.android.material.navigation.NavigationView;
import com.momrach.basicnavigationdrawer.R;
import com.momrach.basicnavigationdrawer.fragments.DefaultFragment;
import com.momrach.basicnavigationdrawer.fragments.FritosFragment;
import com.momrach.basicnavigationdrawer.fragments.GuisosFragment;
import com.momrach.basicnavigationdrawer.fragments.PastasFragment;
import com.momrach.basicnavigationdrawer.fragments.PostresFragment;
import com.momrach.basicnavigationdrawer.fragments.SopasFragment;


public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setToolbar();
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nvNavigationView);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                boolean doChange=false;
                Fragment fragment = null;
                switch (menuItem.getItemId()){
                    case R.id.itFritos:
                        doChange=true;
                        fragment = new FritosFragment();
                        break;
                    case R.id.itGuisos:
                        doChange=true;
                        fragment = new GuisosFragment();
                        break;
                    case R.id.itPastas:
                        doChange=true;
                        fragment = new PastasFragment();
                        break;
                    case R.id.itPostres:
                        doChange=true;
                        fragment = new PostresFragment();
                        break;
                    case R.id.itSopas:
                        doChange=true;
                        fragment = new SopasFragment();
                        break;
                }
                if (doChange){
                    uncheckItems(navigationView);
                    changeFragment(fragment,menuItem);
                }
                return true;
            }
        });

        setDefaultFragment();
    }

    private void uncheckItems(NavigationView navigationView){
        int size = navigationView.getMenu().size();
        for (int i = 0; i < size; i++) {
            navigationView.getMenu().getItem(i).setChecked(false);
        }
    }

    private void setDefaultFragment(){
        changeFragment(new DefaultFragment(), null);
        getSupportActionBar().setTitle(R.string.nombre_app);
    }
    private void changeFragment(Fragment fragment, MenuItem item){
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame,fragment)
                .commit();
        if (item!=null){
            item.setChecked(true);
            getSupportActionBar().setTitle(item.getTitle());
        }
        drawerLayout.closeDrawers();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                // Código necesario para desplegar el Navigation Drawer.
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setToolbar(){
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


}
