package com.netfluke.sergey.greeter;

import android.app.Activity;
import android.content.Intent;
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;

public class MainActivity extends AppCompatActivity {

    public int REQ_CODE = 1001;

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

        // 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("CREATE INTENT", 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);
            }
        });
        */
    }

    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("EDIT");

        // 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){
            String name = data.getData().toString(); // note Studio's complaints here!

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

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

}
