from std/os import symlinkExists, getConfigDir, walkDirs, `/`
from std/envvars import existsEnv, getEnv, delEnv, putEnv
from std/options import Option, some, get, isNone
from std/strformat import fmt
import pkg/owlkettle
import ./themes

const
  # Set the directory path where icons are stored
  iconsDir = "/usr/share/tromjaro-theme-switcher/icons"
  styleIconsDir = iconsDir / "Styles"
  accentIconsDir = iconsDir / "Accent Colors"
  # GTK CSS for overriding the default icon size of buttons
  gtkCSS = """
.style-button {
  min-width: 5.5em;
  min-height: 5em;
  padding: 0.3em;
  border-radius: 14%;
}

.accent-button {
  -gtk-icon-size: 1.65em;
  padding: 0.4em;
  border-radius: 50%;
}
"""

var iconsDirs: seq[string]

for dir in walkDirs(accentIconsDir / "*"):
  iconsDirs.add(dir)

# Function prototypes
proc makeThemeButton(shadeName: string, styleName: string): Widget
proc makeAccentButton(accentColor: string): Widget
proc accentButtons(): Widget
proc themeButtons(): Widget
proc mainGUI(): Widget

# Keep track of current theme button
var currentTheme = getCurrentTheme()

var
  oldConfigDir: Option[string]
  configDirChanged: bool

# Prevent loading GTK theme from ~/.config/gtk-4.0 directory when it is a symlink
if symlinkExists(getConfigDir() / "gtk-4.0"):
  if existsEnv("XDG_CONFIG_HOME"):
    oldConfigDir = some(getEnv("XDG_CONFIG_HOME"))
  putEnv("XDG_CONFIG_HOME", "/dev/null")
  configDirChanged = true

# Display the GTK-4 GUI using owlkettle

viewable App:
  hooks:
    build:
      # Reset the user's XDG_CONFIG_HOME variable back to what it was before
      if configDirChanged == true:
        if oldConfigDir.isNone():
          delEnv("XDG_CONFIG_HOME")
        else:
          putEnv("XDG_CONFIG_HOME", get(oldConfigDir))

method view(app: AppState): Widget =
  result = mainGUI()

brew(appID, gui(App()), icons=iconsDirs, stylesheets=[newStylesheet(gtkCSS)])


# Function declarations

proc makeThemeButton(shadeName: string, styleName: string): Widget =
  let
    shade = if shadeName == "": "" else: fmt"-{shadeName}"
    style = if styleName == "Default": "" else: fmt"-{styleName}"
    color = if currentTheme.color in ["Blue", ""]: "" else: fmt"-{currentTheme.color}"
    themeString = fmt"Colloid{color}{shade}{style}"

  result = gui:
    Button:
      Picture:
        pixbuf = loadPixbuf(fmt"{styleIconsDir}/Colloid{shade}{style}.svg")
      tooltip = if shadeName == "": styleName else: fmt"{shadeName}-{styleName}"
      style = if (shadeName, styleName) == (currentTheme.shade, currentTheme.style):
        [StyleClass("style-button"), ButtonSuggested]
      else:
        [StyleClass("style-button"), ButtonFlat]

      proc clicked() =
        setTheme(themeString)
        # Highlight this button
        currentTheme.shade = shadeName
        currentTheme.style = styleName
        if currentTheme.color == "":
          currentTheme.color = "Blue"

proc makeAccentButton(accentColor: string): Widget =
  let
    shade = if currentTheme.shade == "": "" else: fmt"-{currentTheme.shade}"
    style = if currentTheme.style in ["Default", ""]: "" else: fmt"-{currentTheme.style}"
    color = if accentColor == "Blue": "" else: fmt"-{accentColor}"
    themeString = fmt"Colloid{color}{shade}{style}"

  result = gui:
    Button:
      icon = fmt"{accentColor}{style}{shade}"
      tooltip = accentColor
      style = if accentColor == currentTheme.color:
          [StyleClass("accent-button"), ButtonSuggested]
        else:
          [StyleClass("accent-button"), ButtonFlat]

      proc clicked() =
        setTheme(themeString)
        # Highlight this button
        currentTheme.color = accentColor
        if currentTheme.style == "":
          currentTheme.style = "Default"

proc themeButtons(): Widget =
  gui:
    # Vertical box
    Box(orient = OrientY, spacing = 5):
      for shadeName in themeShades:
        # Horizontal box
        Box(orient = OrientX, spacing = 5):
          for styleName in themeStyles:
            let themeButton = makeThemeButton(shadeName, styleName)
            insert(themeButton) {.vAlign: AlignCenter, hAlign: AlignCenter.}

proc accentButtons(): Widget =
  gui:
    # Horizontal box
    Box(orient = OrientX, spacing = 3):
      for accentColor in accentColors:
        let accentButton = makeAccentButton(accentColor)
        insert(accentButton) {.vAlign: AlignCenter, hAlign: AlignCenter.}

proc mainGUI(): Widget =
  gui:
    Window:
      title = "TROMjaro Theme Switcher"
      # Shrink window to the smallest size
      defaultSize = (1, 1)
      iconName = appLogo
      # Vertical box
      Box(orient = OrientY, margin = 13, spacing = 10):
        Label:
          useMarkup=true
          text="<span size='large'>STYLE</span>"
        # Theme buttons
        insert(themeButtons())
        Separator(margin = Margin(top: 5)) {.vAlign: AlignCenter.}
        Label:
          useMarkup=true
          text="<span size='large'>ACCENT COLOR</span>"
        # Accent buttons
        insert(accentButtons())
        Separator(margin = Margin(top: 12)) {.vAlign: AlignCenter.}
        # Vertical box
        Box(orient = OrientY):
          Label:
            text="A Theme Switcher for TROMjaro's default theme-set (Colloid) and icon-set (Zafiro)."
          Label {.vAlign: AlignStart.}:
            useMarkup = true
            text="<span size='small'>NOTE: Some apps need to be reopened for the theme to be applied.</span>"