package com.revoapp.calendar.helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Typeface;

import com.revoapp.calendarapp.R;

public class ThemeHelper {
    public static int theme = 0;

    private static boolean prepared = false;

    private static Typeface primaryTypeface;
    private static Typeface secondaryTypeface;
    private static final String PACKAGE_SETTING = "com.ipmp.phone_setting";

    private static final int[] background_grey = new int[]{
            R.drawable.b0_background_grey};

    private static final int[] background_white = new int[]{
            R.drawable.b0_background_white};

    private static final int[] background_color = new int[]{
            R.color.b0_background};

    private static final int[] background_tabhost = new int[]{
            R.drawable.b0_background_tabhost};

    private static final int[] background_toast = new int[]{
            R.drawable.b0_background_toast};

    private static final int[] foreground_color = new int[]{
            R.color.b0_foreground};

    private static final int[] cell_sunday_color = new int[]{
            R.color.b0_cell_sunday};

    private static final int[] cell_today_color = new int[]{
            R.color.b0_cell_today};

    private static final int[] alarm_background = new int[]{
            R.drawable.b0_background_alarm};

    private static final int[] primary_colors = new int[]{
            R.color.b0_primary};

    private static final int[] secondary_colors = new int[]{
            R.color.b0_secondary};

    private static final int[] highlight_colors = new int[]{
            R.color.b0_highlight};

    private static final int[] button_selector = new int[]{
            R.drawable.b0_component_button_selector};

    private static final int[] background_dialogbox = new int[]{
            R.drawable.b0_background_dialogbox};

    private static final int[] button_dialogbox_close_selector = new int[]{
            R.drawable.b0_button_dialogbox_close_selector};

    private static final int[] icon50_ok = new int[]{
            R.drawable.b0_icon50_ok};

    private static final int[] icon50_cancel = new int[]{
            R.drawable.b0_icon50_cancel};

    private static final int[] icon50_menu_alarm = new int[]{
            R.drawable.b0_icon50_menu_alarm};

    private static final int[] icon50_menu_delete = new int[]{
            R.drawable.b0_icon50_menu_delete};

    private static final int[] icon50_menu_event = new int[]{
            R.drawable.b0_icon50_menu_event};

    private static final int[] icon50_menu_pill = new int[]{
            R.drawable.b0_icon50_menu_pill};

    private static final int[] tab_icon_day_selector = new int[]{
            R.drawable.b0_tab_icon_day_selector};

    private static final int[] tab_icon_week_selector = new int[]{
            R.drawable.b0_tab_icon_week_selector};

    private static final int[] tab_icon_month_selector = new int[]{
            R.drawable.b0_tab_icon_month_selector};

    private static final int[] icon48_edit = new int[]{
            R.drawable.b0_icon48_edit};

    private static final int[] separator_list = new int[]{
            R.drawable.b0_separator_list};

    private static final int[] button_color_selector = new int[]{
            R.color.b0_button_selector};

    private static final int[] component_textbox_selector = new int[]{
            R.drawable.b0_component_textbox_selector};

    private static final int[] component_cell_off = new int[]{
            R.drawable.b0_component_cell_off};

    private static final int[] component_cell_on = new int[]{
            R.drawable.b0_component_cell_on};

    private static final int[] component_cell_prevmonth = new int[]{
            R.drawable.b0_component_cell_prevmonth};

    private static final int[] component_cell = new int[]{
            R.drawable.b0_component_cell_selector};

    private static final int[] component_cell_today = new int[]{
            R.drawable.b0_component_cell_today_selector};

    private static final int[] component_combobox = new int[]{
            R.drawable.b0_component_combobox_selector};

    private static final int[] component_menu_item_selector = new int[]{
            R.drawable.b0_component_menu_item_selector};

    private static final int[] check_box_selector = new int[]{
            R.drawable.b0_component_checkbox_selector};

    private static final int[] icon50_plus = new int[]{
            R.drawable.b0_icon50_plus};

    private static final int[] icon50_minus = new int[]{
            R.drawable.b0_icon50_minus};

    private static final int[] icon_96_list = new int[]{
            R.drawable.b0_icon96_list_view};

