package com.example.countdown;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class CountdownActivity extends Activity implements
        EventDateListener, EventNameListener {
    private static final int MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;

    private String eventName = "Christmas";
    private Calendar eventCalendar = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // default to this year's Christmas
        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        eventCalendar = new GregorianCalendar(thisYear,
                Calendar.DECEMBER, 25);
        setText();

        TextView textView = (TextView) findViewById(R.id.countdown);
        textView.setTextColor(getResources().getColor(R.color.white));
        getWindow().setBackgroundDrawableResource(R.color.black);

    }

    private void setText() {
        int daysUntilEvent = getDaysUntilEvent(eventCalendar);
        TextView text = (TextView) findViewById(R.id.countdown);

        if (daysUntilEvent == 1) {
            text.setText(getString(R.string.countdownSingular,
                    daysUntilEvent, eventName));
        }
        else {
            text.setText(getString(R.string.countdown, daysUntilEvent,
                    eventName));
        }
    }

    private int getDaysUntilEvent(Calendar event) {
        GregorianCalendar nowCal = new GregorianCalendar();
        long eventTimeStamp = event.getTime().getTime();
        long nowTimeStamp = nowCal.getTime().getTime();
        return (int) Math.ceil((eventTimeStamp - nowTimeStamp)
                / (double) MILLISECONDS_PER_DAY);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
        case R.id.event_date:
            DialogFragment f = new EventDateDialogFragment();
            f.show(getFragmentManager(), "event-date");
            break;
        case R.id.event_name:
            DialogFragment f2 = new EventNameDialogFragment();
            f2.show(getFragmentManager(), "event-name");
            break;
        case R.id.bg_color_black:
        case R.id.bg_color_gray:
        case R.id.bg_color_blue:
        case R.id.bg_color_white:
            item.setChecked(true);
            changeBGColor(id);
            break;
        case R.id.fg_color_black:
        case R.id.fg_color_gray:
        case R.id.fg_color_blue:
        case R.id.fg_color_white:
            item.setChecked(true);
            changeFGColor(id);
            break;
        default:
            super.onOptionsItemSelected(item);
        }
        return true;
    }

    private void changeBGColor(int itemId) {
        if (itemId == R.id.bg_color_black) {
            System.out.println("Black BG");
            getWindow().setBackgroundDrawableResource(R.color.black);
        }
        else if (itemId == R.id.bg_color_gray) {
            System.out.println("Gray BG");
            getWindow().setBackgroundDrawableResource(R.color.gray);
        }
        else if (itemId == R.id.bg_color_blue) {
            getWindow().setBackgroundDrawableResource(R.color.blue);
        }
        else if (itemId == R.id.bg_color_white) {
            getWindow().setBackgroundDrawableResource(R.color.white);
        }
    }

    private void changeFGColor(int itemId) {
        TextView textView = (TextView) findViewById(R.id.countdown);
        if (itemId == R.id.fg_color_black) {
            textView.setTextColor(getResources()
                    .getColor(R.color.black));
        }
        else if (itemId == R.id.fg_color_gray) {
            textView.setTextColor(getResources().getColor(R.color.gray));
        }
        else if (itemId == R.id.fg_color_blue) {
            textView.setTextColor(getResources().getColor(R.color.blue));
        }
        else if (itemId == R.id.fg_color_white) {
            textView.setTextColor(getResources()
                    .getColor(R.color.white));
        }
    }

    @Override
    public void onEventDateChosen(Calendar calendar) {
        eventCalendar = calendar;
        setText();
    }

    @Override
    public void onEventNameChosen(String name) {
        eventName = name;
        setText();
    }
}