/*
 * ************************************************************************
 *
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2013 Adobe Systems Incorporated
 * All Rights Reserved.
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 *
 **************************************************************************
 */

package com.adobe.primetime.reference.advertising;

import com.adobe.mediacore.PSDKErrorCode;
import com.adobe.mediacore.MediaPlayerItem;
import com.adobe.mediacore.MediaResource;
import com.adobe.mediacore.timeline.ContentTracker;
import com.adobe.mediacore.timeline.Opportunity;
import com.adobe.mediacore.timeline.operations.*;
import com.adobe.mediacore.timeline.advertising.*;
import com.adobe.primetime.reference.PrimetimeReference;

import java.util.ArrayList;
import java.util.List;

public class CustomAdProviderContentResolver extends ContentResolver {


	@Override
	protected boolean doCanResolve(Opportunity opportunity) {
		return true;
	}

	@Override
	protected void doCleanup() {
		
	}

	@Override
	protected void doConfigure(MediaPlayerItem mediaPlayerItem) {

	}

	@Override
	protected void doResolve(Opportunity opportunity) {

		List<TimelineOperation> operations = createTimelineOperations(opportunity);

		if (operations.isEmpty())
		{
			_client.notifyFailed(opportunity, PSDKErrorCode.AD_RESOLVER_FAILED);
		}
		else
		{
			_client.process(operations);
			_client.notifyCompleted(opportunity);
		}

	}

	private List<TimelineOperation> createTimelineOperations(Opportunity opportunity)
	{
		List<TimelineOperation> operations = new ArrayList<TimelineOperation>();

		String adURL = "http://ptdemos.com/videos/hlsads/ad6/video.m3u8";
		long duration = 30000; // duration of ad
		AdClick adClick = new AdClick("01", "CustomAdClick", "http://adobe.com");
		MediaResource res = new MediaResource(adURL, MediaResource.Type.HLS, null);

		AdAsset asset = new AdAsset("CustomAdAsset1", duration, res, adClick, null, null);
		Ad ad = new Ad("CustomAd1", Ad.AdType.LINEAR, duration, true, asset, null, new CustomContentTracker(), AdInsertionType.CLIENT_INSERTED, false, true);
		List<Ad> ads = new ArrayList<Ad>();
		ads.add(ad);
		AdBreak adBreak = new AdBreak(ads, new CustomContentTracker());

		AdBreakPlacement placement = new AdBreakPlacement(adBreak, opportunity.getPlacement());

		operations.add(placement);
		return operations;
	}

	class CustomContentTracker implements ContentTracker
	{

		@Override
		public void onAdStarted() {
			PrimetimeReference.logger.d("CustomContentTracker", "Ad started");
		}

		@Override
		public void onAdProgress(long l) {
			PrimetimeReference.logger.d("CustomContentTracker", "Ad progress " + l);
		}

		@Override
		public void onAdCompleted() {
			PrimetimeReference.logger.d("CustomContentTracker", "Ad completed");
		}

		@Override
		public void onAdClick() {
			PrimetimeReference.logger.d("CustomContentTracker", "Ad clicked");
		}
	}
}
