package br.com.mobileti.mynotes.activitys;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseListAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import br.com.mobileti.mynotes.R;
import br.com.mobileti.mynotes.fragments.MainFragment;
import br.com.mobileti.mynotes.objects.Category;
import br.com.mobileti.mynotes.persistence.FirebaseContract;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();
    private FirebaseListAdapter<Category> categoryAdapter;
    @BindView(R.id.drawer_layout) DrawerLayout drawer;
    @BindView(R.id.topToolbar) Toolbar toolbar;
    @BindView(R.id.emailTextView) TextView emailTextView;
    @BindView(R.id.nameTextView) TextView nameTextView;
    @BindView(R.id.categorysDrawerListView) ListView listView;
    private MainFragment mainFragment;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseAuth mAuth;

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

        ButterKnife.bind(this);

        setSupportActionBar(toolbar);

        if (CheckLoginActivity.user != null) {
            // User is signed in
            emailTextView.setText(CheckLoginActivity.user.getEmail());
            nameTextView.setText(CheckLoginActivity.user.getDisplayName());
        } else {
            finish();
        }

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference dbRef = database.getReference(CheckLoginActivity.user.getUid()).child(FirebaseContract.CATEGORY_CHILD);

        listView = (ListView) findViewById(R.id.categorysDrawerListView);

        mainFragment = new MainFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.mainFragment, mainFragment);
        ft.commit();


        categoryAdapter = new FirebaseListAdapter<Category>(this, Category.class, R.layout.category_drawer_item, dbRef) {
            @Override
            protected void populateView(View v, final Category model, final int position) {
                ((TextView)v.findViewById(R.id.categoryNameTextView)).setText(model.getName());

                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showNotes(model);
                    }
                });
            }
        };

        listView.setAdapter(categoryAdapter);
        View header = getLayoutInflater().inflate(R.layout.category_listview_header, null);
        listView.addHeaderView(header);

        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getBaseContext(), CategoryActivity.class);
                startActivity(it);
            }
        });

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

    }

    @OnClick(R.id.navHeader)
    public void headerLayoutClick() {
        Intent it = new Intent(this, EditProfileActivity.class);
        startActivity(it);
    }

    public void allNotesClick(View view) {
        showNotes(new Category("", ""));
    }

    public void showNotes(Category category) {
        drawer.closeDrawer(GravityCompat.START);
        Bundle catBundle = new Bundle();
        catBundle.putParcelable(Category.CATEGORIE_ARG, category);

        mainFragment = new MainFragment();
        mainFragment.setArguments(catBundle);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.mainFragment, mainFragment);
        ft.commit();

        if(!category.getName().isEmpty()) {
            toolbar.setSubtitle(category.getName());
        } else {
            toolbar.setSubtitle("");
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        categoryAdapter.cleanup();
    }
}
