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

import android.os.*;
import android.widget.*;
import com.insidesecure.drm.android.reference.Tools;
import com.insidesecure.drmagent.mediaplayer.MediaPlayer;

public class MediaController implements Runnable
{
    public interface MediaInformationExchanger
    {
        public long getDurationMs();
        public long getCurrentCachedDurationMs();
        public long getCurrentPositionMs();
        public void performSeek(long seekPositionMs);
        public void frameRateUpdate(int currentFramePerSec);
    }

    private final MediaPlayer mMmediaPlayer;
    private final SeekBar mSeekBar;

    private final TextView mDurationTextView;
    private final TextView mCurrentPositionTextView;

    private boolean mShowTimeStampForLiveStreams;
    private boolean mIsLiveStream;
    private final MediaInformationExchanger mMediaInformationExchanger;
    private boolean mAlwaysShowSeekBar;

    private boolean mKeepRunning = true;
    private boolean mUpdating = true;

    private int mCurrentCachedDurationSec;
    private int mCurrentPositionSec;
    private final int mDurationSec;

    private final Handler mHandler;

    private final SeekHandler mSeekHandler;
    private final FrameRateCalculator mFrameRateCalculator;


    public MediaController(final MediaPlayer mediaPlayer, SeekBar seekBar, TextView durationTextView, TextView currentPositionTextView, boolean showTimeStampForLiveStreams, boolean isLiveStream, Looper looper, MediaInformationExchanger mediaInformationExchanger)
    {
        mMmediaPlayer = mediaPlayer;
        mSeekBar = seekBar;
        mDurationTextView = durationTextView;
        mCurrentPositionTextView = currentPositionTextView;

        mShowTimeStampForLiveStreams = showTimeStampForLiveStreams;
        mIsLiveStream = isLiveStream;
/*
            mAlwaysShowSeekBar = ((getDRMContent().getDRMContentFormat() == DRMContentFormat.SMOOTH_STREAMING) ||
                                  (getDRMContent().getDRMContentFormat() == DRMContentFormat.MPEG_DASH)) &&
                                 mIsLiveStream;
*/


        mMediaInformationExchanger = mediaInformationExchanger;

        mHandler = new Handler(looper);
        mHandler.postDelayed(MediaController.this, 1000);

        mDurationSec = (int) (mediaInformationExchanger.getDurationMs() / 1000);
        mSeekBar.setMax(mDurationSec);

        //
       mSeekHandler = new SeekHandler(mSeekBar, new SeekHandler.SeekEventCallbackHandler()
       {
           @Override
           public void seekStart()
           {
               //
               setUpdating(false);
           }

           @Override
           public void seekEnd(final int position)
           {
               // It is now time to seek 
               mMediaInformationExchanger.performSeek((position*1000));
           }
       });

        mFrameRateCalculator = new FrameRateCalculator(mediaPlayer, Looper.getMainLooper(), new FrameRateCalculator.FrameRateReportCallback()
        {
            @Override
            public void framesPerSecond(final int frameRate)
            {
                mMediaInformationExchanger.frameRateUpdate(frameRate);
            }
        });


    }

    public void run()
    {
        if (mUpdating)
        {
            int positionSec = Math.round((float)mMediaInformationExchanger.getCurrentPositionMs() / 1000);
            if (positionSec != mCurrentPositionSec)
            {
                mCurrentPositionSec = positionSec;

                if (!mIsLiveStream)
                {
                    // Update the seekbar!
                    mSeekBar.setProgress((int) mCurrentPositionSec);
                    mDurationTextView.setText(Tools.convertSecToNiceTime(mDurationSec));
                    mCurrentPositionTextView.setText(Tools.convertSecToNiceTime(mCurrentPositionSec));
                }
                else
                {
                    mDurationTextView.setText(Tools.convertSecToNiceTime(mDurationSec) + " (DVR)");
                    mSeekBar.setProgress(mCurrentPositionSec);
                    if (mCurrentPositionSec < (mDurationSec-5))
                    {
                        mCurrentPositionTextView.setText("DVR");
                    }
                    else
                    {
                        mCurrentPositionTextView.setText("Live");
                    }
                }
            }

            int currentCachedDurationSec = Math.round((float)(mMediaInformationExchanger.getCurrentCachedDurationMs() / 1000));
            // Always update the secondary progress (since it may be running in the background already)
            if (currentCachedDurationSec != mCurrentCachedDurationSec)
            {
                mCurrentCachedDurationSec = currentCachedDurationSec;
                mSeekBar.setSecondaryProgress((int) mCurrentCachedDurationSec);
            }

        }
        
        if (mKeepRunning)
        {
            mHandler.postDelayed(MediaController.this, 1000);
        }
    }

    public void shutdown()
    {
        mFrameRateCalculator.shutdown();
        mKeepRunning = false;
    }

    public void setUpdating(boolean updating)
    {
        mUpdating = updating;

    }

    public void complete()
    {
        mDurationTextView.setText("");
        mCurrentPositionTextView.setText("Finished");
    }

}