    private static final int[] component_tab = new int[]{
            R.drawable.b0_component_tab_selector};

    private static final int[] component_tab_color = new int[]{
            R.color.b0_component_tab_selector};

    private static final int[] background_month_header = new int[]{
            R.drawable.b0_background_month_header};

    private static final int[] icon50_add = new int[]{
            R.drawable.b0_icon50_add};

    private static final int[] icon50_arrow_right = new int[]{
            R.drawable.b0_icon50_right_arrow};

    private static final int[] icon50_arrow_left = new int[]{
            R.drawable.b0_icon50_left_arrow};

    private static final int[] icon50_arrow_down = new int[]{
            R.drawable.b0_icon50_arrow_down};

    private static final int[] icon50_arrow_up = new int[]{
            R.drawable.b0_icon50_arrow_up};

    private static final int[] icon50_today = new int[]{
            R.drawable.b0_icon50_today};

    private static final int[] dialogbox_backgroud_add_event = new int[]{
            R.drawable.b0_background_dialogbox_add_event};

    private static final int[] icon100_ok = new int[]{
            R.drawable.b0_icon100_ok};

    private static final int[] icon100_cancel = new int[]{
            R.drawable.b0_icon100_cancel};

    private static final int[] icon100_arrow_right = new int[]{
            R.drawable.b0_icon100_arrow_right};

    private static final int[] icon200_ok = new int[]{
            R.drawable.b0_icon200_ok};

    private static final int[] icon200_cancel = new int[]{
            R.drawable.b0_icon200_cancel};

    private static final int[] icon_160_alarm_clock = new int[]{
            R.drawable.b0_icon160_alarm};

    private static final int[] icon_160_medicine_reminder = new int[]{
            R.drawable.b0_icon160_reminder};

    private static final int[] icon_160_calendar = new int[]{
            R.drawable.b0_icon160_calendar};

    public static int get_background_grey() {
        return background_grey[theme];
    }

    public static int get_background_white() {
        return background_white[theme];
    }

    public static int get_background_color() {
        return background_color[theme];
    }

    public static int get_foreground_color() {
        return foreground_color[theme];
    }

    public static int get_cell_sunday_color() {
        return cell_sunday_color[theme];
    }

    public static int get_cell_today_color() {
        return cell_today_color[theme];
    }

    public static int get_background_tabhost() {
        return background_tabhost[theme];
    }

    public static int get_background_toast() {
        return background_toast[theme];
    }

    public static int get_button_selector() {
        return button_selector[theme];
    }

    public static Typeface get_primary_font() {
        return primaryTypeface;
    }

    public static Typeface get_secondary_font() {
        return secondaryTypeface;
    }

    public static int get_primary_color() {
        return primary_colors[theme];
    }

    public static int get_secondary_color() {
        return secondary_colors[theme];
    }

    public static int get_highlight_color() {
        return highlight_colors[theme];
    }

    public static int get_background_dialogbox() {
        return background_dialogbox[theme];
    }

    public static int get_button_dialogbox_close_selector() {
        return button_dialogbox_close_selector[theme];
    }

    public static int get_icon50_ok() {
        return icon50_ok[theme];
    }

    public static int get_icon50_cancel() {
        return icon50_cancel[theme];
    }

    public static int get_icon50_menu_alarm() {
        return icon50_menu_alarm[theme];
    }

    public static int get_icon50_menu_delete() {
        return icon50_menu_delete[theme];
    }

    public static int get_icon50_menu_event() {
        return icon50_menu_event[theme];
    }

    public static int get_icon50_menu_pill() {
        return icon50_menu_pill[theme];
    }

    public static int get_tab_icon_day_selector() {
        return tab_icon_day_selector[theme];
    }

    public static int get_tab_icon_week_selector() {
        return tab_icon_week_selector[theme];
    }

    public static int get_tab_icon_month_selector() {
        return tab_icon_month_selector[theme];
    }

    public static int get_icon48_edit() {
        return icon48_edit[theme];
    }

    public static int get_separator_list() {
        return separator_list[theme];
    }

    public static int get_button_color_selector() {
        return button_color_selector[theme];
    }

    public static int get_component_textbox_selector() {
        return component_textbox_selector[theme];
    }

