/*
 * 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.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;

import com.btmura.android.reddit.R;

public class GlobalMenuFragment extends Fragment implements
        SearchView.OnFocusChangeListener,
        SearchView.OnQueryTextListener {

    public static final String TAG = "GlobalMenuFragment";

    private static final int REQUEST_SEARCH = 0;

    public interface OnSearchQuerySubmittedListener {
        boolean onSearchQuerySubmitted(String query);
    }

    private OnSearchQuerySubmittedListener listener;
    private MenuItem searchItem;
    private SearchView searchView;

    public static GlobalMenuFragment newInstance() {
        return new GlobalMenuFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnSearchQuerySubmittedListener) {
            listener = (OnSearchQuerySubmittedListener) activity;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.global_menu, menu);
        searchItem = menu.findItem(R.id.menu_search);
        searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextFocusChangeListener(this);
        searchView.setOnQueryTextListener(this);
    }

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

            case R.id.menu_settings:
                handleSettings();
                return true;

            case R.id.menu_submit_link:
                handleSubmitLink();
                return true;

            case R.id.menu_add_subreddit:
                handleAddSubreddit();
                return true;

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

    public void handleSearch() {
        searchItem.expandActionView();
    }

    private void handleSettings() {
        startActivity(new Intent(getActivity(), SettingsActivity.class));
    }

    private void handleSubmitLink() {
        Intent intent = new Intent(getActivity(), SubmitLinkActivity.class);
        startActivity(intent);
    }

    private void handleAddSubreddit() {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(AddSubredditFragment.newInstance(), AddSubredditFragment.TAG);
        ft.commit();
    }

    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            searchItem.collapseActionView();
        }
    }

    public boolean onQueryTextChange(String newText) {
        return false;
    }

    public boolean onQueryTextSubmit(String query) {
        if (listener != null && listener.onSearchQuerySubmitted(query)) {
            searchItem.collapseActionView();
        } else {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
                    | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            intent.putExtra(SearchActivity.EXTRA_QUERY, query);
            startActivityForResult(intent, REQUEST_SEARCH);
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_SEARCH:
                searchItem.collapseActionView();
                break;

            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

@@ -755,22 +755,103 @@
 oid.
-content.Intent
+app.LoaderManager.LoaderCallbacks;%0Aimport android.content.Intent;%0Aimport android.content.Loader
 ;%0Aim
@@ -1060,16 +1060,187 @@
 eddit.R;
+%0Aimport com.btmura.android.reddit.content.AccountLoader;%0Aimport com.btmura.android.reddit.content.AccountLoader.AccountResult;%0Aimport com.btmura.android.reddit.util.Array;
 %0A%0Apublic
@@ -1373,16 +1373,56 @@
 Listener
+,%0A        LoaderCallbacks%3CAccountResult%3E
  %7B%0A%0A    
@@ -1762,16 +1762,49 @@
 rchView;
+%0A    private boolean hasAccounts;
 %0A%0A    pu
@@ -2285,32 +2285,700 @@
 u(true);%0A    %7D%0A%0A
+    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState);%0A        getLoaderManager().initLoader(0, null, this);%0A    %7D%0A%0A    public Loader%3CAccountResult%3E onCreateLoader(int id, Bundle args) %7B%0A        return new AccountLoader(getActivity(), false);%0A    %7D%0A%0A    public void onLoadFinished(Loader%3CAccountResult%3E loader, AccountResult result) %7B%0A        hasAccounts = !Array.isEmpty(result.accountNames);%0A        getActivity().invalidateOptionsMenu();%0A    %7D%0A%0A    public void onLoaderReset(Loader%3CAccountResult%3E loader) %7B%0A        hasAccounts = false;%0A        getActivity().invalidateOptionsMenu();%0A    %7D%0A%0A
     @Override%0A  
@@ -3322,24 +3322,24 @@
 ener(this);%0A
-
         sear
@@ -3374,32 +3374,215 @@
 r(this);%0A    %7D%0A%0A
+    @Override%0A    public void onPrepareOptionsMenu(Menu menu) %7B%0A        super.onPrepareOptionsMenu(menu);%0A        menu.findItem(R.id.menu_submit_link).setVisible(hasAccounts);%0A    %7D%0A%0A
     @Override%0A  

Fix #36: don't show submit link without accounts