package com.hicorenational.antifraud;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hicorenational.antifraud.views.fragment.DashboardFragment;
import com.hicorenational.antifraud.views.fragment.HomeFragment;
import com.hicorenational.antifraud.views.fragment.MineFragment;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

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

        BottomNavigationView navigationView = findViewById(R.id.nav_view);
        //        监听事件
        navigationView.setOnItemSelectedListener(item -> {
            resetIcon(navigationView);
            if (item.getItemId() == R.id.navigation_home) {
                item.setIcon(R.drawable.ic_home_seled);
                getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new HomeFragment()).commit();
                return true;
            } else if (item.getItemId() == R.id.navigation_dashboard) {
                item.setIcon(R.drawable.ic_dashboard_seled);
                getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new DashboardFragment()).commit();
                return true;
            } else if (item.getItemId() == R.id.navigation_notifications) {
                item.setIcon(R.drawable.ic_mine_seled);
                getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new MineFragment()).commit();
                return true;
            }
            return false;
        });
    }

    private void resetIcon(BottomNavigationView navView) {
        MenuItem home = navView.getMenu().findItem(R.id.navigation_home);
        MenuItem dashboard = navView.getMenu().findItem(R.id.navigation_dashboard);
        MenuItem mine = navView.getMenu().findItem(R.id.navigation_notifications);

        home.setIcon(R.drawable.ic_home_unseled);
        dashboard.setIcon(R.drawable.ic_dashboard_unseled);
        mine.setIcon(R.drawable.ic_mine_unseled);
    }
}