package com.insidesecure.drm.android.reference.xoplayer.activities.helpers;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.insidesecure.drmagent.*;
import com.insidesecure.drmagent.download.AbstractDownloadEventListener;

public class CachedDurationCapturingDownloadEventListener extends AbstractDownloadEventListener
{
    private String TAG = CachedDurationCapturingDownloadEventListener.class.getSimpleName();
    private int mCurrentCachedDurationSec;
    private Context mContext;

    public CachedDurationCapturingDownloadEventListener(final Context mContext)
    {
        this.mContext = mContext;
    }

    public void started(final DRMContent drmContent, final int totalSegmentsDownloaded, final int totalNumSegments, final int totalNumSecondsAvailableInCache)
    {
        Log.d(TAG, "Download started; totalSegmentsDownloaded=" + totalSegmentsDownloaded + ", " +
                "totalNumSegments=" + totalNumSegments + ", totalNumSecondsAvailableInCache=" +
                totalNumSecondsAvailableInCache);
        mCurrentCachedDurationSec = totalNumSecondsAvailableInCache;
    }

    public void segmentDownloaded(final DRMContent drmContent, final int segmentNumber, final int totalSegmentsDownloaded,
                                  final int totalNumSegments, final int totalNumSecondsAvailableInCache)
    {
        Log.d(TAG, "Segment downloaded; segmentNumber=" + segmentNumber + ", totalSegmentsDownloaded=" + totalSegmentsDownloaded + ", " +
                "totalNumSegments=" + totalNumSegments + ", totalNumSecondsAvailableInCache=" +
                totalNumSecondsAvailableInCache);
        mCurrentCachedDurationSec = totalNumSecondsAvailableInCache;
    }

    @Override
    public void error(final DRMContent drmContent, final DRMError error)
    {
        Log.e(TAG, "Error occurred during download of content: " + error);
        final Toast toast = Toast.makeText(mContext, "Error occurred during download of addition media data: " + error, Toast.LENGTH_LONG);
        toast.show();
    }

    public void complete(final DRMContent drmContent, final int totalNumSegments, final int totalDuration)
    {
        mCurrentCachedDurationSec = totalDuration * 1000;
        Log.d(TAG, "Download Complete: totalDuration=" + totalDuration + ", " + "totalNumSegments=" + totalNumSegments);
        final Toast toast = Toast.makeText(mContext, "Download completed", Toast.LENGTH_LONG);
        toast.show();
    }

    public void stopped(final DRMContent drmContent, final int totalSegmentsDownloaded, final int totalNumSegments,
                        final int totalNumSecondsAvailableInCache)
    {
        Log.d(TAG, "Download stopped; totalSegmentsDownloaded=" + totalSegmentsDownloaded + ", " +
                "totalNumSegments=" + totalNumSegments + ", totalNumSecondsAvailableInCache=" +
                totalNumSecondsAvailableInCache);
    }

    public int getCurrentCachedDurationMs()
    {
        return mCurrentCachedDurationSec * 1000;
    }
}