package @APP_PACKAGE@;

import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaMetadata;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Build;
import android.util.Log;

/**
 * Created by Christian BROWET on 1/10/17.
 */

public class XBMCMediaSession
{
  native void _onPlayRequested();

  native void _onPauseRequested();

  native void _onNextRequested();

  native void _onPreviousRequested();

  native void _onForwardRequested();

  native void _onRewindRequested();

  native void _onStopRequested();

  native void _onSeekRequested(long pos);

  native boolean _onMediaButtonEvent(Intent intent);

  private static final String TAG = "@APP_NAME@";

  private class XBMCMediaSessionCallback extends MediaSession.Callback
  {

    @Override
    public void onPlay()
    {
      Log.d(TAG, "XBMCMediaSession.onPlay: ");
      super.onPlay();
      _onPlayRequested();
    }

    @Override
    public void onPause()
    {
      Log.d(TAG, "XBMCMediaSession.onPause: ");
      super.onPause();
      _onPauseRequested();
    }

    @Override
    public void onSkipToNext()
    {
      Log.d(TAG, "XBMCMediaSession.onSkipToNext: ");
      super.onSkipToNext();
      _onNextRequested();
    }

    @Override
    public void onSkipToPrevious()
    {
      Log.d(TAG, "XBMCMediaSession.onSkipToPrevious: ");
      super.onSkipToPrevious();
      _onPreviousRequested();
    }

    @Override
    public void onFastForward()
    {
      Log.d(TAG, "XBMCMediaSession.onFastForward: ");
      super.onFastForward();
      _onForwardRequested();
    }

    @Override
    public void onRewind()
    {
      Log.d(TAG, "XBMCMediaSession.onRewind: ");
      super.onRewind();
      _onRewindRequested();
    }

    @Override
    public void onStop()
    {
      Log.d(TAG, "XBMCMediaSession.onStop: ");
      super.onStop();
      _onStopRequested();
    }

    @Override
    public void onSeekTo(long pos)
    {
      Log.d(TAG, "XBMCMediaSession.onSeekTo: ");
      super.onSeekTo(pos);
      _onSeekRequested(pos);
    }

    @Override
    public boolean onMediaButtonEvent(Intent intent)
    {
      Log.d(TAG, "XBMCMediaSession.onMediaButtonEvent: ");
      if (!_onMediaButtonEvent(intent))
        return super.onMediaButtonEvent(intent);
      return true;
    }
  }

  private MediaSession mSession = null;
  private XBMCMediaSessionCallback mMediaSessionCallback;
  private long mTotDurMs = 0;

  public XBMCMediaSession()
  {
    Log.d(TAG, "XBMCMediaSession init");
    this.mSession = new MediaSession(Main.MainActivity, "XBMC_session");
    if (Build.VERSION.SDK_INT < 26) {
      this.mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    }

    Main.MainActivity.runOnUiThread(new Runnable()
    {
      @Override
      public void run()
      {
        Log.d(TAG, "XBMCMediaSession callback");
        mMediaSessionCallback = new XBMCMediaSessionCallback();
        mSession.setCallback(mMediaSessionCallback);
      }
    });
  }

  public void activate(boolean state)
  {
    mSession.setActive(state);
  }

  private void updatePlaybackState(PlaybackState mystate)
  {
    mSession.setPlaybackState(mystate);
  }

  private void updateMetadata(MediaMetadata myData)
  {
    mSession.setMetadata(myData);
  }

  private void updateIntent(Intent intent)
  {
    PendingIntent pi = PendingIntent.getActivity(Main.MainActivity, 99 /*request code*/,
            intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    mSession.setSessionActivity(pi);
  }

}
