/*************************************************************
 * By: Coralys Cubero Rivera
 * Date: 2019
 *************************************************************/

package uprrp.tania;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
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.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

import org.researchstack.backbone.ui.PinCodeActivity;


public class MainActivity extends PinCodeActivity {

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

        // If the application was opened using the URL scheme
        Intent intent = getIntent();
        if (intent != null && intent.getData() != null) {
            Uri uri = intent.getData();
            String expToken = null;
            if (uri != null) {
                expToken = uri.getQueryParameter("expToken");
            }
            Log.d("URL SCHEME", "The experience token is " + expToken);
            //SendActivateExperienceToServer activateExperience = new SendActivateExperienceToServer();
            //activateExperience.execute(expToken);
        }
        /////////////////////////////////////////////////////////////////////////////

        // We need to know if the user has already registered, otherwise they need to do that first
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean needsToRegister = prefs.getBoolean("needsToRegister", true);

        if (needsToRegister){
           Intent needsRegisterIntent = new Intent(this, GettingStarted.class);
            startActivity(needsRegisterIntent);
         }
        /////////////////////////////////////////////////////////////////////////////


        //////////////////// Let's get NOTIFICATIONS //////////////////////////
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
                    @Override
                    public void onSuccess(InstanceIdResult instanceIdResult) {
                        device_token = instanceIdResult.getToken();
                    }
                });

        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "us-east-1:574094cd-0784-4e26-bd14-4fa72ae63579", // Identity pool ID
                Regions.US_EAST_1 // Region
        );

        SNSRegister snsRegister = new SNSRegister(credentialsProvider);
        snsRegister.execute();
        ////////////////////////////////////////////////////////////////


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setTitle("TANIA");
        actionBar.setDisplayShowTitleEnabled(true);

        BottomNavigationView bottomNavigationMenu = findViewById(R.id.bottom_navigation_menu);
        bottomNavigationMenu.setOnNavigationItemSelectedListener(navListener);


    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = new Fragment();

                    switch (item.getItemId()) {
                        case R.id.nav_assessments:
                            selectedFragment = new AssessmentsFragment();
                            break;
                        case R.id.nav_consent:
                            selectedFragment = new ConsentFragment();
                            break;
                        case R.id.nav_settings:
                            selectedFragment = new WithdrawFragment();
                            break;
                    }

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

                    return true;
                }
            };

}











