package com.insidesecure.drm.android.reference.entitlements;

import java.net.URL;

import android.content.Context;
import android.os.*;
import com.insidesecure.drmagent.*;
import com.insidesecure.drmagent.entitlements.*;

/** Entitlement delegate purely added to provide callbacks from entitlements operations being being performed.
 * <br/><br/>
 * This is mainly used for UI purposes - the heavy lifting is still done by the {@link SamplePlayReadyEntitlementDelegate}.
 *
 */
public class CallbackEnabledSamplePlayReadyEntitlementDelegate extends URLRewritingPlayReadyEntitlementDelegate
{
    private final Handler mHandler;

    public static interface Callback
    {
        public static enum Action {ACQUIRE_LICENSE, ACKNOWLEDGE_LICENSE, JOIN_DOMAIN, LEAVE_DOMAIN, CANCEL}

        public void actionInitiated(Action action, final DRMContent drmContent);
        public void actionCompleted(Action action, final DRMContent drmContent, DRMError result);
    }

    private Callback mCallback;

    /** Creates a new instance of a <code>{@link CallbackEnabledSamplePlayReadyEntitlementDelegate}</code>, wrapping the necessary helpers.
     *
     * @param context Normal android context
     * @param licenseURLOverride Potential license URL override (from cdesc or similar)
     * @param callback Instance of a <code>{@link Callback}</code> to be used to propagate events to UI
     * @param looper Looper to use for callbacks (normally uses the main UI thread looper)
     */
    public CallbackEnabledSamplePlayReadyEntitlementDelegate(final Context context, final URL licenseURLOverride, final Callback callback, final Looper looper)
    {
        super(context, licenseURLOverride);
        this.mCallback = callback;
        this.mHandler = new Handler(looper);
    }

    /** Creates a new instance of a <code>{@link CallbackEnabledSamplePlayReadyEntitlementDelegate}</code>, wrapping the necessary helpers.  This does not use a license URL override.
     *
     * @param context Normal android context
     * @param licenseURLOverride Potential license URL override (from cdesc or similar)
     * @param callback Instance of a <code>{@link Callback}</code> to be used to propagate events to UI
     * @param looper Looper to use for callbacks (normally uses the main UI thread looper)
     */
    public CallbackEnabledSamplePlayReadyEntitlementDelegate(final Context context, final Callback mCallback, final Looper mainLooper)
    {
        super(context, null);
        this.mCallback = mCallback;
        this.mHandler = new Handler(mainLooper);
    }

    @Override
    public DRMError acquireLicense(final DRMContent drmContent, final EntitlementRequest entitlementRequest, final EntitlementResponse entitlementResponse)
    {
        postActionInitiated(Callback.Action.ACQUIRE_LICENSE,drmContent);
        final DRMError drmError = super.acquireLicense(drmContent, entitlementRequest, entitlementResponse);
        postActionCompleted(Callback.Action.ACQUIRE_LICENSE,drmContent,drmError);
        return drmError;
    }

    @Override
    public DRMError acknowledgeLicense(final DRMContent drmContent, final EntitlementRequest entitlementRequest, final EntitlementResponse entitlementResponse)
    {
        postActionInitiated(Callback.Action.ACKNOWLEDGE_LICENSE,drmContent);
        final DRMError drmError = super.acknowledgeLicense(drmContent, entitlementRequest, entitlementResponse);
        postActionCompleted(Callback.Action.ACKNOWLEDGE_LICENSE,drmContent,drmError);
        return drmError;
    }

    @Override
    public DRMError joinDomain(final EntitlementRequest entitlementRequest, final EntitlementResponse entitlementResponse)
    {
        postActionInitiated(Callback.Action.JOIN_DOMAIN,null);
        final DRMError drmError = super.joinDomain(entitlementRequest, entitlementResponse);
        postActionCompleted(Callback.Action.JOIN_DOMAIN,null,drmError);
        return drmError;
    }

    @Override
    public DRMError leaveDomain(final EntitlementRequest entitlementRequest, final EntitlementResponse entitlementResponse)
    {
        postActionInitiated(Callback.Action.LEAVE_DOMAIN,null);
        final DRMError drmError = super.leaveDomain(entitlementRequest, entitlementResponse);
        postActionCompleted(Callback.Action.LEAVE_DOMAIN,null,drmError);
        return drmError;
    }

    @Override
    public void cancelEntitlementRequest()
    {
        postActionCompleted(Callback.Action.CANCEL,null,DRMError.SUCCESS);
        super.cancelEntitlementRequest();
        postActionCompleted(Callback.Action.CANCEL,null,DRMError.SUCCESS);
    }

    private void postActionCompleted(final Callback.Action action, final DRMContent drmContent, final DRMError drmError)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                mCallback.actionCompleted(action, drmContent, drmError);
            }
        });
    }

    private void postActionInitiated(final Callback.Action action, final DRMContent drmContent)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                mCallback.actionInitiated(action, drmContent);
            }
        });
    }

}
