package com.example.converter;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public abstract class MenuActivity extends Activity {
    private static int backgroundColor = R.color.black;
    private static boolean enableContextSensitiveHelp = true;

    /*
     * Make sure each activity is using the proper background color 
     * when it is displayed.
     */
    @Override
    protected void onResume() {
        super.onResume();
        getWindow().setBackgroundDrawableResource(
                MenuActivity.backgroundColor);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.optionsmenu, menu);
        return true;
    }

    /*
     * Set the current checked radio button in the color picker menu 
     * to match the value of the static backgroundColor field.
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        switch (MenuActivity.backgroundColor) {
        case R.color.black:
            menu.findItem(R.id.color_black).setChecked(true);
            break;
        case R.color.gray:
            menu.findItem(R.id.color_gray).setChecked(true);
            break;
        case R.color.blue:
            menu.findItem(R.id.color_blue).setChecked(true);
            break;
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case R.id.temperature_converter:
            showTemperatureActivity();
            break;
        case R.id.distance_converter:
            showDistanceActivity();
            break;
        case R.id.mass_converter:
            showMassActivity();
            break;
        case R.id.help_distance:
            showDistanceHelpDialog();
            break;
        case R.id.help_mass:
            showMassHelpDialog();
            break;
        case R.id.help_temperature:
            showTemperatureHelpDialog();
            break;
        case R.id.context_sensitive_help_enabled:
            MenuActivity.enableContextSensitiveHelp = !item.isChecked();
            item.setChecked(!item.isChecked());
            break;
        case R.id.color_black:
        case R.id.color_gray:
        case R.id.color_blue:
            item.setChecked(true);
            changeColor(id);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    public void aboutClicked(MenuItem mi) {
        showAboutDialog();
    }

    private void changeColor(int itemId) {
        if (itemId == R.id.color_black) {
            MenuActivity.backgroundColor = R.color.black;
        }
        else if (itemId == R.id.color_gray) {
            MenuActivity.backgroundColor = R.color.gray;
        }
        else if (itemId == R.id.color_blue) {
            MenuActivity.backgroundColor = R.color.blue;
        }
        getWindow().setBackgroundDrawableResource(
                MenuActivity.backgroundColor);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        if (MenuActivity.enableContextSensitiveHelp) {
            super.onCreateContextMenu(menu, v, menuInfo);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.contextmenu, menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            showAboutDialog();
            break;
        case R.id.help:
            showContextSensitiveHelpDialog();
            break;
        default:
            return super.onContextItemSelected(item);
        }
        return true;
    }

    protected abstract void showContextSensitiveHelpDialog();

    protected void showAboutDialog() {
        Bundle args = new Bundle();
        Resources r = getResources();
        args.putString("title", "");
        args.putString("text", r.getString(R.string.about_text));

        DialogFragment fragment = new ConverterDialogFragment();
        fragment.setArguments(args);
        fragment.show(getFragmentManager(), "about");
    }

    protected void showDistanceHelpDialog() {
        Bundle args = new Bundle();
        Resources r = getResources();
        args.putString("title",
                r.getString(R.string.distance_converter));
        args.putString("text",r.getString(R.string.help_distance_text));

        DialogFragment fragment = new ConverterDialogFragment();
        fragment.setArguments(args);
        fragment.show(getFragmentManager(), "distance-help");
    }

    protected void showMassHelpDialog() {
        Bundle args = new Bundle();
        Resources r = getResources();
        args.putString("title", r.getString(R.string.mass_converter));
        args.putString("text", r.getString(R.string.help_mass_text));

        DialogFragment fragment = new ConverterDialogFragment();
        fragment.setArguments(args);
        fragment.show(getFragmentManager(), "mass-help");
    }

    protected void showTemperatureHelpDialog() {
        Bundle args = new Bundle();
        Resources r = getResources();
        args.putString("title",
                r.getString(R.string.temperature_converter));
        args.putString("text", r.getString(R.string.help_temp_text));

        DialogFragment fragment = new ConverterDialogFragment();
        fragment.setArguments(args);
        fragment.show(getFragmentManager(), "temperature-help");
    }

    protected void showDistanceActivity() {
        Intent i = new Intent(MenuActivity.this,
                DistanceConverterActivity.class);
        startActivity(i);
    }

    protected void showTemperatureActivity() {
        Intent i = new Intent(MenuActivity.this,
                TemperatureConverterActivity.class);
        startActivity(i);
    }

    protected void showMassActivity() {
        Intent i = new Intent(MenuActivity.this,
                MassConverterActivity.class);
        startActivity(i);
    }
}
