package com.jachdev.consumerprotection.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.jachdev.commonlibs.base.BaseActivity;
import com.jachdev.consumerprotection.R;
import com.jachdev.consumerprotection.ui.calories.PedometerActivity;
import com.jachdev.consumerprotection.ui.camera.CameraActivity;
import com.jachdev.consumerprotection.ui.dashboard.DashboardFragment;
import com.jachdev.consumerprotection.ui.home.HomeFragment;
import com.jachdev.consumerprotection.ui.prediction.ContactUsFragment;
import com.jachdev.consumerprotection.util.SessionManager;

import org.jetbrains.annotations.NotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import butterknife.BindView;

public class HomeActivity extends BaseActivity implements HomeFragment.FragmentEventListener, DashboardFragment.FragmentEventListener{

    private static final String TAG = HomeActivity.class.getSimpleName();
    @BindView(R.id.nav_view)
    BottomNavigationView navigationView;

    private NavController navController;

    @Override
    protected int layoutRes() {
        return R.layout.activity_home;
    }

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

        navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        navController.addOnDestinationChangedListener(mOnDestinationChangedListener);

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        SessionManager.getInstance().logoutSession();

        activityToActivity(LoginActivity.class);
        HomeActivity.this.finish();

        return super.onOptionsItemSelected(item);
    }

    private NavController.OnDestinationChangedListener mOnDestinationChangedListener = new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull @NotNull NavController controller, @NonNull @NotNull NavDestination destination, @Nullable @org.jetbrains.annotations.Nullable Bundle arguments) {
            switch (controller.getCurrentDestination().getId()){
                case R.id.navigation_home:
                    navigationView.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

    @Override
    public void onNearestStocksClick() {
    }

    @Override
    public void onNearestShopsClick() {
    }

    @Override
    public void onImportClick() {
    }

    @Override
    public void onPriceClick() {
    }

    @Override
    public void onSaleClick() {
    }

    @Override
    public void onComplaintsClick() {
    }

    @Override
    public void onContactUsClick() {
    }

    @Override
    public void onImageRecognitionClick() {
        Intent intent = new Intent(HomeActivity.this, CameraActivity.class);
        intent.setAction(ContactUsFragment.ACTION);
        activityToActivity(intent, 0);
    }

    @Override
    public void onCalorieCounterClick() {
        Intent intent = new Intent(HomeActivity.this, PedometerActivity.class);
        intent.setAction(ContactUsFragment.ACTION);
        activityToActivity(intent, 0);
    }

    @Override
    public void onMealPlanClick() {

    }
}