wheelDestinyWindow = nil

function init()
  local ui = g_ui.loadUI("game_wheel_destiny")
  if not ui then
    print("[Wheel of Destiny] Failed to load UI file.")
    return
  end

  wheelDestinyWindow = ui:recursiveGetChildById("Wheel_Destiny_GunzodusOT")
  if not wheelDestinyWindow then
    print("[Wheel of Destiny] Failed to find Wheel of Destiny Window in UI.")
    return
  end

  modules.game_interface.getRootPanel():addChild(wheelDestinyWindow)
  wheelDestinyWindow:show()
  wheelDestinyWindow:breakAnchors()
  wheelDestinyWindow:move(
    (g_window.getWidth() - wheelDestinyWindow:getWidth()) / 2,
    (g_window.getHeight() - wheelDestinyWindow:getHeight()) / 2
  )

  -- Top bar icons
  local button1     = wheelDestinyWindow:recursiveGetChildById("button1")
  local button2     = wheelDestinyWindow:recursiveGetChildById("button2")
  local button3     = wheelDestinyWindow:recursiveGetChildById("button3")
  local information = wheelDestinyWindow:recursiveGetChildById("information")
  local presets     = wheelDestinyWindow:recursiveGetChildById("presets")

  button1.image:setImageSource("/images/game/wheel_destiny/wheel_destiny")
  button1.image:setHeight(26); button1.image:setWidth(26)
  button2.image:setImageSource("/images/game/wheel_destiny/gem_atelier")
  button3.image:setImageSource("/images/game/wheel_destiny/fragment_workshop")
  information.image:setImageSource("/images/game/wheel_destiny/information")
  information.image:setHeight(28); information.image:setWidth(26)
  presets.image:setImageSource("/images/game/wheel_destiny/presets")
  presets.image:setHeight(29); presets.image:setWidth(29); presets.image:setMarginLeft(-5)

  -- Top buttons state visuals
  local topButtons = { button1, button2, button3 }
  local function press(btn, isPressed)
    if not btn then return end
    local BG_ACTIVE, BG_INACTIVE = '#5e5d5dff', '#9c9999ff'
    local bcol = isPressed and BG_ACTIVE or BG_INACTIVE
    if btn.setImageColor      then btn:setImageColor(bcol) end
    if btn.setBackgroundColor then btn:setBackgroundColor(bcol) end
    if btn.setBorderWidth     then btn:setBorderWidth(isPressed and 3 or 1) end
    if btn.setBorderColor     then btn:setBorderColor(isPressed and '#941b1bff' or '#9c9999ff') end
    if btn.setBorderTopColor    then btn:setBorderTopColor(   isPressed and '#000000' or '#2a2a2a') end
    if btn.setBorderLeftColor   then btn:setBorderLeftColor(  isPressed and '#000000' or '#2a2a2a') end
    if btn.setBorderRightColor  then btn:setBorderRightColor( isPressed and '#000000' or '#2a2a2a') end
    if btn.setBorderBottomColor then btn:setBorderBottomColor(isPressed and '#000000' or '#2a2a2a') end
    if btn.setColor            then btn:setColor(isPressed and '#ffffff' or '#dddddd') end
    if btn.setOpacity          then btn:setOpacity(1.0) end
  end
  local function setActive(target)
    for _, b in ipairs(topButtons) do press(b, b == target) end
  end
  local function onTopButtonClick(which)
    setActive(which)
    if which == button1 then
      print("Wheel of Destiny - Action Button Clicked.")
    elseif which == button2 then
      print("Gem Atelier - Action Button Clicked.")
    elseif which == button3 then
      print("Fragment Workshop - Action Button Clicked.")
    end
  end
  button1.onClick = function() onTopButtonClick(button1) end
  button2.onClick = function() onTopButtonClick(button2) end
  button3.onClick = function() onTopButtonClick(button3) end
  setActive(button1)


  local CloseWheelWindow = wheelDestinyWindow:recursiveGetChildById('close_button')
  if CloseWheelWindow then
     CloseWheelWindow.onClick = function()
        print("[Wheel Of Destiny] Close button clicked.")
        terminate()
     end
  end

  -- === Wheel parts ===
  local q1     = wheelDestinyWindow:recursiveGetChildById('center_tr')
  local ring1  = wheelDestinyWindow:recursiveGetChildById('circle_white_tr')

  local q2     = wheelDestinyWindow:recursiveGetChildById('red_2nd_quarter')
  local ring2  = wheelDestinyWindow:recursiveGetChildById('circle_white_2nd_quarter')

  local q3     = wheelDestinyWindow:recursiveGetChildById('red_2nd_quarter_half')
  local ring3  = wheelDestinyWindow:recursiveGetChildById('circle_white_2nd_quarter_half')

  local ring1Hit = wheelDestinyWindow:recursiveGetChildById('circle_white_tr_hit')
  local ring2Hit = wheelDestinyWindow:recursiveGetChildById('circle_white_2nd_quarter_hit')
  local ring3Hit = wheelDestinyWindow:recursiveGetChildById('circle_white_2nd_quarter_half_hit')

  local RED, WHITE = '#f50909ff', '#ffffff'

  -- Generic setters
  local function setQuarter(widget, on)
    if not widget then return end
    widget.__active = on
    if widget.setImageColor then
      widget:setImageColor(on and RED or WHITE)
    elseif widget.setBackgroundColor then
      widget:setBackgroundColor(on and RED or WHITE)
    end
  end

  local function setRing(widget, on)
    if not widget then return end
    widget.__active = on
    if widget.setImageColor then widget:setImageColor(WHITE) end
    if widget.setVisible then
      widget:setVisible(on)
    elseif widget.setOpacity then
      widget:setOpacity(on and 1.0 or 0.0)
    elseif widget.show and widget.hide then
      if on then widget:show() else widget:hide() end
    end
    if on and widget.raise then widget:raise() end
  end

  -- Award tracking per quarter
  local printed1, printed2, printed3 = false, false, false
  local function checkFull(quarter, ring, which)
    local full = (quarter and quarter.__active) and (ring and ring.__active)
    if which == 1 then
      if full and not printed1 then
        print("First Red quarter - 50 wheel points added.")
        printed1 = true
      elseif not full then
        printed1 = false
      end
    elseif which == 2 then
      if full and not printed2 then
        print("Second Red quarter - 50 wheel points added.")
        printed2 = true
      elseif not full then
        printed2 = false
      end
    else
      if full and not printed3 then
        print("Third Red quarter - 50 wheel points added.")
        printed3 = true
      elseif not full then
        printed3 = false
      end
    end
  end

  -- Exclusivity helper (used to keep exactly one ring selected)
  local function setRingExclusive(targetRing)
    if not targetRing then return end
    if ring1 then setRing(ring1, targetRing == ring1) end
    if ring2 then setRing(ring2, targetRing == ring2) end
    if ring3 then setRing(ring3, targetRing == ring3) end
    checkFull(q1, ring1, 1)
    checkFull(q2, ring2, 2)
    checkFull(q3, ring3, 3)
  end

  -- Initial states
  if q1 then q1.__active = false end
  if ring1 then ring1.__active = false end
  if q2 then q2.__active = false end
  if ring2 then ring2.__active = false end
  if q3 then q3.__active = false end
  if ring3 then ring3.__active = false end

  setQuarter(q1,  false); setRing(ring1,  false)
  setQuarter(q2,  false); setRing(ring2,  false)
  setQuarter(q3,  false); setRing(ring3,  false)

  if ring1 and ring1.raise then ring1:raise() end
  if ring2 and ring2.raise then ring2:raise() end
  if ring3 and ring3.raise then ring3:raise() end

  -- =======================
  -- HANDLERS (always move selection on ANY click)
  -- =======================

  -- FIRST SET
  if q1 then
    q1.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        -- Toggle red, ALWAYS move selection to this ring
        local new = not widget.__active
        setQuarter(widget, new)
        setRingExclusive(ring1)
        return true
      elseif left then
        -- Just move selection
        setRingExclusive(ring1)
        return true
      end
      return false
    end
  end

  if ring1 then
    ring1.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        -- Toggle red for its quarter, ALWAYS keep/move selection here
        local new = not (q1 and q1.__active)
        setQuarter(q1, new)
        setRingExclusive(widget)
        return true
      elseif left then
        setRingExclusive(widget)
        return true
      end
      return false
    end
  end

  -- SECOND SET
  if q2 then
    q2.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        local new = not widget.__active
        setQuarter(widget, new)
        setRingExclusive(ring2)
        return true
      elseif left then
        setRingExclusive(ring2)
        return true
      end
      return false
    end
  end

  if ring2 then
    ring2.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        local new = not (q2 and q2.__active)
        setQuarter(q2, new)
        setRingExclusive(widget)
        return true
      elseif left then
        setRingExclusive(widget)
        return true
      end
      return false
    end
  end

  -- THIRD (HALF) SET
  if q3 then
    q3.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        local new = not widget.__active
        setQuarter(widget, new)
        setRingExclusive(ring3)
        return true
      elseif left then
        setRingExclusive(ring3)
        return true
      end
      return false
    end
  end

  if ring3 then
    ring3.onMouseRelease = function(widget, pos, button)
      local right = (button == MouseRightButton) or (button == 2) or (button == 'RightButton')
      local left  = (button == MouseLeftButton)  or (button == 1) or (button == 'LeftButton')
      if right then
        local new = not (q3 and q3.__active)
        setQuarter(q3, new)
        setRingExclusive(widget)
        return true
      elseif left then
        setRingExclusive(widget)
        return true
      end
      return false
    end
  end
  
end

function terminate()
  if wheelDestinyWindow then
    wheelDestinyWindow:destroy()
    wheelDestinyWindow = nil
  end
end

function hide()
  if wheelDestinyWindow then
    wheelDestinyWindow:hide()
  end
end
