package org.example.proyecto;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends AppCompatActivity {

    public static SharedPreferences pref;
    public static AdaptadorCiudades adaptador;
    public static Activity activity;
    public static AlmacenCiudadesPreferencias almacen;


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

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

        activity = MainActivity.this;

        pref = PreferenceManager.getDefaultSharedPreferences(this);

        Aplicacion app = (Aplicacion) getApplication();
        adaptador = app.getAdaptador();

        almacen = adaptador.almacen;

        adaptador.actualizarAlmacen(MainActivity.this,this,adaptador,false);
        establecerAlarmaDeporte();

        adaptador.notifyDataSetChanged();

    }

    @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) {
            lanzarPreferencias(null);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void lanzarMisCiudades(View v) {
        Intent i = new Intent(this, MisCiudades.class);
        startActivity(i);
    }

    public void lanzarPreferencias(View view) {
        Intent i = new Intent(this, Preferencias.class);
        startActivity(i);
    }

    public void lanzarClima(View v) {
        Intent i = new Intent(this, Clima.class);
        i.putExtra("posicion", 0);
        startActivity(i);
    }

    public void lanzarAcercaDe(View v) {
        Intent i = new Intent(this, AcercaDe.class);
        startActivity(i);
    }

    private void establecerAlarmaDeporte() {
        AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

        int interval = 1000*60*60*1; //1 hora

        Intent intent = new Intent(this, MyReceiver.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        manager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),interval, pIntent);
    }

    @Override
    public void onResume(){

        if (adaptador.vectorCiudades == null || adaptador.vectorCiudades.size() == 0)
            adaptador.almacen.getVectorCiudades(adaptador);
        adaptador.actualizarAlmacen(MainActivity.this,this,adaptador,false);
        adaptador.notifyDataSetChanged();

        super.onResume();

    }

    @Override
    public void onDestroy(){

        almacen.actualizarVectorCiudades(adaptador);

        super.onDestroy();

    }

}