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 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.source.SourceDescription;
import com.theoplayer.android.api.source.addescription.THEOplayerAdDescription;

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();

        initializeYoubora();
    }

    private void initializeYoubora() {
        YouboraLog.setDebugLevel(YouboraLog.Level.VERBOSE);
        Options youboraOptions = YouboraConfigManager.Companion.getInstance().getOptions(this);
        youboraPlugin = new Plugin(youboraOptions);
        youboraPlugin.setActivity(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);
    }
}
