package com.kolobochki.memory;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.room.Room;

import com.kolobochki.memory.databinding.ActivityMainBinding;

import com.kolobochki.memory.AppDatabase;
import com.kolobochki.memory.GameRepository;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private NavController navController;

    private GameRepository gameRepository;
    private static AppDatabase database;


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

        database = Room.databaseBuilder(getApplicationContext(),
                        AppDatabase.class, "memory-db")
                .fallbackToDestructiveMigration()
                .build();

        gameRepository = new GameRepository(database.levelRecordDao());

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home,
                R.id.navigation_dashboard,
                R.id.navigation_task1,
                R.id.navigation_task2,
                R.id.navigation_task3,
                R.id.navigation_task4,
                R.id.navigation_info,
                R.id.navigation_records)
                .build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
            if (destination.getId() == R.id.navigation_task1
             || destination.getId() == R.id.navigation_task2
             || destination.getId() == R.id.navigation_task3
             || destination.getId() == R.id.navigation_task4
             || destination.getId() == R.id.navigation_info
             || destination.getId() == R.id.navigation_records) {
                binding.navView.setVisibility(View.GONE);
            } else {
                binding.navView.setVisibility(View.VISIBLE);
            }
        });

    }

    public GameRepository getGameRepository() {
        return gameRepository;
    }

    public static AppDatabase getDatabase() {
        return database;
    }

    @Override
    protected void onDestroy() {
        if (database != null && database.isOpen()) {
            database.close();
        }
        super.onDestroy();
    }

    @Override
    public boolean onSupportNavigateUp() {
        return navController.navigateUp() || super.onSupportNavigateUp();
    }
}