package com.averi.worldscribe.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.averi.worldscribe.Category;
import com.averi.worldscribe.R;
import com.averi.worldscribe.utilities.AttributeGetter;

public class BottomBar extends RelativeLayout {

    private Animation slideOutAnimation;
    private Animation slideInAnimation;

    private LayoutInflater inflater;
    private BottomBarActivity activity;

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!(isInEditMode())) {
            this.activity = (BottomBarActivity) context;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.activity_bottom_bar, this, true);

            slideOutAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_out_bottom);
            slideInAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);

            this.findViewById(R.id.peopleButton).setOnClickListener(peopleOnClickListener);
            this.findViewById(R.id.groupsButton).setOnClickListener(groupsOnClickListener);
            this.findViewById(R.id.placesButton).setOnClickListener(placesOnClickListener);
            this.findViewById(R.id.itemsButton).setOnClickListener(itemsOnClickListener);
            this.findViewById(R.id.conceptsButton).setOnClickListener(conceptsOnClickListener);
        }
    }

    private OnClickListener peopleOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            activity.respondToBottomBarButton(Category.Person);
        }
    };

    private OnClickListener groupsOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            activity.respondToBottomBarButton(Category.Group);
        }
    };

    private OnClickListener placesOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            activity.respondToBottomBarButton(Category.Place);
        }
    };

    private OnClickListener itemsOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            activity.respondToBottomBarButton(Category.Item);
        }
    };

    private OnClickListener conceptsOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            activity.respondToBottomBarButton(Category.Concept);
        }
    };

    public void highlightCategoryButton(Context context, Category category) {
        LinearLayout categoryButton;

        unhighlightAllButtons(context);

        switch (category) {
            case Person:
                categoryButton = (LinearLayout) findViewById(R.id.peopleButton);
                break;
            case Group:
                categoryButton = (LinearLayout) findViewById(R.id.groupsButton);
                break;
            case Place:
                categoryButton = (LinearLayout) findViewById(R.id.placesButton);
                break;
            case Item:
                categoryButton = (LinearLayout) findViewById(R.id.itemsButton);
                break;
            case Concept:
            default:
                categoryButton = (LinearLayout) findViewById(R.id.conceptsButton);
        }

        int selectedButtonColor = AttributeGetter.getColorAttribute(context, R.attr.colorPrimaryDark);
        categoryButton.setBackgroundColor(selectedButtonColor);
    }

    /**
     * Changes the background color of all buttons to their normal, unhighlighted color.
     * @param context The Context containing this BottomBar.
     */
    private void unhighlightAllButtons(Context context) {
        int normalButtonColor = AttributeGetter.getColorAttribute(context, R.attr.colorPrimary);
        (findViewById(R.id.peopleButton)).setBackgroundColor(normalButtonColor);
        (findViewById(R.id.groupsButton)).setBackgroundColor(normalButtonColor);
        (findViewById(R.id.placesButton)).setBackgroundColor(normalButtonColor);
        (findViewById(R.id.itemsButton)).setBackgroundColor(normalButtonColor);
        (findViewById(R.id.conceptsButton)).setBackgroundColor(normalButtonColor);
    }

    /**
     * Translates the bar down off the bottom edge of the screen so that it is not visible.
     */
    public void slideOut() {
        this.startAnimation(slideOutAnimation);
        this.setVisibility(GONE);
    }

    /**
     * Translates the bar up from the bottom edge of the screen so that it is visible.
     */
    public void slideIn() {
        this.setVisibility(VISIBLE);
        this.startAnimation(slideInAnimation);
    }

}
@@ -691,16 +691,172 @@
 ctivity;
+%0A    private View peopleButton;%0A    private View groupsButton;%0A    private View placesButton;%0A    private View itemsButton;%0A    private View conceptsButton;
 %0A%0A    pu
@@ -1395,24 +1395,327 @@
 m_bottom);%0A%0A
+            peopleButton = findViewById(R.id.peopleButton);%0A            groupsButton = findViewById(R.id.groupsButton);%0A            placesButton = findViewById(R.id.placesButton);%0A            itemsButton = findViewById(R.id.itemsButton);%0A            conceptsButton = findViewById(R.id.conceptsButton);%0A%0A
             
@@ -2177,24 +2177,875 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Gets the View acting as the button for a specific Category.%0A     * @param category The Category whose button will be retrieved%0A     * @return The button View corresponding to the given Category%0A     */%0A    private View getCategoryButton(Category category) %7B%0A        View categoryButton;%0A%0A        switch (category) %7B%0A            case Person:%0A                categoryButton = peopleButton;%0A                break;%0A            case Group:%0A                categoryButton = groupsButton;%0A                break;%0A            case Place:%0A                categoryButton = placesButton;%0A                break;%0A            case Item:%0A                categoryButton = itemsButton;%0A                break;%0A            case Concept:%0A            default:%0A                categoryButton = conceptsButton;%0A        %7D%0A%0A        return categoryButton;%0A    %7D%0A%0A
     private 
@@ -4115,757 +4115,99 @@
     
-LinearLayout categoryButton;%0A%0A        unhighlightAllButtons(context);%0A%0A        switch (category) %7B%0A            case Person:%0A                categoryButton = (LinearLayout) findViewById(R.id.peopleButton);%0A                break;%0A            case Group:%0A                categoryButton = (LinearLayout) findViewById(R.id.groupsButton);%0A                break;%0A            case Place:%0A                categoryButton = (LinearLayout) findViewById(R.id.placesButton);%0A                break;%0A            case Item:%0A                categoryButton = (LinearLayout) findViewById(R.id.itemsButton);%0A                break;%0A            case Concept:%0A            default:%0A                categoryButton = (LinearLayout) findViewById(R.id.conceptsButton);%0A        %7D
+View categoryButton = getCategoryButton(category);%0A%0A        unhighlightAllButtons(context);
 %0A%0A  
@@ -4699,24 +4699,25 @@
 imary);%0A
+%0A
         
 (findVie
@@ -4712,240 +4712,114 @@
     
-(findViewById(R.id.peopleButton)).setBackgroundColor(normalButtonColor);%0A        (findViewById(R.id.groupsButton)).setBackgroundColor(normalButtonColor);%0A        (findViewById(R.id.placesButton)).setBackgroundColor(normalButtonCol
+for (Category category : Category.values()) %7B%0A            View categoryButton = getCategoryButton(categ
 or
+y
 );%0A 
@@ -4829,40 +4829,26 @@
     
-(findViewById(R.id.items
+    category
 Button
-))
 .set
@@ -4895,82 +4895,9 @@
     
-(findViewById(R.id.conceptsButton)).setBackgroundColor(normalButtonColor);
+%7D
 %0A   

Write and use getCategoryButton