package com.zoblak.farm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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 java.lang.System;

public class MainScreen extends AppCompatActivity {



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



    }

    public static void setupAlarmManager(Context context) {
        AlarmManager alarmMgr;

        PendingIntent alarmIntent;

        Intent intent = new Intent(context, PeriodicalPingReceiver.class);

        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);


        int PERIOD_IN_MS = 2 * 60 * 1000; // 2 minutes

        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        // Hopefully your alarm will have a lower frequency than this!
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(),
                PERIOD_IN_MS, alarmIntent);

    }


    @Override
    protected void onResume() {
        super.onResume();
        SoundPlayingService.stopPlayingAlarm(this);
    }

    @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_screen, 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) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
