package com.example.ar;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;

public class AccountRegistrationActivity1 extends Activity {
    private static String TAG = "AccountRegistration";
    private Resources res;
    private EditText name;
    private CheckBox over18;

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

        res = getResources();
        name = (EditText) findViewById(R.id.name);
        over18 = (CheckBox) findViewById(R.id.over18);

        name.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                setCheckBoxText(over18.isChecked());
            }
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) { }
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) { }
        });

        over18.setOnCheckedChangeListener(
                new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb,
                    boolean checked) {
                setCheckBoxText(checked);
            }
        });

        Button next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                EditText name = (EditText) findViewById(R.id.name);
                EditText phone = (EditText) findViewById(R.id.phone);
                EditText email = (EditText) findViewById(R.id.email);
                ToggleButton newsletter =
                        (ToggleButton) findViewById(R.id.newsletter);

                Log.v(TAG, "Name: " + name.getText());
                Log.v(TAG, "Phone: " + phone.getText());
                Log.v(TAG, "Email: " + email.getText());
                Log.v(TAG, "Newsletter: " + newsletter.isChecked());
                Log.v(TAG, "Over 18: " + over18.isChecked());

                Intent i = new Intent();
                i.setClass(AccountRegistrationActivity1.this,
                        AccountRegistrationActivity2.class);
                startActivity(i);
            }
        });
    }

    private void setCheckBoxText(boolean checked) {
        if (checked) {
            String age18Statement =
                    res.getString(R.string.age18statement);
            over18.setText(age18Statement);
        }
        else {
            String age18Question =
                    res.getString(R.string.age18question);
            over18.setText(String.format(age18Question,
                    name.getText()));
        }
    }
}