package com.example.navigationdrawer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
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 androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    Toolbar toolbar;
    FloatingActionButton fab;
    NavigationView navigationView;
   DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=findViewById(R.id.toolbar);
        navigationView=findViewById(R.id.design_navigation_view);
        fab=findViewById(R.id.fab);
        drawerLayout=findViewById(R.id.drawer);

        setSupportActionBar(toolbar);

        // header code

        View header_layout=navigationView.getHeaderView(0);
        TextView textView_name=  header_layout.findViewById(R.id.textView);
        ImageView imageView_pic=  header_layout.findViewById(R.id.imageView);
        textView_name.setText("zonaira");
        imageView_pic.setImageResource(R.mipmap.ic_launcher);

        //click event for fab button
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                     //logic
            }
        });

    // Action bar
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
            drawerLayout.addDrawerListener(toggle);
            toggle.syncState();

    navigationView.setNavigationItemSelectedListener(this);

    if(savedInstanceState==null)
    {
        Fragment fragment=new Fragmentone();
        navigationView.getMenu().getItem(0).setChecked(true);
        FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.flContent,fragment).commit();

    }

    }

    @Override
    public void onBackPressed() {super.onBackPressed();
        if (drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id=menuItem.getItemId();
        Fragment fragment;
        switch(id){
            case R.id.home:
                fragment  =new Fragment_two();
           default:
                fragment  =new Fragmentone();
        }
        FragmentManager manager=getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.flContent,fragment).commit();

        navigationView.setCheckedItem(id);
        drawerLayout.closeDrawer(GravityCompat.START);

        return false;
    }



}
