/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.app;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.content.Loader;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.content.AccountLoader;
import com.btmura.android.reddit.content.AccountLoader.AccountResult;
import com.btmura.android.reddit.net.UriHelper;
import com.btmura.android.reddit.util.Objects;
import com.btmura.android.reddit.widget.FilterAdapter;

/**
 * {@link Activity} for viewing a user's profile.
 */
public class UserProfileActivity extends AbstractBrowserActivity implements OnNavigationListener {

    private static final String STATE_FILTER = "filter";

    private String currentUser;
    private int currentFilter = -1;
    private FilterAdapter adapter;
    private String accountName;

    @Override
    protected void setContentView() {
        setContentView(R.layout.profile);
    }

    @Override
    protected boolean skipSetup() {
        // Get the user from the intent data or extra.
        Uri data = getIntent().getData();
        if (data != null) {
            currentUser = UriHelper.getUser(data);
            currentFilter = UriHelper.getUserFilter(data);
        }

        // Quit if there is no profile to view.
        if (TextUtils.isEmpty(currentUser)) {
            finish();
            return true;
        }

        // Continue on since we have some user.
        return false;
    }

    @Override
    protected void setupViews() {
    }

    @Override
    protected void setupActionBar(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            currentFilter = savedInstanceState.getInt(STATE_FILTER);
        }

        adapter = new FilterAdapter(this);
        adapter.setTitle(currentUser);

        bar.setListNavigationCallbacks(adapter, this);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setDisplayShowTitleEnabled(false);
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    }

    @Override
    public Loader<AccountResult> onCreateLoader(int id, Bundle args) {
        return new AccountLoader(this, true, false);
    }

    @Override
    public void onLoadFinished(Loader<AccountResult> loader, AccountResult result) {
        accountName = result.getLastAccount();
        adapter.addProfileFilters(this, Objects.equals(accountName, currentUser));

        if (currentFilter == -1) {
            currentFilter = FilterAdapter.PROFILE_OVERVIEW;
        }
        bar.setSelectedNavigationItem(currentFilter);
    }

    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        currentFilter = getFilter();

        ThingListFragment frag = getThingListFragment();
        if (frag == null || !Objects.equals(frag.getAccountName(), accountName)
                || frag.getFilter() != currentFilter) {
            setProfileThingListNavigation(currentUser);
        }
        return true;
    }


    public void onLoaderReset(Loader<AccountResult> loader) {
    }

    @Override
    public String getAccountName() {
        return accountName;
    }

    @Override
    protected int getFilter() {
        return adapter.getFilter(bar.getSelectedNavigationIndex());
    }

    @Override
    protected boolean hasSubredditList() {
        return false;
    }

    @Override
    protected void refreshActionBar(String subreddit, Bundle thingBundle) {
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.profile_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        boolean showThingless = isSinglePane || !hasThing();
        menu.setGroupVisible(R.id.thingless, showThingless);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_new_message:
                handleNewMessage();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void handleNewMessage() {
        MenuHelper.startComposeActivity(this, ComposeActivity.MESSAGE_TYPE_SET,
                null, currentUser, null, null, null, false);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_FILTER, currentFilter);
    }
}

@@ -1165,23 +1165,130 @@
 it.util.
-Objects
+Array;%0Aimport com.btmura.android.reddit.util.Objects;%0Aimport com.btmura.android.reddit.widget.AccountFilterAdapter
 ;%0Aimport
@@ -1633,16 +1633,23 @@
 private 
+Account
 FilterAd
@@ -2567,33 +2567,32 @@
 TER);%0A        %7D%0A
-%0A
         adapter 
@@ -2597,16 +2597,23 @@
 r = new 
+Account
 FilterAd
@@ -2629,48 +2629,8 @@
 s);%0A
-        adapter.setTitle(currentUser);%0A%0A
     
@@ -3137,76 +3137,235 @@
 ();%0A
-        adapter.addProfileFilters(this, Objects.equals(accountName, 
+%0A        // Don't show additional account-only profile filters.%0A        adapter.addProfileFilters(this, false);%0A%0A        // Set the only account option to the user we are viewing not us.%0A        adapter.setAccountInfo(Array.of(
 curr
@@ -3372,16 +3372,22 @@
 entUser)
+, null
 );%0A%0A    
@@ -3499,51 +3499,229 @@
     
-bar.setSelectedNavigationItem(currentFilter
+adapter.setFilter(currentFilter);%0A%0A        // Reset the adapter to trigger a selection callback since there is only 1 account.%0A        bar.setListNavigationCallbacks(adapter, this);%0A        bar.setSelectedNavigationItem(0
 );%0A 
@@ -3799,24 +3799,67 @@
 g itemId) %7B%0A
+        adapter.updateState(itemPosition);%0A
         curr
@@ -3870,16 +3870,24 @@
 ilter = 
+adapter.
 getFilte
@@ -4179,17 +4179,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -4417,40 +4417,8 @@
 ter(
-bar.getSelectedNavigationIndex()
 );%0A 

Use AccountFilterAdapter in UserProfileActivity