local _,ns = ...
local L = ns.L


local optsPrice = CreateFrame("Frame", "AutoScrapPriceOptionsFrame", UIParent, "SettingsFrameTemplate")
optsPrice:SetSize(480, 250)
optsPrice:SetPoint("CENTER")
optsPrice:SetFrameStrata("DIALOG")
optsPrice:SetMovable(true)
optsPrice:EnableMouse(true)
optsPrice:RegisterForDrag("LeftButton")
optsPrice:SetScript("OnDragStart", optsPrice.StartMoving)
optsPrice:SetScript("OnDragStop", optsPrice.StopMovingOrSizing)
optsPrice:SetClampedToScreen(true)

optsPrice.NineSlice.Text:SetText(L["WoW Token Price Alert"])

local priceAlertCheck = CreateFrame("CheckButton", nil, optsPrice, "ChatConfigCheckButtonTemplate")
priceAlertCheck:SetPoint("TOPLEFT", 16, - 40)
priceAlertCheck.Text:SetText(L["Show a message when WoW token price changes"])
priceAlertCheck:SetChecked(false)

local goldTriggerLabel = optsPrice:CreateFontString(nil, "OVERLAY", "GameFontNormal")
goldTriggerLabel:SetPoint("TOPLEFT", priceAlertCheck, "BOTTOMLEFT", 0, - 16)
goldTriggerLabel:SetText(L["Alert when price changes by (gold)"])

local goldTriggerInput = CreateFrame("EditBox", nil, optsPrice, "InputBoxTemplate")
goldTriggerInput:SetSize(80, 20)
goldTriggerInput:SetPoint("TOPLEFT", goldTriggerLabel, "BOTTOMLEFT", 20, - 6)
goldTriggerInput:SetAutoFocus(false)
goldTriggerInput:SetNumeric(true)
goldTriggerInput:SetText( 0)

goldTriggerInput:SetScript("OnEditFocusLost", function(self)
    local val = tonumber(self:GetText()) or 10000
    if val < 0 then val = 10000 end
    self:SetText(val)
end)

local showInInstancesCheck = CreateFrame("CheckButton", nil, optsPrice, "ChatConfigCheckButtonTemplate")
showInInstancesCheck:SetPoint("TOPLEFT", priceAlertCheck, "BOTTOMLEFT", 0, - 66)
showInInstancesCheck.Text:SetText(L["Show alerts in raids and battlegrounds"])
showInInstancesCheck:SetChecked(false)

local showTokenCombinedBags = CreateFrame("CheckButton", nil, optsPrice, "ChatConfigCheckButtonTemplate")
showTokenCombinedBags:SetPoint("TOPLEFT", showInInstancesCheck, "BOTTOMLEFT", 0, -6)
showTokenCombinedBags.Text:SetText(L["Show Wow Token price in Combined Bags"])
showTokenCombinedBags:SetChecked(false)

local textUpdateRate = optsPrice:CreateFontString(nil, "OVERLAY", "GameFontNormal")
--textUpdateRate:SetFont(STANDARD_TEXT_FONT, 14, "")
textUpdateRate:SetPoint("BOTTOMLEFT", optsPrice, 15, 6)
textUpdateRate:SetText(L["*Refresh rate 60 seconds"])
textUpdateRate:SetTextColor(120 / 255, 120 / 255, 120 / 255, 1)

local updateUI
local saveBtn = CreateFrame("Button", nil, optsPrice, "UIPanelButtonTemplate")
saveBtn:SetSize(90, 22)
saveBtn:SetPoint("BOTTOMRIGHT", - 12, 12)
saveBtn:SetText(L["Save"])
saveBtn:SetScript("OnClick", function()
    AutoScrap_GlobalSettings.WowTokenPriceWindowEnabled = priceAlertCheck:GetChecked()
    AutoScrap_GlobalSettings.WowTokenPriceThreshold = tonumber(goldTriggerInput:GetText())
    AutoScrap_GlobalSettings.WowTokenListenerGroupInstance = showInInstancesCheck:GetChecked()
    AutoScrap_GlobalSettings.WowTokenInCombinedBags = showTokenCombinedBags:GetChecked()
    updateUI()
    optsPrice:Hide()
end)

updateUI = function()
    priceAlertCheck:SetChecked(AutoScrap_GlobalSettings.WowTokenPriceWindowEnabled)
    goldTriggerInput:SetText(AutoScrap_GlobalSettings.WowTokenPriceThreshold)
    showInInstancesCheck:SetChecked(AutoScrap_GlobalSettings.WowTokenListenerGroupInstance)
    showTokenCombinedBags:SetChecked(AutoScrap_GlobalSettings.WowTokenInCombinedBags)
    if not AutoScrap_GlobalSettings.WowTokenInCombinedBags then
        ns.hideCombinedBagsWowTokenText()
    end
end
ns.wowTokenConfigUpdateUI = updateUI

function ns.toggleWowTokenConfig()
    if optsPrice:IsShown() then
        optsPrice:Hide()
    else
        updateUI()
        optsPrice:Show()
    end
end

optsPrice:Hide()