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


import android.os.Bundle;
import android.widget.TextView;

import com.insidesecure.drm.android.reference.xoplayer.R;
import com.insidesecure.drmagent.v2.mediaplayer.CodecCounters;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class PlayMediaXOPlayerActivity extends PlayMediaSharedPlayerActivity {
    public static final String TAG = PlayMediaXOPlayerActivity.class.getSimpleName();

    private ScheduledExecutorService scheduleTaskExecutor;
    private CodecCounters mCodecCounters = new CodecCounters();
    private int mLastRenderedOutputBufferCount;
    protected TextView mRenderedBufferCountTxt;

    @Override
    protected int getContentView() {

        return R.layout.video;
    }


    @Override
    protected int getControlPanelLinearLayout() {

        return R.id.ControlPanel;
    }


    @Override
    protected int getSurfaceView() {

        return R.id.surface;
    }

    @Override
    protected int getBitrateAndBandWidthInfoLayout() {
        return R.id.bitrate_info_layout;
    }

    @Override
    protected int getBitrateInfoTxt() {
        return R.id.current_bitrate_txt;
    }

    @Override
    protected int getBandwidthInfoTxt() {
        return R.id.perceived_bandwidth_txt;
    }

    @Override
    protected int getStreamResolutionInfoTxt() {
        return R.id.stream_resolution_txt;
    }

    @Override
    protected int getCcAudioBoc() {

        return R.id.CC_Audio_Box;
    }


    @Override
    protected int getAudioLabel() {

        return R.id.Audio_Label;
    }

    @Override
    protected int getSpinnerAudio() {

        return R.id.Spinner_Audio;
    }


    @Override
    protected int getSpinnerSubtitle() {

        return R.id.Spinner_CC;
    }


    @Override
    protected int getSubtitleLabel() {

        return R.id.CC_Label;
    }

    @Override
    protected void enableSeekBarControl(final boolean enable) {

        if (onAudioSelectionFlag) {
            super.enableSeekBarControl(enable);
        }
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mRenderedBufferCountTxt = (TextView) findViewById(R.id.rendered_output_buffer_count_txt);

        scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
        scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        calculateCurrentRenderedFramePerSecond();
                    }
                });

            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    private void calculateCurrentRenderedFramePerSecond() {
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.updateCodecCounters(mCodecCounters);
            if (mLastRenderedOutputBufferCount != 0) {
                //Updating the Content Info layout
                updateContentInfoTxtView(mRenderedBufferCountTxt, "Rendered Buffer Count / Sec.: " + (mCodecCounters.mRenderedOutputBufferCount - mLastRenderedOutputBufferCount));
            }
            mLastRenderedOutputBufferCount = mCodecCounters.mRenderedOutputBufferCount;
        }
    }

    @Override
    protected void onDestroy() {
        scheduleTaskExecutor.shutdown();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        scheduleTaskExecutor.shutdown();
        super.onPause();
    }
}