package com.example.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 1;
    private static final String LOGIN_ACTION = "com.example.LOGIN";

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

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Explicit Intent
                Intent intent =
                        new Intent(MainActivity.this,
                                LoginActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Explicit Intent passing in extras
                Intent intent = new Intent();
                intent.setAction(LOGIN_ACTION);
                intent.putExtra("ConfirmPassword", false);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Implicit Intent
                Intent intent = new Intent();
                intent.setAction(LOGIN_ACTION);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                String user = data.getStringExtra("Username");
                String pw = data.getStringExtra("Password");

                if (user.equals(pw)) {
                    Toast.makeText(this, R.string.success,
                            Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(this, R.string.failure,
                            Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}