import os

from enum import Enum

import dearpygui.dearpygui as dpg

from utils import *
from constants import *

def apply_alpha(color, divisor=2):
    r, g, b, a = color
    return (r, g, b, a / divisor)

def add_colors_to_theme(colors, theme):
    with dpg.theme_component(dpg.mvAll, parent=theme):
        for color_type in COLOR_TYPE_TO_DPG_ITEM:
            for item in COLOR_TYPE_TO_DPG_ITEM[color_type]:
                color = colors[color_type]
                if item in APPLY_ALPHA:
                    color = apply_alpha(color)
                dpg.add_theme_color(item, color, category=dpg.mvThemeCat_Core)

def add_styles_to_theme(theme):
    rounding = (
        dpg.mvStyleVar_ChildRounding,
        dpg.mvStyleVar_WindowRounding,
        dpg.mvStyleVar_FrameRounding,
        dpg.mvStyleVar_TabRounding,
        dpg.mvStyleVar_GrabRounding,
        dpg.mvStyleVar_ScrollbarRounding,
        dpg.mvStyleVar_PopupRounding
    )

    spacing = (
        dpg.mvStyleVar_ItemSpacing,
        dpg.mvStyleVar_ItemInnerSpacing,
        dpg.mvStyleVar_WindowPadding,
        dpg.mvStyleVar_FramePadding,
    )

    with dpg.theme_component(dpg.mvAll, parent=theme):
        for item in rounding:
            dpg.add_theme_style(item, ROUNDING, category=dpg.mvThemeCat_Core)
        
        for item in spacing:
            dpg.add_theme_style(item, SPACE, SPACE, category=dpg.mvThemeCat_Core)

        dpg.add_theme_style(dpg.mvStyleVar_ChildBorderSize, BORDER, category=dpg.mvThemeCat_Core)
        dpg.add_theme_style(dpg.mvStyleVar_WindowBorderSize, 0, category=dpg.mvThemeCat_Core)
        dpg.add_theme_style(dpg.mvStyleVar_FrameBorderSize, 0, category=dpg.mvThemeCat_Core)


def setup_fonts():
    font_registry = dpg.add_font_registry()
    montserrat_font = dpg.add_font(FONT_PATH, FONT_SIZE, parent=font_registry)
    dpg.bind_font(montserrat_font)

def apply_theme(theme_name):
    theme = dpg.add_theme()
    add_styles_to_theme(theme)
    add_colors_to_theme(THEMES[theme_name], theme)
    dpg.bind_theme(theme)

def setup_themes():
    apply_theme(get_current_theme())

def get_accent_color():
    return THEMES[get_current_theme()]['ACCENT']