package com.revoapp.calendar;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.revoapp.api.utils.ImageHelper;
import com.revoapp.api.utils.Utils;
import com.revoapp.calendar.components.RevoButton;
import com.revoapp.calendar.helper.ThemeHelper;
import com.revoapp.calendarapp.R;

public class CalendarActivity extends TabActivity {
    private Context context;

    public static TabHost tabHost;
    public static RevoButton tab1, tab2, tab3;

    private static CalendarActivity me;
    private int orientation;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        me = this;

        setContentView(R.layout.calendar_activity);
        context = this;
        Utils.disableLockScreen(this);
        init();
    }

    public static void refresh() {
        if (me != null) {
            ((ParentActivity) me.getLocalActivityManager().getCurrentActivity())
                    .refreshView();
        }
    }

    private void init() {
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        orientation = getResources().getConfiguration().orientation;
        if (orientation == ImageHelper.LANDSCAPE) {
            tabHost.setBackgroundResource(ThemeHelper.get_background_tabhost());
        } else if (orientation == ImageHelper.PORTRAIT) {
            tabHost.setBackgroundResource(ThemeHelper.get_background_white());
        }

        tab1 = (RevoButton) findViewById(R.id.button_tab1);
        tab2 = (RevoButton) findViewById(R.id.button_tab2);
        tab3 = (RevoButton) findViewById(R.id.button_tab3);

        if (orientation == ImageHelper.LANDSCAPE) {
            tab1.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
                            .getDrawable(ThemeHelper.get_tab_icon_month_selector()), null,
                    null);
            tab2.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
                            .getDrawable(ThemeHelper.get_tab_icon_week_selector()), null,
                    null);
            tab3.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
                            .getDrawable(ThemeHelper.get_tab_icon_day_selector()), null,
                    null);
        } else if (orientation == ImageHelper.PORTRAIT) {
            tab1.setCompoundDrawablesWithIntrinsicBounds(getResources()
                    .getDrawable(ThemeHelper.get_tab_icon_month_selector()), null, null, null);
            tab1.setPadding(10, 0, 0, 10);
            tab1.setCompoundDrawablePadding(-10);
            tab2.setCompoundDrawablesWithIntrinsicBounds(getResources()
                    .getDrawable(ThemeHelper.get_tab_icon_week_selector()), null, null, null);
            tab2.setPadding(10, 0, 0, 10);
            tab2.setCompoundDrawablePadding(-10);
            tab3.setCompoundDrawablesWithIntrinsicBounds(getResources()
                    .getDrawable(ThemeHelper.get_tab_icon_day_selector()), null, null, null);
            tab3.setPadding(10, 0, 0, 10);
            tab3.setCompoundDrawablePadding(-10);
        }

        TabSpec month = tabHost.newTabSpec("month");
        TabSpec week = tabHost.newTabSpec("week");
        TabSpec day = tabHost.newTabSpec("day");

        month.setIndicator("month");
        month.setContent(new Intent(context, MonthActivity.class));

        week.setIndicator("week");
        week.setContent(new Intent(context, WeekActivity.class));

        day.setIndicator("day");
        day.setContent(new Intent(context, DayActivity.class));

        tabHost.addTab(month);
        tabHost.addTab(week);
        tabHost.addTab(day);

        tab1.performClick();
    }

    public void tabHandler(View target) {
        tab1.setSelected(false);
        tab2.setSelected(false);
        tab3.setSelected(false);
        if (target.getId() == R.id.button_tab1) {
            tabHost.setCurrentTab(0);
            tab1.setSelected(true);
        } else if (target.getId() == R.id.button_tab2) {
            tabHost.setCurrentTab(1);
            tab2.setSelected(true);
        } else if (target.getId() == R.id.button_tab3) {
            tabHost.setCurrentTab(2);
            tab3.setSelected(true);
        }
    }

    public static void setTab(int tabId) {
        switch (tabId) {
            case 0:
                tab1.performClick();
                break;
            case 1:
                tab2.performClick();
                break;
            case 2:
                tab3.performClick();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    @Override
    protected void onDestroy() {
        me = null;
        super.onDestroy();
    }
}