package com.npaw.youbora.lib6.theoplayer.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Switch;

import com.npaw.youbora.lib6.YouboraLog;
import com.npaw.youbora.lib6.plugin.Options;
import com.npaw.youbora.lib6.plugin.Plugin;
import com.npaw.youbora.lib6.theoplayer.TheoplayerAdapter;
import com.npaw.youbora.lib6.theoplayer.TheoplayerAdsAdapter;
import com.npaw.youbora.lib6.utils.youboraconfigutils.YouboraConfigManager;

import com.theoplayer.android.api.THEOplayerView;
import com.theoplayer.android.api.player.RequestCallback;
import com.theoplayer.android.api.source.SourceDescription;
import com.theoplayer.android.api.source.addescription.THEOplayerAdDescription;
import com.theoplayer.android.api.timerange.TimeRanges;

public class MainActivity extends AppCompatActivity {
    private THEOplayerView tpv;
    private Plugin youboraPlugin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tpv = findViewById(R.id.theoplayer_view);
        setSource();

        // Testeable ui
        final Button buttonPlay = findViewById(R.id.play);
        final Button buttonPause = findViewById(R.id.pause);
        final Button buttonSeek = findViewById(R.id.seekButton);
        final Button buttonRefreshTimes = findViewById(R.id.refreshTimes);
        final Button buttonCheckAds = findViewById(R.id.checkAds);
        final CheckBox addDisplaying = findViewById(R.id.addDisplaying);
        final EditText seekTime = findViewById(R.id.seekTime);
        final EditText totalTime = findViewById(R.id.totalTime);
        final EditText actualTime = findViewById(R.id.currentTime);

        buttonPlay.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                 tpv.getPlayer().play();
            }
        });

        buttonPause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tpv.getPlayer().pause();
            }
        });

        buttonSeek.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tpv.getPlayer().setCurrentTime(Double.valueOf(seekTime.getText().toString()));
            }
        });

        buttonRefreshTimes.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                totalTime.setText(Double.toString(tpv.getPlayer().getDuration()));
                tpv.getPlayer().requestCurrentTime(new RequestCallback<Double>() {
                    @Override
                    public void handleResult(Double cTime) {
                        actualTime.setText(Double.toString(cTime));
                    }
                });
            }
        });

        buttonCheckAds.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tpv.getPlayer().getAds().requestPlaying(
                    new RequestCallback<Boolean>() {
                        @Override
                        public void handleResult(Boolean adIsDisplaying) {
                            addDisplaying.setChecked(adIsDisplaying);
                        }
                    }
                );
            }
        });

        initializeYoubora();
    }

    private void initializeYoubora() {
        YouboraLog.setDebugLevel(YouboraLog.Level.VERBOSE);
        Options youboraOptions = YouboraConfigManager.Companion.getInstance().getOptions(this);
        youboraPlugin = new Plugin(youboraOptions, this);
        youboraPlugin.setAdapter(new TheoplayerAdapter(tpv));
        youboraPlugin.setAdsAdapter(new TheoplayerAdsAdapter(tpv.getPlayer()));
    }

    private void setSource() {
        SourceDescription sourceDescription = SourceDescription.Builder.sourceDescription(
                getIntent().getStringExtra("url"))
                .ads(
                        THEOplayerAdDescription.Builder.adDescription(
                                "https://cdn.theoplayer.com/demos/preroll.xml")
                                .timeOffset("0")
                                .skipOffset("3")
                                .build(),
                        THEOplayerAdDescription.Builder.adDescription(
                                "https://cdn.theoplayer.com/demos/preroll.xml")
                                .timeOffset("50")
                                .skipOffset("3")
                                .build(),
                        THEOplayerAdDescription.Builder.adDescription(
                                "https://cdn.theoplayer.com/demos/preroll.xml")
                                .timeOffset("100")
                                .skipOffset("3")
                                .build())
                .build();
        tpv.getPlayer().setSource(sourceDescription);
    }

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

    @Override
    protected void onResume() {
        super.onResume();
        tpv.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.youbora_options) {
            YouboraConfigManager.Companion.getInstance().showConfig(this);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
