package com.example.tips;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TipCalculatorActivity extends Activity {
    private EditText billAmountEditText;
    private RadioGroup rg;

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

        billAmountEditText = (EditText) findViewById(R.id.billAmount);
        billAmountEditText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                updateTextViews();
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }
        });

        rg = (RadioGroup) findViewById(R.id.tipRadioGroup);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group,
                    int checkedId) {
                updateTextViews();
            }
        });
    }

    private void updateTextViews() {
        Resources res = getResources();
        TextView tipAmountTextView =
                (TextView) findViewById(R.id.tipAmount);
        TextView totalBillTextView =
                (TextView) findViewById(R.id.totalBill);
        RadioButton rb =
                (RadioButton) findViewById(rg
                        .getCheckedRadioButtonId());

        try {
            float billAmount =
                    Float.parseFloat(billAmountEditText.getText()
                            .toString());
            // Use split function to remove % sign from String
            float tipPercent =
                    Float.parseFloat(rb.getText().toString()
                            .split("%")[0]);
            tipPercent = tipPercent / 100;

            float tip = billAmount * tipPercent;
            float total = billAmount + tip;

            String tipLabel =
                    res.getString(R.string.tip_amount_label2);
            String totalLabel =
                    res.getString(R.string.total_bill_label2);
            tipAmountTextView.setText(String.format(tipLabel, tip));
            totalBillTextView.setText(String
                    .format(totalLabel, total));
        }
        catch (NumberFormatException e) {
            tipAmountTextView.setText(R.string.tip_amount_label);
            totalBillTextView.setText(R.string.total_bill_label);
            System.err.println(e);
        }
    }
}