package com.netfluke.sergey.greeter;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public int REQ_CODE = 1001;
    private boolean pressed;

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

        if (savedInstanceState == null)
            Log.d("BUNDLE onStart", "got NULL");
        else
            Log.d("BUNDLE onStart", savedInstanceState.toString());

        // This function runs when it gets the  android.intent.action.MAIN  from
        //  Android's Launcher app (with the category  android.intent.category.LAUNCHER;
        //  see MainActivity's intent-filter element in the AndroidManifest.
        // So we can print that Intent to the log, to see what it looks like;
        //  Android core classes typically have a useful toString() method on them.
        Intent init_intent = getIntent();
        Log.d("INTENT in onCreate", init_intent.toString());
        // Otherwise, this code serves no purpose, but it could call different handler functions
        //  depending on the TYPE of the Intent passed. See
        //    https://developer.android.com/training/sharing/index.html
        //    and https://developer.android.com/training/sharing/receive.html

        // Connect button 2 if it's not connected via XML attribute
        //    android:onClick="onButtonClick"
        // in the <Button ... > element of the activity_main.xml layout:
        /*
        Button b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonClick(view);
            }
        });
        */

        pressed = false;

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.DR");

        registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("RCV", intent.toString());
                pressed = true;
            }
        }, filter);

    }

    public void onButtonClick(View v) {
        Log.d("MAIN", "Clicked");

        // This Intent is explicit: it names the class to run directly.
        //Intent intent = new Intent(this, EditActivity.class);

        // This Intent is implicit: it merely specifies the action, and
        //   relies on EditActivity specifying the intent-filter for just
        //   that action in the AndroidManifest. Otherwise clicking the
        //   button will crash the app, and you will see a Java exception
        //   stack trace in Logcat!
        Intent intent = new Intent("ASK_NAME");

        // The line above created the intent object, i.e., packed the message.
        // Now send to send this message. REQ_CODE will be used to mark
        //  the response to this message (see onActivityResult below).
        startActivityForResult(intent, REQ_CODE);
        // Cf. https://developer.android.com/training/basics/intents/result.html
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        // Let's see the intent that comes back
        //   It may be null if the user presses the back button instead of
        //    completing the edit activity.
         if( data != null )
            Log.d("RESULT INTENT", data.toString());
        else
            Log.d("RESULT INTENT", "Got Null");

        // You get both the request code that marked the initial intent, and
        //   the result code set by the responding activity. The latter could be
        //   an error, in which case there could be no data to unpack.
        if( requestCode == REQ_CODE && resultCode ==  RESULT_OK){

            Log.d("MAIN", "Got OK back from Edit");

            String name = data.getData().toString(); // note Studio's complaints here!

            // add "Dr." depending on whether button
            if( pressed ){
                name = "Dr. " + name;
            }

            // Find the TextView element and reset the text it displays to include the name.
            TextView tv = (TextView) findViewById(R.id.txtV);
            tv.setText("Howdy " + name +"!"); // note Studio's complaints here!
        }
        else
            super.onActivityResult(requestCode, resultCode, data);
    }

    @Override protected void onStart(){
        Log.d("CYCLE", "onStart");
        super.onStart();
    }

    @Override protected void onResume(){
        Log.d("CYCLE", "onResume");
        super.onResume();
    }

    @Override protected void onPause(){
        Log.d("CYCLE", "onPause");
        super.onPause();
    }

    @Override protected void onStop(){
        Log.d("CYCLE", "onStop");
        super.onStop();
    }

    @Override protected void onDestroy(){
        Log.d("CYCLE", "onDestroy");
        super.onDestroy();
    }

     /*
    // This only works for activities with  FLAG_ACTIVITY_SINGLE_TOP  set (or
    //   launch mode "single top".
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        Log.d("ON NEW INTENT", intent.toString());
    } */

}
