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

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.accounts.AccountUtils;

public class GlobalMenuFragment extends Fragment implements OnFocusChangeListener,
        OnQueryTextListener {

    public static final String TAG = "GlobalMenuFragment";

    private static final int REQUEST_SEARCH = 0;

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

    private SubredditNameHolder subredditNameHolder;
    private AccountNameHolder accountNameHolder;
    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 SubredditNameHolder) {
            subredditNameHolder = (SubredditNameHolder) activity;
        }
        if (activity instanceof AccountNameHolder) {
            accountNameHolder = (AccountNameHolder) 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 void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        boolean isAccount = accountNameHolder != null
                && AccountUtils.isAccount(accountNameHolder.getAccountName());
        menu.findItem(R.id.menu_submit_link).setVisible(isAccount);
        menu.findItem(R.id.menu_profile).setVisible(isAccount);
        menu.findItem(R.id.menu_messages).setVisible(isAccount);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_subreddit:
                return handleAddSubreddit();

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

            case R.id.menu_search:
                return handleSearch();

            case R.id.menu_profile:
                return handleProfile();

            case R.id.menu_messages:
                return handleMessages();

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

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

    private boolean handleAddSubreddit() {
        AddSubredditFragment.newInstance().show(getFragmentManager(), AddSubredditFragment.TAG);
        return true;
    }

    private boolean handleSubmitLink() {
        Intent intent = new Intent(getActivity(), SubmitLinkActivity.class);
        if (subredditNameHolder != null) {
            intent.putExtra(SubmitLinkActivity.EXTRA_SUBREDDIT,
                    subredditNameHolder.getSubredditName());
        }
        startActivity(intent);
        return true;
    }

    public boolean handleSearch() {
        searchItem.expandActionView();
        return true;
    }

    private boolean handleProfile() {
        MenuHelper.startProfileActivity(getActivity(), accountNameHolder.getAccountName());
        return true;
    }

    private boolean handleMessages() {
        Intent intent = new Intent(getActivity(), MessageActivity.class);
        intent.putExtra(MessageActivity.EXTRA_USER, accountNameHolder.getAccountName());
        startActivity(intent);
        return true;
    }

    private boolean handleSettings() {
        startActivity(new Intent(getActivity(), SettingsActivity.class));
        return true;
    }

    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);
        }
    }
}

@@ -6095,16 +6095,62 @@
 SEARCH:%0A
+                if (searchItem != null) %7B%0A    
         
@@ -6182,32 +6182,50 @@
 seActionView();%0A
+                %7D%0A
                 

Fix #90: fix NPE for SearchView