    public static int get_component_cell() {
        return component_cell[theme];
    }

    public static int get_component_cell_off() {
        return component_cell_off[theme];
    }

    public static int get_component_cell_on() {
        return component_cell_on[theme];
    }

    public static int get_component_cell_prevmonth() {
        return component_cell_prevmonth[theme];
    }

    public static int get_component_cell_today() {
        return component_cell_today[theme];
    }

    public static int get_component_combobox() {
        return component_combobox[theme];
    }

    public static int get_component_menu_item() {
        return component_menu_item_selector[theme];
    }

    public static int get_check_box_selector() {
        return check_box_selector[theme];
    }

    public static int get_icon50_minus() {
        return icon50_minus[theme];
    }

    public static int get_icon50_plus() {
        return icon50_plus[theme];
    }

    public static int get_alarm_background() {
        return alarm_background[theme];
    }

    public static int get_icon_96_list() {
        return icon_96_list[theme];
    }

    public static int get_icon_160_alarm_clock() {
        return icon_160_alarm_clock[theme];
    }

    public static int get_icon_160_medicine_reminder() {
        return icon_160_medicine_reminder[theme];
    }

    public static int get_icon_160_calendar() {
        return icon_160_calendar[theme];
    }

    public static int get_component_tab() {
        return component_tab[theme];
    }

    public static int get_component_tab_color() {
        return component_tab_color[theme];
    }

    public static int get_background_month_header() {
        return background_month_header[theme];
    }

    public static int get_icon50_add() {
        return icon50_add[theme];
    }

    public static int get_icon50_arrow_left() {
        return icon50_arrow_left[theme];
    }

    public static int get_icon50_arrow_right() {
        return icon50_arrow_right[theme];
    }

    public static int get_icon50_arrow_down() {
        return icon50_arrow_down[theme];
    }

    public static int get_icon50_arrow_up() {
        return icon50_arrow_up[theme];
    }

    public static int get_icon50_today() {
        return icon50_today[theme];
    }

    public static int get_dialogbox_backgroud_add_event() {
        return dialogbox_backgroud_add_event[theme];
    }

    public static int get_icon100_ok() {
        return icon100_ok[theme];
    }

    public static int get_icon100_cancel() {
        return icon100_cancel[theme];
    }

    public static int get_icon100_arrow_right() {
        return icon100_arrow_right[theme];
    }

    public static int get_icon200_ok() {
        return icon200_ok[theme];
    }

    public static int get_icon200_cancel() {
        return icon200_cancel[theme];
    }

    /**
     * Funkcja inicjująca klasę. Pobiera odpowiednie czcionki i ustawia wybraną
     * skórkę.
     *
     * @param context context aktywności
     */
    public static void prepare(Context context) {
        if (!prepared) {
            theme = getTheme(context);
            primaryTypeface = Typeface.createFromAsset(context
                    .getApplicationContext().getAssets(), "fonts/roboto.ttf");

            secondaryTypeface = Typeface.createFromAsset(context
                            .getApplicationContext().getAssets(),
                    "fonts/roboto_thin.ttf");

            prepared = true;
        }
    }

    /**
     * Funkcja sprawdzająca czy skórka została zmieniona. skórkę.
     *
     * @param context context aktywności
     */
    public static boolean isThemeChanged(Context context) {
        int tmpTheme = getTheme(context);

        if (theme != tmpTheme) {
            theme = tmpTheme;
            return true;
        } else
            return false;
    }

    /**
     * Funkcja sprawdzająca aktualnie wybraną skórkę aplikacji.
     *
     * @param context context aktywności
     */
    @SuppressLint("WorldReadableFiles")
    private static int getTheme(Context context) {
        int skin = 0;
        try {
            Context setting = context.createPackageContext(PACKAGE_SETTING,
                    Context.CONTEXT_IGNORE_SECURITY);

            String DATABASE_101A = "ST101Asystem";

            SharedPreferences preferences = setting.getSharedPreferences(
                    DATABASE_101A, (Context.MODE_WORLD_READABLE));

            //skin = Integer.valueOf(preferences.getString("PHONE_APP_THEME", "0"));

        } catch (NameNotFoundException e) {
        }
        return skin;
    }
}