package com.jcf.sportpager;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.firebase.iid.FirebaseInstanceId;

public class MainActivity extends AppCompatActivity //implements android.view.View.OnClickListener
{
    private final static String TAG = "SPORTPAGER MainActivity";
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private TextView dbgmsg;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setContentView(R.layout.splash);

        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    startActivity(new Intent(MainActivity.this, Navegador.class));
                    finish();
                    /*
                    Intent intent = new Intent(MainActivity.this,MainActivity.class);
                    startActivity(intent);
                    */
                }
            }
        };
        timerThread.start();

        checkPlayServices();

        /*
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        findViewById(R.id.btn_registrarse).setOnClickListener(this);
        findViewById(R.id.btn_navegador).setOnClickListener(this);
        dbgmsg = (TextView) findViewById(R.id.dbgmsg);
        */
        /*
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        */

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        //finish();
    }


    @Override
    protected void onResume()
    {
        super.onResume();
        checkPlayServices();

        String dispositivoId = MyFirebaseInstanceIdService.getFBToken();
        if(dispositivoId != null)
        {
            //dbgmsg.setText(dispositivoId);
            Log.i("dispositivoId", dispositivoId);
        }
    }

    /*
    @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.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    */

    private boolean checkPlayServices()
    {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS)
        {
            if (apiAvailability.isUserResolvableError(resultCode))
            {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                        .show();
            }
            else
            {
                Log.i(TAG, "Este dispositivo no es compatible con Google Play services");
                finish();
            }
            return false;
        }
        return true;
    }

    /*
    @Override
    public void onClick(View v)
    {
        switch (v.getId()) {
            case R.id.btn_registrarse:
                 startActivity(new Intent(this, SignInActivity.class));
                break;
            case R.id.btn_navegador:
                startActivity(new Intent(this, Navegador.class));
                break;
            // ...
        }
    }
    */
}
