package io.github.deltajulio.pantrybank;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import io.github.deltajulio.pantrybank.R;
import io.github.deltajulio.pantrybank.data.Database;
import io.github.deltajulio.pantrybank.data.FoodItem;
import io.github.deltajulio.pantrybank.ui.MainFragmentListener;

public class MainActivity extends AppCompatActivity implements MainFragmentListener
{
    private static final String TAG = "MainActivity";

    /**
     * Firebase Objects
     */
    private FirebaseAuth auth;
    private FirebaseAuth.AuthStateListener authListener;
    private Database database;

    /**
     * View Objects
     */
    TabLayout tabLayout;
    TabItem pantryTab;
    TabItem listTab;
    TabItem recipeTab;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        pantryTab = (TabItem) findViewById(R.id.tab_pantry);
        listTab = (TabItem) findViewById(R.id.tab_list);
        recipeTab = (TabItem) findViewById(R.id.tab_recipes);

        // Verify that the user is logged in
        auth = FirebaseAuth.getInstance();

        if (auth.getCurrentUser() == null)
        {
            // User is not logged in, start the login activity
            Log.d(TAG, "onCreate: User is NOT logged in");
            OpenLoginScreen();
        } else
        {
            // User is logged in
            Log.d(TAG, "onCreate:" + auth.getCurrentUser().getEmail());
        }

        authListener = new FirebaseAuth.AuthStateListener()
        {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
            {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null)
                {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:logged_in:" + user.getUid());
                } else
                {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out:");
                    OpenLoginScreen();
                }
            }
        };

        // Create database handler
        database = new Database(auth.getCurrentUser().getUid());

        // Set up view pager
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new TabAdapter(getSupportFragmentManager(), MainActivity.this));
        tabLayout.setupWithViewPager(viewPager);

        // DEBUG
        FoodItem testItem = new FoodItem("eggs", false, FoodItem.QuantityType.NUMERICAL, 5, FoodItem.QuantityApprox.NULL);
        database.AddItem(testItem);
    }

    @Override
    public void onStart()
    {
        super.onStart();
        auth.addAuthStateListener(authListener);
    }

    @Override
    public void onStop()
    {
        super.onStop();
        if (authListener != null)
        {
            auth.removeAuthStateListener(authListener);
        }
    }

    private void OpenLoginScreen()
    {
        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public Database GetDatabase()
    {
        return database;
    }
}

@@ -2440,157 +2440,134 @@
 ));%0A
-                %7D else%0A                %7B%0A                    // User is signed out%0A                    Log.d(TAG, %22onAuthStateChanged:signed_out:%22
+%0A                    // Create database handler%0A                    database = new Database(auth.getCurrentUser().getUid()
 );%0A
+%0A
     
@@ -2582,34 +2582,24 @@
         
-OpenLoginScreen();
+// DEBUG
 %0A       
@@ -2611,79 +2611,147 @@
     
-%7D%0A
     
-        %7D%0A        %7D;%0A%0A        // Create database handler%0A
+FoodItem testItem = new FoodItem(%22eggs%22, false, FoodItem.QuantityType.NUMERICAL, 5, FoodItem.QuantityApprox.NULL);%0A            
         
 data
@@ -2742,32 +2742,34 @@
             
+//
 database
  = new Datab
@@ -2760,58 +2760,41 @@
 base
- = new Database(auth.getCurrentUser().getUid());%0A%0A
+.AddItem(testItem);%0A%0A            
     
@@ -2818,16 +2818,28 @@
 w pager%0A
+            
         
@@ -2910,16 +2910,28 @@
         
+       
+     
 viewPage
@@ -3004,24 +3004,36 @@
 ity.this));%0A
+            
         tabL
@@ -3073,183 +3073,237 @@
 r);%0A
-%0A        // DEBUG%0A        FoodItem testItem = new FoodItem(%22eggs%22, false, FoodItem.QuantityType.NUMERICAL, 5, FoodItem.QuantityApprox.NULL);%0A        database.AddItem(testItem)
+                %7D else%0A                %7B%0A                    // User is signed out%0A                    Log.d(TAG, %22onAuthStateChanged:signed_out:%22);%0A                    OpenLoginScreen();%0A                %7D%0A            %7D%0A        %7D
 ;%0A  

fix loading viewpager before database was initialized