/*
 * Copyright 2012 Google Inc.
 *
 * 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.funkyandroid.droidcon.uk.iosched.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ShareCompat;
import android.view.ActionProvider;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import com.funkyandroid.droidcon.uk.iosched.R;
import com.funkyandroid.droidcon.uk.iosched.appwidget.ScheduleWidgetProvider;
import com.funkyandroid.droidcon.uk.iosched.provider.ScheduleContract;
import com.funkyandroid.droidcon.uk.iosched.service.ScheduleUpdaterService;
import com.funkyandroid.droidcon.uk.iosched.ui.SocialStreamActivity;
import com.funkyandroid.droidcon.uk.iosched.ui.SocialStreamFragment;

import static com.funkyandroid.droidcon.uk.iosched.util.LogUtils.LOGD;
import static com.funkyandroid.droidcon.uk.iosched.util.LogUtils.makeLogTag;

/**
 * Helper class for dealing with common actions to take on a session.
 */
public final class SessionsHelper {

    private static final String TAG = makeLogTag(SessionsHelper.class);

    private final Activity mActivity;

    public SessionsHelper(Activity activity) {
        mActivity = activity;
    }

    public Intent createShareIntent(int messageTemplateResId, String title, String hashtags,
            String url) {
        ShareCompat.IntentBuilder builder = ShareCompat.IntentBuilder.from(mActivity)
                .setType("text/plain")
                .setText(mActivity.getString(messageTemplateResId,
                        title, UIUtils.getSessionHashtagsString(hashtags), url));
        return builder.getIntent();
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void tryConfigureShareMenuItem(MenuItem menuItem, int messageTemplateResId,
            final String title, String hashtags, String url) {
        if (UIUtils.hasICS()) {
            ActionProvider itemProvider = menuItem.getActionProvider();
            ShareActionProvider provider;
            if (!(itemProvider instanceof ShareActionProvider)) {
                provider = new ShareActionProvider(mActivity);
            } else {
                provider = (ShareActionProvider) itemProvider;
            }
            provider.setShareIntent(createShareIntent(messageTemplateResId, title, hashtags, url));
            provider.setOnShareTargetSelectedListener(
                    new ShareActionProvider.OnShareTargetSelectedListener() {
                        @Override
                        public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                            LOGD("Tracker", "Shared: " + title);
                            return false;
                        }
                    });

            menuItem.setActionProvider(provider);
        }
    }

    public void shareSession(Context context, int messageTemplateResId, String title,
            String hashtags, String url) {
        LOGD("Tracker", "Shared: " + title);
        context.startActivity(Intent.createChooser(
                createShareIntent(messageTemplateResId, title, hashtags, url),
                context.getString(R.string.title_share)));
    }

    public void setSessionStarred(Uri sessionUri, boolean starred, String title) {
        LOGD(TAG, "setSessionStarred uri=" + sessionUri + " starred=" +
                starred + " title=" + title);
        sessionUri = ScheduleContract.addCallerIsSyncAdapterParameter(sessionUri);
        final ContentValues values = new ContentValues();
        values.put(ScheduleContract.Sessions.SESSION_STARRED, starred);
        AsyncQueryHandler handler =
                new AsyncQueryHandler(mActivity.getContentResolver()) {
                };
        handler.startUpdate(-1, null, sessionUri, values, null, null);

        // Because change listener is set to null during initialization, these
        // won't fire on pageview.
        mActivity.sendBroadcast(ScheduleWidgetProvider.getRefreshBroadcastIntent(mActivity, false));

        // Sync to the cloudz.
        uploadStarredSession(mActivity, sessionUri, starred);
    }

    public static void uploadStarredSession(Context context, Uri sessionUri, boolean starred) {
        final Intent updateServerIntent = new Intent(context, ScheduleUpdaterService.class);
        updateServerIntent.putExtra(ScheduleUpdaterService.EXTRA_SESSION_ID,
                ScheduleContract.Sessions.getSessionId(sessionUri));
        updateServerIntent.putExtra(ScheduleUpdaterService.EXTRA_IN_SCHEDULE, starred);
        context.startService(updateServerIntent);
    }

    public void startSocialStream(String hashtags) {
        Intent intent = new Intent(mActivity, SocialStreamActivity.class);
        intent.putExtra(SocialStreamFragment.EXTRA_QUERY, UIUtils.getSessionHashtagsString(hashtags));
        mActivity.startActivity(intent);
    }
}

@@ -954,24 +954,35 @@
 ort android.
+support.v4.
 view.ActionP
@@ -1005,16 +1005,27 @@
 android.
+support.v4.
 view.Men
@@ -1029,16 +1029,22 @@
 MenuItem
+Compat
 ;%0Aimport
@@ -1052,16 +1052,27 @@
 android.
+support.v7.
 widget.S
@@ -1083,32 +1083,62 @@
 ActionProvider;%0A
+import android.view.MenuItem;%0A
 import com.funky
@@ -2707,24 +2707,30 @@
 vider = 
-m
+M
 enuItem
+Compat
 .getActi
@@ -2740,16 +2740,24 @@
 rovider(
+menuItem
 );%0A     
@@ -3563,24 +3563,30 @@
         
-m
+M
 enuItem
+Compat
 .setActi
@@ -3596,16 +3596,26 @@
 rovider(
+menuItem, 
 provider

Use MenuItemCompat to fix cast exception bug.