package com.example.login;

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

public class LoginActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        final EditText user = (EditText) findViewById(R.id.username);
        final EditText pw1 = (EditText) findViewById(R.id.password);
        final EditText pw2 = (EditText) findViewById(R.id.password2);

        Intent intent = getIntent();
        boolean confirmPassword = intent.getBooleanExtra(
                "ConfirmPassword", true);
        if (confirmPassword) {
            pw2.setVisibility(View.VISIBLE);
        }
        else {
            pw2.setVisibility(View.GONE);
        }

        Button login = (Button) findViewById(R.id.login);
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = user.getText().toString();
                String password1 = pw1.getText().toString();
                String password2 = pw2.getText().toString();

                if (password1.equals(password2)) {
                    Intent intent = new Intent();
                    intent.putExtra("Username", userName);
                    intent.putExtra("Password", password1);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }
                else {
                    DialogFragment f = new PasswordMatchDialogFragment();
                    f.show(getFragmentManager(), "pw-match");
                }
            }
        });
    }
}