/*******************************************************************************
 * ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2014 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.advertising;

import com.adobe.mediacore.PSDKErrorCode;
import com.adobe.mediacore.MediaPlayerItem;
import com.adobe.mediacore.MediaPlayerItemConfig;
import com.adobe.primetime.reference.ui.catalog.ContentRequest;
import com.adobe.mediacore.timeline.Opportunity;
import com.adobe.mediacore.timeline.Placement;
import com.adobe.mediacore.timeline.operations.TimelineOperation;
import com.adobe.mediacore.timeline.operations.DeleteRangeTimelineOperation;
import com.adobe.mediacore.metadata.AdvertisingMetadata;
import com.adobe.mediacore.timeline.advertising.ContentResolver;
import org.json.JSONObject;

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

public class SpliceInCustomResolver extends ContentResolver {

    private MediaPlayerItem _item;
    private static final int TIME_SCALE = 1000;

    @Override
    protected void doConfigure(MediaPlayerItem mediaPlayerItem) {
        _item = mediaPlayerItem;
    }

    @Override
    protected void doResolve(Opportunity opportunity) {
		List<TimelineOperation> result = new ArrayList<TimelineOperation>();
		
        if (opportunity == null) {
            _client.notifyFailed(opportunity, PSDKErrorCode.NULL_POINTER);
			return;
        }
		
		try {
			TimelineOperation deleteRange = createDeleteRangeOperation(opportunity.getPlacement());
			result.add(deleteRange);
			_client.process(result);
			_client.notifyCompleted(opportunity);
		} catch (Exception e) {} 
    }

    @Override
    protected void doCleanup() {}

    @Override
    protected boolean doCanResolve(Opportunity opportunity) {
        if (opportunity.getPlacement().getMode() == Placement.Mode.SPLICEIN) {
            return true;
        }
        return false;
    }
	
	private TimelineOperation createDeleteRangeOperation(Placement placement) {
        Placement adjustedPlacement = new Placement (
                placement.getType(),
                placement.getTime(),
                placement.getDuration(),
				Placement.Mode.SPLICEIN
        );

        TimelineOperation result = new DeleteRangeTimelineOperation(adjustedPlacement);
        return result;
    }
}