package com.gh4a.activities.home;

import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;

import com.gh4a.Constants;
import com.gh4a.R;
import com.gh4a.fragment.IssueListFragment;
import com.gh4a.fragment.SearchIssueListFragment;

import java.util.HashMap;
import java.util.Map;

public class IssueListFactory extends FragmentFactory {
    private static final String QUERY = "is:%s is:%s %s:%s";

    private static final int[] TAB_TITLES = new int[] {
            R.string.created, R.string.assigned, R.string.mentioned
    };

    private String mState;
    private String mLogin;
    private boolean mIsPullRequest;
    private IssueListFragment.SortDrawerHelper mDrawerHelper =
            new IssueListFragment.SortDrawerHelper();
    private int[] mHeaderColors;

    public IssueListFactory(HomeActivity activity, String userLogin, boolean pr) {
        super(activity);
        mLogin = userLogin;
        mState = Constants.Issue.STATE_OPEN;
        mIsPullRequest = pr;
    }

    @Override
    protected int getTitleResId() {
        if (Constants.Issue.STATE_OPEN.equals(mState)) {
            return mIsPullRequest ? R.string.pull_requests_open : R.string.issues_open;
        } else {
            return mIsPullRequest ? R.string.pull_requests_closed : R.string.issues_closed;
        }
    }

    @Override
    protected int[] getTabTitleResIds() {
        return TAB_TITLES;
    }

    @Override
    protected int[] getHeaderColors() {
        return mHeaderColors;
    }

    @Override
    protected Fragment getFragment(int position) {
        Map<String, String> filterData = new HashMap<>();
        filterData.put("sort", mDrawerHelper.getSortMode());
        filterData.put("order", mDrawerHelper.getSortDirection());

        final String action;
        if (position == 1) {
            action = "assignee";
        } else if (position == 2) {
            action = "mentions";
        } else {
            action = "author";
        }

        filterData.put("q", String.format(QUERY, mIsPullRequest ? "pr" : "issue",
                mState, action, mLogin));

        return SearchIssueListFragment.newInstance(filterData,
                Constants.Issue.STATE_CLOSED.equals(mState),
                mIsPullRequest ? R.string.no_pull_requests_found : R.string.no_issues_found,
                true, mIsPullRequest);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        int resIdState = Constants.Issue.STATE_OPEN.equals(mState) ?
                R.string.issues_menu_show_closed : R.string.issues_menu_show_open;
        MenuItem item = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, resIdState);
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);

        item = menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, R.string.actions)
                .setIcon(R.drawable.overflow);
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                toggleStateFilter();
                return true;
            case Menu.FIRST + 1:
                mActivity.toggleToolDrawer();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected int[] getToolDrawerMenuResIds() {
        return new int[] { IssueListFragment.SortDrawerHelper.getMenuResId() };
    }

    @Override
    protected boolean onDrawerItemSelected(MenuItem item) {
        if (mDrawerHelper.handleItemSelection(item)) {
            reloadIssueList();
            return true;
        }
        return false;
    }

    private void reloadIssueList() {
        mActivity.invalidateFragments();
    }

    private void toggleStateFilter() {
        mState = Constants.Issue.STATE_CLOSED.equals(mState)
                ? Constants.Issue.STATE_OPEN : Constants.Issue.STATE_CLOSED;
        reloadIssueList();
        updateHeaderColor();
        mActivity.invalidateTitle();
    }

    private void updateHeaderColor() {
        boolean showingClosed = Constants.Issue.STATE_CLOSED.equals(mState);
        mHeaderColors = new int[] {
            showingClosed ? R.attr.colorIssueClosed : R.attr.colorIssueOpen,
            showingClosed ? R.attr.colorIssueClosedDark : R.attr.colorIssueOpenDark
        };
        mActivity.invalidateTabs();
    }
}

@@ -28,16 +28,42 @@
 .home;%0A%0A
+import android.os.Bundle;%0A
 import a
@@ -141,16 +141,47 @@
 Compat;%0A
+import android.text.TextUtils;%0A
 import a
@@ -537,16 +537,82 @@
 s:%25s%22;%0A%0A
+    private static final String STATE_KEY_STATE = %22issue:state%22;%0A%0A
     priv
@@ -3948,32 +3948,590 @@
 n false;%0A    %7D%0A%0A
+    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putString(STATE_KEY_STATE, mState);%0A    %7D%0A%0A    @Override%0A    protected void onRestoreInstanceState(Bundle state) %7B%0A        super.onRestoreInstanceState(state);%0A        String newState = state.getString(STATE_KEY_STATE);%0A        if (!TextUtils.equals(mState, newState)) %7B%0A            mState = newState;%0A            reloadIssueList();%0A            updateHeaderColor();%0A            mActivity.invalidateTitle();%0A        %7D%0A    %7D%0A%0A
     private void

Save open/close state to instance state.