/*
 * ***********************************************************************
 *
 *  ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 *
 * ***********************************************************************
 */

package com.adobe.primetime.reference.feature.ads;

import junit.framework.Assert;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;

import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.feature.playback.SimpleFeedItem;
import com.adobe.primetime.reference.feeds.ContentRenditionInfo.ContentFormat;
import com.adobe.primetime.reference.manager.AdsManagerOn;
import com.adobe.primetime.reference.ui.catalog.VideoItem;
import com.adobe.primetime.reference.ui.player.PlayerActivity;
import com.robotium.solo.Solo;

/**
 * Base class for DVR Playback end to end tests which use the PlayerActivity to
 * load HLS video item. Loads a media URL with specified in the contentUrl,
 * which must be set in the constructor of derived classes. Uses the Robotium
 * framework to enable test interaction with the UI.
 */
public abstract class PlaybackWithAdsEndToEndTestBase extends
		ActivityInstrumentationTestCase2<PlayerActivity> {

	public PlaybackWithAdsEndToEndTestBase() {
		super(PlayerActivity.class);
	}

	protected String contentUrl;
	protected JSONObject jsonObject;
	protected ContentFormat contentFormat = ContentFormat.hls;

	public Solo solo = null;
	private VideoItem content = null;
	private boolean autoHide;
	private boolean defaultSeekOverAds;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		Context context = getInstrumentation().getTargetContext();
		Intent intent = new Intent(context, PlayerActivity.class);

		Assert.assertNotNull(contentUrl);

		if (jsonObject != null) {
			content = new VideoItem(new SimpleFeedItem(contentFormat,
					contentUrl, jsonObject));
		} else {
			content = new VideoItem(new SimpleFeedItem(contentFormat,
					contentUrl));
		}

		intent.putExtra("CONTENT_INFO", content);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		setActivityIntent(intent);

		solo = new Solo(getInstrumentation(), getActivity());
		solo.clearLog();

		// set the control bar to not autohide
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		autoHide = prefs.getBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE,
				PrimetimeReference.DEFAULT_AUTOHIDE_CONTROL_BAR);
		prefs.edit().putBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE, false)
				.commit();

		// enable seek over ads
		defaultSeekOverAds = prefs.getBoolean(AdsManagerOn.SETTINGS_SEEK_IN_AD,
				AdsManagerOn.DEFAULT_SEEK_IN_AD);
		prefs.edit().putBoolean(AdsManagerOn.SETTINGS_SEEK_IN_AD, true)
				.commit();

	}

	@Override
	protected void tearDown() throws Exception {

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getInstrumentation()
						.getTargetContext());
		prefs.edit().putBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE, autoHide)
				.commit();
		prefs.edit()
				.putBoolean(AdsManagerOn.SETTINGS_SEEK_IN_AD,
						defaultSeekOverAds).commit();

		solo.finishOpenedActivities();
		super.tearDown();
	}

	/**
	 * Convert string representation of time MM:SS to number of seconds
	 * 
	 * @param time
	 * @return
	 */
	protected int timeToSec(String time) {
		int min = Integer.parseInt(time.substring(0, time.indexOf(":")));
		int sec = Integer.parseInt(time.substring(time.indexOf(":") + 1));
		return min * 60 + sec;
	}

}
