package com.example.converter;

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

public class MassConverterActivity extends MenuActivity {
    private ActionMode actionMode;

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

        View image = findViewById(R.id.massImage);
        final Callback callback = new ActionModeCallback();
        image.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {
                if (actionMode != null) {
                    return false;
                }
                actionMode = startActionMode(callback);
                return true;
            }
        });

        final Button poundsButton = 
                (Button) findViewById(R.id.poundsButton);
        final Button kgButton = (Button) findViewById(R.id.kgButton);
        final EditText massEditText =(EditText) findViewById(R.id.mass);
        final TextView result = (TextView) findViewById(R.id.result);

        OnClickListener listener = new OnClickListener() {
            public void onClick(View view) {
                Resources res = getResources();
                try {
                    double mass = Double.parseDouble(massEditText
                            .getText().toString());

                    if (((Button) view).getText().equals(
                            poundsButton.getText())) {
                        String p2k = res.getString(R.string.p2k_result);
                        double kg = mass * 0.45359237;
                        result.setText(String.format(p2k, mass, kg));
                    }
                    else if (((Button) view).getText().equals(
                            kgButton.getText())) {
                        String k2p = res.getString(R.string.k2p_result);
                        double p = mass / 0.45359237;
                        result.setText(String.format(k2p, mass, p));
                    }
                }
                catch (NumberFormatException nfe) {
                    result.setText(getString(R.string.error));
                }
            }
        };

        poundsButton.setOnClickListener(listener);
        kgButton.setOnClickListener(listener);

        if (getResources().getBoolean(R.bool.use_initial_mass)) {
            massEditText.setText(R.integer.initial_mass);
        }
    }

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

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

    class ActionModeCallback implements ActionMode.Callback {
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.contextmenu, menu);
            return true;
        }

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
            actionMode = null;
        }

        public boolean onActionItemClicked(ActionMode mode,
                MenuItem item) {
            switch (item.getItemId()) {
            case R.id.about:
                showAboutDialog();
                mode.finish();
                return true;
            case R.id.help:
                showContextSensitiveHelpDialog();
                mode.finish();
                return true;
            default:
                return false;
            }
        }
    }
}