local addonName = ...

AhUI = AhUI or {}
local PT = AhUI

PT.professionsReady = false

do
    local _, _, _, toc = GetBuildInfo()
    PT.IS_MID_AVAILABLE = toc and toc >= 120000
end

local function KillLegacyAhUIState()
    if not PT then return end
    if PT.Bar then
        local bar = PT.Bar

        if bar.frame then
            bar.frame:Hide()
            bar.frame:SetParent(nil)
        end

        if bar.panels then
            for _, p in pairs(bar.panels) do
                if type(p) == "table" and p.frame then
                    p.frame:Hide()
                    p.frame:SetParent(nil)
                elseif type(p) == "table" and p.Hide then
                    p:Hide()
                    p:SetParent(nil)
                end
            end
        end

        PT.Bar = nil
    end
    PT.RegisteredDataTexts = nil
    PT.tooltipFunc = nil

    local f = _G["AhUI_BarFrame"]
    if f then f:Hide(); f:SetParent(nil); _G["AhUI_BarFrame"] = nil end

    for i = 1, 50 do
        local pn = _G["AhUI_Panel_" .. i]
        if pn then
            pn:Hide()
            pn:SetParent(nil)
            _G["AhUI_Panel_" .. i] = nil
        end
    end
end

KillLegacyAhUIState()

PT.DB = PT.DB or {}
PT.modules = PT.modules or {}

AhUI_DB = AhUI_DB or {}
PT.config = AhUI_DB

AhUI_CharConfig = AhUI_CharConfig or {}

if AhUI_CharConfig.autoLootInitialized == nil then
    AhUI_CharConfig.autoLootInitialized = false
end

AhUI_DB = AhUI_DB or {}
PT.config = AhUI_DB

AhUI_DB.minimap = AhUI_DB.minimap or {
    hide = false,
}

AhUI_CharConfig = AhUI_CharConfig or {}

if AhUI_CharConfig.showProfessionsWindow == nil then
    AhUI_CharConfig.showProfessionsWindow = true
end

PT.config.showProfessionsWindow = AhUI_CharConfig.showProfessionsWindow

if AhUI_CharConfig.skinningCheckEnabled == nil then
    AhUI_CharConfig.skinningCheckEnabled = true
end

AhUI_CharConfig.skinningIconSize = AhUI_CharConfig.skinningIconSize or 32

local defaultConfig = {
    showAllProfessions   = false,
    minimized            = false,
    hideCatchUp          = false,
    weekly               = false,
    hideMaxedProfession  = false,
    hideWhenDone         = false,
    hideTreatise         = false,
    showConcentration       = true,
    showProfessionsWindow   = true,  
    lockWindows             = false,
}


for k, v in pairs(defaultConfig) do
    if PT.config[k] == nil then
        PT.config[k] = v
    end
end

PT.collapsedProfessions = PT.collapsedProfessions or {}
PT.collapsedCategories  = PT.collapsedCategories  or {}

function PT:RefreshUI()
end

function PT:RefreshData()
    if PT.GetProfessions then
        PT.professions = PT.GetProfessions(true)
    end
end

local function InitAutoLootForNewChar()
    if AhUI_CharConfig.autoLootInitialized then
        return
    end

    if C_CVar and C_CVar.SetCVar then
        C_CVar.SetCVar("autoLootDefault", "1")
    else
        SetCVar("autoLootDefault", 1)
    end

    AhUI_CharConfig.autoLootInitialized = true
end

local f = CreateFrame("Frame")
PT.eventFrame = f

f:SetScript("OnEvent", function(self, event, ...)
    if event == "ADDON_LOADED" then
        local name = ...
        if name == addonName then

            AhUI_DB = AhUI_DB or {}
            PT.config = AhUI_DB

            for k, v in pairs(defaultConfig) do
                if PT.config[k] == nil then
                    PT.config[k] = v
                end
            end
			PT.LoadActiveDB()
			PT:RefreshData()
			PT:RefreshUI()

			if PT.Settings and PT.Settings.Register then
				PT.Settings:Register()
			end

        end

elseif event == "PLAYER_LOGIN" then
    InitAutoLootForNewChar()
	PT.professionsReady = false
    PT:RefreshUI()

    C_Timer.After(1, function()
        if PT.config.showConcentration and PT.ConcentrationUI then
            PT.ConcentrationUI:Show()
        end
    end)

    C_Timer.After(1, function()
        if AhUI_CharConfig.showProfessionsWindow and PT.UI and PT.UI.frame then
            PT.UI.frame:Show()
        elseif PT.UI and PT.UI.frame then
            PT.UI.frame:Hide()
        end
    end)

    if PT.config.lockWindows then
        if PT.UI and PT.UI.frame then
            PT.UI.frame:SetMovable(false)
            PT.UI.frame:EnableMouse(false)
        end
        if PT.ConcentrationUI and PT.ConcentrationUI.frame then
            PT.ConcentrationUI.frame:SetMovable(false)
            PT.ConcentrationUI.frame:EnableMouse(false)
        end
    end
    
elseif event == "TRADE_SKILL_DATA_SOURCE_CHANGED" then
	PT.professions = nil
    PT.professionsReady = true
    PT:RefreshData()
    PT:RefreshUI()

elseif event == "PLAYER_ENTERING_WORLD"
    or event == "QUEST_LOG_UPDATE"
    or event == "CURRENCY_DISPLAY_UPDATE"
    or event == "BAG_UPDATE"
    or event == "SKILL_LINES_CHANGED"
then
	PT.professions = nil
	PT:RefreshData()
	PT:RefreshUI()

    elseif event == "PLAYER_LOGOUT" then

        AhUI_DB = AhUI_DB or {}
        for k, v in pairs(PT.config) do
            AhUI_DB[k] = v
        end

        AhUI_CharConfig.showProfessionsWindow = AhUI_CharConfig.showProfessionsWindow
    end
end)

f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("QUEST_LOG_UPDATE")
f:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
f:RegisterEvent("BAG_UPDATE")
f:RegisterEvent("SKILL_LINES_CHANGED")
f:RegisterEvent("TRADE_SKILL_DATA_SOURCE_CHANGED")
f:RegisterEvent("PLAYER_LOGOUT")

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")

f:SetScript("OnEvent", function(self, event, addonName)
    if addonName ~= "AhUI" then return end

    PT.LoadActiveDB()
    PT:RefreshData()
    PT:RefreshUI()

    self:UnregisterEvent("ADDON_LOADED")
end)
