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

import android.util.Log;
import android.widget.SeekBar;

/**
 * @author
 */
public class SeekHandler
{
    private static final String TAG = SeekHandler.class.getSimpleName();
    private final SeekEventCallbackHandler mSeekEventCallbackHandler;
    private int mDesiredPosition;

    public static interface SeekEventCallbackHandler
    {
        public void seekStart();
        public void seekEnd(int position);
    }

    private SeekBar mSeekBar;

    public SeekHandler(final SeekBar seekBar, final SeekEventCallbackHandler seekEventCallbackHandler)
    {
        mSeekBar = seekBar;
        mSeekEventCallbackHandler = seekEventCallbackHandler;

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            public void onProgressChanged(final SeekBar seekBar, final int currentPosition, final boolean seekBarTracking)
            {
                Log.v(TAG, "Progress changed: " + currentPosition + (seekBarTracking ? "(tracking!)" : ""));
                if (seekBarTracking)
                {
                    mDesiredPosition = currentPosition;
                }
            }

            public void onStartTrackingTouch(final SeekBar seekBar)
            {
                Log.v(TAG, "SeekBar tracking started");
                seekEventCallbackHandler.seekStart();
            }

            public void onStopTrackingTouch(final SeekBar seekBar)
            {
                Log.v(TAG, "SeekBar tracking stopped @ " + mDesiredPosition);
                seekEventCallbackHandler.seekEnd(mDesiredPosition);
            }
        });

    }
}
