/*
 * ***********************************************************************
 *
 *  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.playback;

import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.adobe.mediacore.MediaPlayer.PlayerStatus;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.feature.ads.PlaybackWithAdsEndToEndTestBase;
import com.robotium.solo.Condition;
import junit.framework.Assert;
import org.json.JSONObject;

/**
 * Play a HLS stream with play and pause operation.
 */
public class DVREndToEndTestWithAds extends PlaybackWithAdsEndToEndTestBase {

	private final String BUFFER_COMPLETE = "Buffering complete";
	private final String PLAYBACK_START = "Player state changed to ["
			+ PlayerStatus.PLAYING + "].";
	private final String AUDITUDE_AD_START = "Reporting AD-START";

	public DVREndToEndTestWithAds() throws Exception {
		contentUrl = "http://d398890tia84ty.cloudfront.net/cloudfront_hls.m3u8";

		String json = "{\"id\":\"vid2\",\"title\":\"Video With Primetime Ads\",\"description\": \"This is an episode with primetime ads.\",\"categories\":\"documentary, educational, children\",\"keywords\":\"List, of, comma, separated, keywords\",\"isLive\": false,\"content\":[{\"format\":\"M3U8\",\"url\":\"http://d398890tia84ty.cloudfront.net/cloudfront_hls.m3u8\",\"language\": \"en\"}],\"thumbnails\":[{\"format\":\"JPEG\",\"height\":\"90\",\"width\":\"160\",\"url\":\"http://example.com/small.jpg\"},{\"format\": \"JPEG\",\"height\":\"180\",\"width\":\"320\",\"url\":\"http://example.com/medium.jpg\"}],\"metadata\":{\"ad\":{\"type\":\"Primetime Ads\",\"details\":{\"domain\":\"auditude.com\",\"mediaid\":\"adbe_tearsofsteel2\",\"zoneid\":\"123869\"}}}}";
		jsonObject = new JSONObject(json);
	}

	/**
	 * Test player activity loads and play the video item correctly
	 */
	public void testLoadAndPlayVideo() throws InterruptedException {
		// Verify buffering on prepare stage
		Assert.assertTrue("View is displaying buffering spinner", solo.waitForView(R.id.pbBufferingSpinner));
        final ProgressBar spinner = (ProgressBar) solo.getView(R.id.pbBufferingSpinner);

        // wait for buffering to complete
        solo.waitForCondition (new Condition() {
            @Override
            public boolean isSatisfied() {
                return spinner.getVisibility() == View.INVISIBLE;
            }
        }, 10000);

		// Verify content is LIVE
		String timestamp = ((TextView) solo.getView(R.id.txtCurrentTime)).getText().toString();
		Assert.assertTrue("Timestamp contains 'LIVE'", timestamp.contains("LIVE"));
	}

	/**
	 * Test play ad and seek within DVR window with ads
	 * 
	 * @throws InterruptedException
	 */
	public void testSeekWithAdsWithinDVRWindow() throws InterruptedException {

		// Wait until video starts playing
		Assert.assertTrue(solo.waitForLogMessage(PLAYBACK_START));
		solo.sleep(10000);
		// Wait until auditude ad starts to play
		Assert.assertTrue(solo.waitForLogMessage(AUDITUDE_AD_START));
		solo.clearLog();

		// This test checks if ads will play within 120 seconds after you seek
		// back within DVR window, only uncomment if you are
		// sure ads will play within 120 seconds after seek
		// Seek to the middle of DVR window, check ads play
		// View controlBar = (View) solo.getView(R.id.ControlBar);
		// solo.clickOnView(controlBar);
		// Assert.assertTrue(solo.waitForLogMessage(AUDITUDE_AD_START, 120000));
		// String seekedTimeString =
		// ((TextView)solo.getView(R.id.txtCurrentTime)).getText().toString().substring(0,
		// 5);
		// int seekedTime = timeToSec(seekedTimeString);
		// Assert.assertTrue(seekedTime < clientLiveTime);
	}
}
