package es.upvmaster.fullmediaplayer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ReproductorWeb extends AppCompatActivity {

    private WebView webView;
    private String tipo;
    private String ruta;

    protected PowerManager.WakeLock wakelock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final PowerManager pm=(PowerManager)getSystemService(Context.POWER_SERVICE);
        this.wakelock=pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "label");
        wakelock.acquire();

        setContentView(R.layout.activity_reproductor_web);

        Intent intent = getIntent();
        tipo = intent.getStringExtra("Tipo");
        ruta = intent.getStringExtra("Ruta");
        if (tipo.length()==0 || ruta.length()==0) finish();
        if (tipo.equals("LOCAL")) ruta = "file://" + ruta;

        webView = (WebView) this.findViewById(R.id.webView);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean aceleracionHardware = prefs.getBoolean("p_aceleracionHardware",true);
        if (aceleracionHardware)
            webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
        else
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        int SDK_INT = android.os.Build.VERSION.SDK_INT;
        if (SDK_INT > 16) {
            webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(false);
        webView.loadUrl(ruta);

        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                return false;
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView.destroy();
        this.wakelock.release();
        finish();
    }
}
