package uprrp.tania.activities;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

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

import org.researchstack.backbone.ui.PinCodeActivity;

import uprrp.tania.R;
import uprrp.tania.fragments.AssessmentsFragment;
import uprrp.tania.fragments.ConsentFragment;
import uprrp.tania.fragments.WithdrawFragment;

public class MainActivity extends PinCodeActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        // Constructor stuff
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Create toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null; // TODO: find out if this causes crashes
        actionBar.setTitle(R.string.appName);
        actionBar.setDisplayShowTitleEnabled(true);


        // Set navigation buttons listener
        BottomNavigationView bottomNavigationMenu = findViewById(R.id.bottom_navigation_menu);
        bottomNavigationMenu.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                Fragment selectedFragment;

                int id = item.getItemId();
                if(id == R.id.nav_assessments) {
                    selectedFragment = new AssessmentsFragment();
                } else if(id == R.id.nav_consent) {
                    selectedFragment = new ConsentFragment();
                } else if(id == R.id.nav_settings) {
                    selectedFragment = new WithdrawFragment();
                } else {
                    Log.wtf(TAG, "Illegal fragment reach attempt with id: " + id);
                    selectedFragment = new Fragment(); // use empty fragment
                }

                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.mainFragment, selectedFragment)
                        .commit();

                return true;

            }
        });

    }

}
