/*
 * Copyright (C) 2013 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.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import com.btmura.android.reddit.database.Subreddits;
import com.btmura.android.reddit.provider.SubredditProvider;
import com.btmura.android.reddit.util.Array;
import com.btmura.android.reddit.widget.SubredditAdapter;

class AccountSubredditListController implements SubredditListController {

    private static final String[] PROJECTION = {
            Subreddits._ID,
            Subreddits.COLUMN_NAME
    };

    private static final String EXTRA_ACCOUNT_NAME = SubredditListFragment.ARG_ACCOUNT_NAME;
    private static final String EXTRA_SUBREDDIT = SubredditListFragment.ARG_SELECTED_SUBREDDIT;
    private static final String EXTRA_SESSION_ID = SubredditListFragment.STATE_SESSION_ID;
    private static final String EXTRA_ACTION_ACCOUNT_NAME =
            SubredditListFragment.STATE_SELECTED_ACTION_ACCOUNT;

    private final Context context;
    private final SubredditAdapter adapter;
    private String accountName;
    private String subreddit;
    private long sessionId;
    private String actionAccountName;

    AccountSubredditListController(Context context, SubredditAdapter adapter, Bundle args) {
        this.context = context;
        this.adapter = adapter;
        this.accountName = getAccountNameExtra(args);
        this.subreddit = getSubredditExtra(args);
    }

    @Override
    public void restoreInstanceState(Bundle savedInstanceState) {
        this.accountName = getAccountNameExtra(savedInstanceState);
        this.subreddit = getSubredditExtra(savedInstanceState);
        this.sessionId = getSessionIdExtra(savedInstanceState);
        this.actionAccountName = getActionAccountNameExtra(savedInstanceState);
    }

    @Override
    public void saveInstanceState(Bundle outState) {
        outState.putString(EXTRA_ACCOUNT_NAME, accountName);
        outState.putString(EXTRA_SUBREDDIT, subreddit);
        outState.putLong(EXTRA_SESSION_ID, sessionId);
        outState.putString(EXTRA_ACTION_ACCOUNT_NAME, actionAccountName);
    }

    // Loader related methods.

    @Override
    public boolean isLoadable() {
        return accountName != null;
    }

    @Override
    public Loader<Cursor> createLoader() {
        return new CursorLoader(context, SubredditProvider.SUBREDDITS_URI, PROJECTION,
                Subreddits.SELECT_BY_ACCOUNT_NOT_DELETED, Array.of(accountName),
                Subreddits.SORT_BY_NAME);
    }

    @Override
    public void swapCursor(Cursor cursor) {
        adapter.swapCursor(cursor);
    }

    // Getters

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

    @Override
    public String getActionAccountName() {
        return actionAccountName;
    }

    @Override
    public SubredditAdapter getAdapter() {
        return adapter;
    }

    @Override
    public String getSelectedSubreddit() {
        return subreddit;
    }

    @Override
    public boolean hasActionAccountName() {
        return getActionAccountName() != null;
    }

    @Override
    public boolean isSingleChoice() {
        return adapter.isSingleChoice();
    }

    @Override
    public boolean isSwipeDismissable(int position) {
        return Subreddits.hasSidebar(adapter.getName(position));
    }

    // Setters

    @Override
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    @Override
    public void setActionAccountName(String actionAccountName) {
        this.actionAccountName = actionAccountName;
    }

    @Override
    public String setSelectedPosition(int position) {
        return adapter.setSelectedPosition(position);
    }

    @Override
    public void setSelectedSubreddit(String subreddit) {
        this.subreddit = subreddit;
    }

    // Getters for extras.

    private static String getAccountNameExtra(Bundle extras) {
        return extras.getString(EXTRA_ACCOUNT_NAME);
    }

    private static String getSubredditExtra(Bundle extras) {
        return extras.getString(EXTRA_SUBREDDIT);
    }

    private static long getSessionIdExtra(Bundle extras) {
        return extras.getLong(EXTRA_SESSION_ID);
    }

    private static String getActionAccountNameExtra(Bundle extras) {
        return extras.getString(EXTRA_ACTION_ACCOUNT_NAME);
    }
}

@@ -1360,24 +1360,33 @@
 tring EXTRA_
+SELECTED_
 SUBREDDIT = 
@@ -1384,16 +1384,28 @@
 REDDIT =
+%0A           
  Subredd
@@ -1775,38 +1775,8 @@
 me;%0A
-    private String subreddit;%0A
     
@@ -2053,38 +2053,44 @@
 s);%0A        
-this.s
+setSelectedS
 ubreddit
  = getSubred
@@ -2069,39 +2069,45 @@
 electedSubreddit
- = 
+(
 getS
+electedS
 ubredditExtra(ar
@@ -2105,24 +2105,25 @@
 tExtra(args)
+)
 ;%0A    %7D%0A%0A   
@@ -2279,28 +2279,40 @@
     
-this.subreddit = get
+setSelectedSubreddit(getSelected
 Subr
@@ -2333,32 +2333,33 @@
 edInstanceState)
+)
 ;%0A        this.s
@@ -2648,32 +2648,41 @@
 putString(EXTRA_
+SELECTED_
 SUBREDDIT, subre
@@ -2676,25 +2676,36 @@
 REDDIT, 
-s
+getSelectedS
 ubreddit
 );%0A     
@@ -2696,16 +2696,18 @@
 ubreddit
+()
 );%0A     
@@ -3695,25 +3695,44 @@
  return 
-s
+adapter.getSelectedS
 ubreddit
 ;%0A    %7D%0A
@@ -3715,32 +3715,34 @@
 electedSubreddit
+()
 ;%0A    %7D%0A%0A    @Ov
@@ -4566,22 +4566,36 @@
     
-this.s
+adapter.setSelectedS
 ubreddit
  = s
@@ -4590,19 +4590,17 @@
 ubreddit
- = 
+(
 subreddi
@@ -4600,16 +4600,17 @@
 ubreddit
+)
 ;%0A    %7D%0A
@@ -4791,16 +4791,24 @@
 ing getS
+electedS
 ubreddit
@@ -4868,16 +4868,25 @@
 g(EXTRA_
+SELECTED_
 SUBREDDI

Fix selected subreddit highlighting