package com.averi.worldscribe.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;

import com.averi.worldscribe.Category;
import com.averi.worldscribe.R;
import com.averi.worldscribe.adapters.ConnectionsAdapter;
import com.averi.worldscribe.utilities.AppPreferences;
import com.averi.worldscribe.views.BottomBar;

/**
 * Created by mark on 05/07/16.
 *
 * ArticleActivity is the superclass for each Activity that displays a certain Category of Articles.
 *
 * All subclasses must implement {@link #getLayoutResourceID} using the Layout Resource file
 * corresponding to their respective Categories, as well as the other abstract methods for
 * obtaining specific Views.
 */
public abstract class ArticleActivity extends AppCompatActivity {

    /**
     * The BottomBar navigation View.
     */
    private BottomBar bottomBar;
    /**
     * The name of the World in which this Article exists.
     */
    private String worldName;
    /**
     * The {@link Category} this Article belongs to.
     */
    private Category category;
    /**
     * The name of the Article displayed by this Activity.
     */
    private String articleName;
    /**
     * Contains cards for all of the Article's {@link com.averi.worldscribe.Connection}s.
     */
    private RecyclerView connectionsList;

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

        bottomBar = getBottomBar();
        Intent intent = getIntent();
        worldName = intent.getStringExtra(AppPreferences.WORLD_NAME);
        category = (Category) intent.getSerializableExtra(AppPreferences.CATEGORY);
        articleName = intent.getStringExtra(AppPreferences.ARTICLE_NAME);
        connectionsList = getConnectionsRecycler();

        setUpArticleCore();
    }

    /**
     * Load data pertaining to the selected Article, and use it in set-up processes that are common
     * to Article Activities of all Categories.
     */
    private void setUpArticleCore() {
        setAppBar();
        bottomBar.highlightCategoryButton(this, Category.Person);
        populateConnections();
    }

    /**
     * @return The Android Resource ID of this Activity's layout resource file.
     */
    protected abstract int getLayoutResourceID();

    /**
     * @return The bottom navigation bar for this Activity.
     */
    protected abstract BottomBar getBottomBar();

    /**
     * @return The RecyclerView for this Article's
     * {@link com.averi.worldscribe.Connection Connection}s.
     */
    protected abstract RecyclerView getConnectionsRecycler();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.article_menu, menu);
        return true;
    }

    /**
     * Set this Activity's toolbar and set the title to be the Article's name.
     */
    private void setAppBar() {
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle(articleName);
    }

    /**
     * Populate the Connections RecyclerView with cards for this Article's
     * {@link com.averi.worldscribe.Connection Connection}s.
     */
    private void populateConnections() {
        connectionsList.setLayoutManager(new LinearLayoutManager(this));
        connectionsList.setAdapter(new ConnectionsAdapter(this, worldName, category, articleName));
    }
}

@@ -2108,335 +2108,8 @@
  %7D%0A%0A
-    /**%0A     * Load data pertaining to the selected Article, and use it in set-up processes that are common%0A     * to Article Activities of all Categories.%0A     */%0A    private void setUpArticleCore() %7B%0A        setAppBar();%0A        bottomBar.highlightCategoryButton(this, Category.Person);%0A        populateConnections();%0A    %7D%0A%0A
     
@@ -2192,16 +2192,16 @@
 e file.%0A
+
      */%0A
@@ -2576,193 +2576,322 @@
     
-@Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        MenuInflater inflater = getMenuInflater();%0A        inflater.inflate(R.menu.article_menu, menu);%0A        return true
+/**%0A     * Load data pertaining to the selected Article, and use it in set-up processes that are common%0A     * to Article Activities of all Categories.%0A     */%0A    private void setUpArticleCore() %7B%0A        setAppBar();%0A        bottomBar.highlightCategoryButton(this, Category.Person);%0A        populateConnections()
 ;%0A  
@@ -3602,16 +3602,215 @@
 Name));%0A
-
     %7D%0A
+%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        MenuInflater inflater = getMenuInflater();%0A        inflater.inflate(R.menu.article_menu, menu);%0A        return true;%0A    %7D%0A%0A
 %7D%0A

Reorganize methods in ArticleActivity