package com.hellosanket.sol;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private boolean mServiceWarmedUp = false;

    private void getSolarTimes() {
        Intent intent = new Intent(MainActivity.this, MainService.class);
        intent.setAction(MainService.ACTION_GET_SOL_TIMES);
        startService(intent);

    }

    private boolean tryPermission() {
        if ((ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    Constants.PERMISSIONS_REQUEST_LOCATION);
            return false;
        }
        return true;
    }

    // TODO handle screen orientation changes
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setBackgroundColor((getResources().getColor(R.color.main_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();
            }
        });

        // start service in advance
        Intent intent = new Intent(MainActivity.this, MainService.class);
        startService(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!mServiceWarmedUp){
           getSolarTimes();
            mServiceWarmedUp = true;
        }

        // TODO refresh UI
    }

    on

    @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);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        L.d(TAG, "onRequestPermissionsResult");

        switch (requestCode) {
            case Constants.PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    L.d(TAG,"Granted Permission");
                    getSolarTimes();
                } else {
                    Toast.makeText(this, "No Location, no reminder :'(", Toast.LENGTH_SHORT).show();
                }
                return;
            }
        }
    }


}

@@ -657,16 +657,90 @@
 false;%0A%0A
+    /*---------------- LOCAL METHODS ----------------------------------*/%0A
     priv
@@ -933,25 +933,24 @@
 nt);%0A%0A    %7D%0A
-%0A
     private 
@@ -1414,54 +1414,252 @@
   %7D%0A
-%0A
     
-// TODO handle screen orientation changes
+private void restoreOnCreate(Bundle savedInstanceState) %7B%0A        mServiceWarmedUp = savedInstanceState.getBoolean(%22mServiceWarmedUp%22);%0A    %7D%0A%0A%0A    /*-------------------------------- OVERRIDDEN METHODS ------------------------------------*/
 %0A   
@@ -2386,24 +2386,70 @@
        %7D);%0A%0A
+        if (savedInstanceState == null) %7B%0A    
         // s
@@ -2472,16 +2472,20 @@
 advance%0A
+    
         
@@ -2550,32 +2550,36 @@
 class);%0A        
+    
 startService(int
@@ -2584,16 +2584,92 @@
 ntent);%0A
+        %7D else %7B%0A            restoreOnCreate(savedInstanceState);%0A        %7D%0A
     %7D%0A%0A 
@@ -2774,32 +2774,33 @@
 Up)%7B%0A           
+ 
 getSolarTimes();
@@ -2886,16 +2886,8 @@
  %7D%0A%0A
-    on%0A%0A
     
@@ -4399,11 +4399,200 @@
 %0A    %7D%0A%0A
+    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        outState.putBoolean(%22mServiceWarmedUp%22, mServiceWarmedUp);%0A        super.onSaveInstanceState(outState);%0A    %7D
 %0A%7D%0A

handle orientation change