package com.example.converter;

import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;

public class TemperatureConverterActivity extends MenuActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperature);

        View image = findViewById(R.id.temperatureImage);
        image.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {
                PopupMenu popup = new PopupMenu(
                        TemperatureConverterActivity.this, view);
                popup.setOnMenuItemClickListener(new PopupListener());
                popup.inflate(R.menu.contextmenu);
                popup.show();
                return true;
            }
        });

        final Button fahrButton = (Button)findViewById(R.id.fahrButton);
        final Button celButton = 
                (Button) findViewById(R.id.celsiusButton);
        final EditText temperatureEditText = 
                (EditText) findViewById(R.id.temperature);
        final TextView result = (TextView) findViewById(R.id.result);

        class MyButtonListener implements OnClickListener {
            public void onClick(View view) {
                Resources res = getResources();
                try {
                    double temp = Double
                            .parseDouble(temperatureEditText.getText()
                                    .toString());

                    if (((Button) view).getText().equals(
                            fahrButton.getText())) {
                        double c = (5.0 / 9.0) * (temp - 32);
                        String f2c = res.getString(R.string.f2c_result, temp, c);
                        result.setText(f2c);
                    }
                    else if (((Button) view).getText().equals(
                            celButton.getText())) {
                        double f = (9.0 / 5.0) * temp + 32;
                        String c2f = res.getString(R.string.c2f_result, temp, f);
                        result.setText(c2f);
                    }
                }
                catch (NumberFormatException nfe) {
                    result.setText(getString(R.string.error));
                }
            }
        }

        MyButtonListener listener = new MyButtonListener();
        fahrButton.setOnClickListener(listener);
        celButton.setOnClickListener(listener);

        if (getResources().getBoolean(R.bool.use_initial_temp)) {
            temperatureEditText.setText(R.integer.initial_temp);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.findItem(R.id.temperature_converter).setEnabled(false);
        return true;
    }

    @Override
    protected void showContextSensitiveHelpDialog() {
        showTemperatureHelpDialog();
    }

    class PopupListener implements PopupMenu.OnMenuItemClickListener {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.about:
                showAboutDialog();
                return true;
            case R.id.help:
                showContextSensitiveHelpDialog();
                return true;
            default:
                return false;
            }
        }
    }
}