package com.example.countdown;

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

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

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

    private String eventName = "Christmas";
    private Calendar eventCalendar = Calendar.getInstance();
    private SharedPreferences prefs;
    public static final String DATE_PREF_KEY = "EVENT_TIME";
    private static final String NAME_PREF_KEY = "EVENT_NAME";
    private static final String BG_PREF_KEY = "BG_COLOR";
    private static final String FG_PREF_KEY = "FG_COLOR";
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        setPage();
        // Register listener for preference changes
        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        	@Override
    		public void onSharedPreferenceChanged(SharedPreferences prefs,
    				String key) {
        		setPage();
    		}
    	}; 
    	PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(prefListener);
    }
    
    private void setPage() {
        // default to this year's Christmas
        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        eventCalendar =
                new GregorianCalendar(thisYear, Calendar.DECEMBER, 25);
        long timeVal = eventCalendar.getTimeInMillis();
        timeVal = prefs.getLong(DATE_PREF_KEY, timeVal);
        eventCalendar.setTimeInMillis(timeVal);
        eventName = prefs.getString(NAME_PREF_KEY, eventName);
        setText();
        String resName = prefs.getString(FG_PREF_KEY, "fg_color_white");
        changeFGColor(resName);
        resName = prefs.getString(BG_PREF_KEY, "bg_color_black");
        changeBGColor(resName);
    }

    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.preferences:
  		  startActivity(new Intent("com.example.countdown.PREFERENCES"));
          break;
        default:
            super.onOptionsItemSelected(item);
        }
        return true;
    }


    private void changeBGColor(String resName) {
        if (resName.equals("bg_color_black")) {
            getWindow().setBackgroundDrawableResource(R.color.black);
        }
        else if (resName.equals("bg_color_gray")) {
            getWindow().setBackgroundDrawableResource(R.color.gray);
        }
        else if (resName.equals("bg_color_blue")) {
            getWindow().setBackgroundDrawableResource(R.color.blue);
        }
        else if (resName.equals("bg_color_white")) {
            getWindow().setBackgroundDrawableResource(R.color.white);
        }
    }

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