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

/*
 *  AdOverlay is a fragment class which contains the UI Component for displaying the Ad information such as 
 *  Ad remaining time,remaining ads in break, ad time,ad duration etc.
 *  Ad Overlay is displayed on top the player view in the player fragment.
 */

package com.adobe.primetime.reference.ui.player;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.adobe.mediacore.timeline.advertising.Ad;
import com.adobe.mediacore.timeline.advertising.AdBreak;
import com.adobe.primetime.reference.R;

import java.util.Iterator;

public class AdOverlay {

	private final int MS_IN_SECOND = 1000;

	private final ViewGroup container;
	private final TextView txtAdRemainingTime;
	private final TextView txtRemainingAdsInBreak;

	private long currentAdBreakSize;
	private long currentAdTime;
	private long currentAdIndex;
	private long currentAdDuration;

	public AdOverlay(ViewGroup container) {
		this.container = container;
		txtAdRemainingTime = (TextView) container.findViewById(R.id.txtAdRemainingTime);
		txtRemainingAdsInBreak = (TextView) container.findViewById(R.id.txtRemainingAdsInBreak);

		hide();
	}

	private void hide() {
		this.container.setVisibility(View.INVISIBLE);
	}

	private void show() {
		this.container.setVisibility(View.VISIBLE);
	}

	public void startAdBreak(AdBreak adBreak) {
		show();
		hideAdProgress();

		Iterator<Ad> it = adBreak.getAdsIterator();
		for(currentAdBreakSize = 0; it.hasNext(); currentAdBreakSize++, it.next());

		currentAdIndex = 0;
	}

	public void startAd(AdBreak adBreak, Ad ad) {
		Iterator<Ad> it = adBreak.getAdsIterator();

		currentAdIndex = 0;
		while (it.hasNext()) {
			currentAdIndex++;
			if (it.next().getId().equals(ad.getId())) {
				break;
			}
		}

		currentAdTime = 0;
		currentAdDuration = (ad.getDuration() / MS_IN_SECOND);

		txtRemainingAdsInBreak.setText(currentAdIndex + "/" + currentAdBreakSize);
		txtAdRemainingTime.setText(currentAdTime + "/" + currentAdDuration);

		showAdProgress();
	}

	public void stopAd(AdBreak adBreak, Ad ad) {
		hideAdProgress();

		currentAdTime = 0;
		currentAdDuration = 0;
	}

	public void stopAdBreak(AdBreak adBreak) {
		currentAdBreakSize = 0;
		hide();
	}

	public void notifyAdProgress(AdBreak adBreak, Ad ad, double percentage) {
		currentAdTime = (long) (currentAdDuration * (percentage / 100.0f));

		txtRemainingAdsInBreak.setText(currentAdIndex + "/" + currentAdBreakSize);
		txtAdRemainingTime.setText(currentAdTime + "/" + currentAdDuration);
	}

	private void showAdProgress() {
		txtRemainingAdsInBreak.setVisibility(View.VISIBLE);

		txtAdRemainingTime.setVisibility(View.VISIBLE);
	}

	private void hideAdProgress() {
		txtRemainingAdsInBreak.setVisibility(View.INVISIBLE);
		txtAdRemainingTime.setVisibility(View.INVISIBLE);
	}
}
