/*
 * 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.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.content.ClipHelper;
import com.btmura.android.reddit.database.Things;
import com.btmura.android.reddit.net.Urls;
import com.btmura.android.reddit.widget.ThingPagerAdapter;

public class ThingMenuFragment extends Fragment {

    public static final String TAG = "ThingMenuFragment";

    private static final String ARG_THING_BUNDLE = "tb";

    public interface ThingPagerHolder {
        ViewPager getPager();
    }

    private Bundle thingBundle;
    private ShareActionProvider shareProvider;

    public static ThingMenuFragment newInstance(Bundle thingBundle) {
        Bundle args = new Bundle(1);
        args.putBundle(ARG_THING_BUNDLE, thingBundle);
        ThingMenuFragment f = new ThingMenuFragment();
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thingBundle = getArguments().getBundle(ARG_THING_BUNDLE);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.thing_menu, menu);
        shareProvider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        boolean self = Things.isSelf(thingBundle);
        boolean showingLink = isShowingLink();
        boolean showLink = !self && !showingLink;
        boolean showComments = !self && showingLink;

        menu.findItem(R.id.menu_link).setVisible(showLink);
        menu.findItem(R.id.menu_comments).setVisible(showComments);

        updateShareProvider();
    }

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

            case R.id.menu_link:
                handleLink();
                return true;

            case R.id.menu_comments:
                handleComments();
                return true;

            case R.id.menu_copy_url:
                handleCopyUrl();
                return true;

            case R.id.menu_open:
                handleOpen();
                return true;

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

    private void updateShareProvider() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, Things.getTitle(thingBundle));
        intent.putExtra(Intent.EXTRA_TEXT, getLink());
        shareProvider.setShareIntent(intent);
    }

    private void handleViewSidebar() {
        Intent intent = new Intent(getActivity(), SidebarActivity.class);
        intent.putExtra(SidebarActivity.EXTRA_SUBREDDIT, Things.getSubreddit(thingBundle));
        startActivity(intent);
    }

    private void handleLink() {
        getHolder().getPager().setCurrentItem(0);
    }

    private void handleComments() {
        getHolder().getPager().setCurrentItem(1);
    }

    private void handleCopyUrl() {
        ClipHelper.setClipToast(getActivity(), Things.getTitle(thingBundle), getLink());
    }

    private void handleOpen() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(getLink().toString()));
        startActivity(Intent.createChooser(intent, getString(R.string.menu_open)));
    }

    private boolean isShowingLink() {
        int position = getHolder().getPager().getCurrentItem();
        return ThingPagerAdapter.getType(thingBundle, position) == ThingPagerAdapter.TYPE_LINK;
    }

    private CharSequence getLink() {
        if (isShowingLink()) {
            return Things.getUrl(thingBundle);
        } else {
            return Urls.permaUrl(Things.getPermaLink(thingBundle), null).toExternalForm();
        }
    }

    private ThingPagerHolder getHolder() {
        return (ThingPagerHolder) getActivity();
    }
}

@@ -1446,55 +1446,8 @@
 dle;
-%0A    private ShareActionProvider shareProvider;
 %0A%0A  
@@ -2126,106 +2126,9 @@
 u);%0A
-        shareProvider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
 %0A
+
     
@@ -2596,16 +2596,20 @@
 rovider(
+menu
 );%0A    %7D
@@ -3348,24 +3348,33 @@
 areProvider(
+Menu menu
 ) %7B%0A        
@@ -3586,24 +3586,165 @@
  getLink());
+%0A%0A        ShareActionProvider shareActionProvider =%0A                (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
 %0A        sha
@@ -3741,24 +3741,30 @@
        share
+Action
 Provider.set

Fix NPE where onPrepareOptionsMenu called early