local PT = AhUI
PT.config = PT.config or {}
if not PT.IS_MID_AVAILABLE then
    PT.config.concentrationMode = "TWW"
end
PT.ConcentrationUI = {}
local UI = PT.ConcentrationUI
local L = PT.L
local Tooltip = PT.Tooltip

PT.config.showConcentration = PT.config.showConcentration ~= false
PT.config.concentrationMode = PT.config.concentrationMode or "DF"
if PT.config.concentrationMode == "MID" and not PT.IS_MID_AVAILABLE then
    PT.config.concentrationMode = "DF"
end

local FONT = "Interface\\AddOns\\AhUI\\fonts\\GothamXNarrow-Medium.ttf"
local ICON_DF = "Interface\\AddOns\\AhUI\\media\\DF.tga"
local ICON_TWW = "Interface\\AddOns\\AhUI\\media\\TWW.tga"
local ICON_MID = "Interface\\AddOns\\AhUI\\media\\MID.tga"

local MAX_CONCENTRATION = 1000
local TIME_TO_MAX = 6000
local FILL_RATE = MAX_CONCENTRATION / TIME_TO_MAX

local NUM_SEGMENTS = 5
local SEGMENT_VALUE = MAX_CONCENTRATION / NUM_SEGMENTS
local DFConcentrationCurrencyId = {
    [755] = 3047, -- ювелирное дело
    [197] = 3048, -- портняжное дело
    [165] = 3049, -- кожевничество
    [164] = 3050, -- кузнечное дело
    [333] = 3051, -- наложение чар
    [202] = 3052, -- инженерное дело
    [773] = 3053, -- начертание
    [171] = 3054, -- алхимия
}
local TWWConcentrationCurrencyId = {
	[755] = 3013, --ювелирное
    [164] = 3040, --кузнечное дело
	[197] = 3041, --портняжное дело
	[165] = 3042, --кожевничество
	[773] = 3043, --начертание
	[202] = 3044, --инженерное дело
    [171] = 3045, --алхимия
    [333] = 3046, --наложение чар
}
local MIDConcentrationCurrencyId = {
    [171] = 3161, -- алхимия
    [164] = 3162, -- кузнечное
    [333] = 3163, -- наложение чар
    [202] = 3164, -- инженерное
    [773] = 3165, -- начертание
    [755] = 3166, -- ювелирное
    [165] = 3167, -- кожевничество
    [197] = 3168, -- портняжное
}
local function FormatTime(minutes)
    if minutes < 60 then
        return string.format(L["TIME_MIN"], minutes)
    elseif minutes < 1440 then
        return string.format(
            L["TIME_HOUR"],
            math.floor(minutes / 60),
            minutes % 60
        )
    else
        return string.format(
            L["TIME_DAY"],
            math.floor(minutes / 1440),
            math.floor((minutes % 1440) / 60)
        )
    end
end

local lastOpenedSkillLine = nil

local function OpenProfessions(profData)
    if not profData or not profData.skillLine then
        return
    end

    if ProfessionsFrame
       and ProfessionsFrame:IsShown()
       and lastOpenedSkillLine == profData.skillLine
    then
        HideUIPanel(ProfessionsFrame)
        lastOpenedSkillLine = nil
        return
    end

    if ProfessionsFrame then
        ShowUIPanel(ProfessionsFrame)
    end

    if C_TradeSkillUI and C_TradeSkillUI.OpenTradeSkill then
        C_TradeSkillUI.OpenTradeSkill(profData.skillLine)
        lastOpenedSkillLine = profData.skillLine
    end
end

local function SavePosition(frame)
    AhUI_DB = AhUI_DB or {}
    AhUI_DB.positions = AhUI_DB.positions or {}

    local point, _, relPoint, x, y = frame:GetPoint()
    AhUI_DB.positions.concentration = {
        point = point,
        relPoint = relPoint,
        x = x,
        y = y,
    }
end

local function LoadPosition(frame)
    frame:ClearAllPoints()

    if AhUI_DB
       and AhUI_DB.positions
       and AhUI_DB.positions.concentration
    then
        local p = AhUI_DB.positions.concentration
        frame:SetPoint(p.point, UIParent, p.relPoint, p.x, p.y)
    else
        frame:SetPoint("CENTER", UIParent, "CENTER", 0, -200)
    end
end

local function CreateIconBorder(icon)
    local parent = icon:GetParent()

    local holder = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    holder:SetFrameLevel(parent:GetFrameLevel() + 10)

    holder:SetPoint("TOPLEFT", icon, "TOPLEFT", -2, 2)
    holder:SetPoint("BOTTOMRIGHT", icon, "BOTTOMRIGHT", 2, -2)

    holder:SetBackdrop({
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 2,
    })

    holder:SetBackdropBorderColor(0, 0, 0, 1)
end

local function CreateBarBorder(bar)
    local f = CreateFrame("Frame", nil, bar, "BackdropTemplate")
    f:SetFrameLevel(bar:GetFrameLevel() + 15)

    f:SetPoint("TOPLEFT", bar, "TOPLEFT", -2, 2)
    f:SetPoint("BOTTOMRIGHT", bar, "BOTTOMRIGHT", 2, -2)

    f:SetBackdrop({
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 2,
    })

    f:SetBackdropBorderColor(0, 0, 0, 1)
	bar.border = f
end

local function CreateModeToggleButton(parent)
    local b = CreateFrame("Button", nil, parent)
    b:SetSize(50, 32)
    b:SetFrameLevel(parent:GetFrameLevel() + 20)

    b.icon = b:CreateTexture(nil, "ARTWORK")
    b.icon:SetAllPoints()
    b.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)

    local mask = b:CreateMaskTexture()
    mask:SetTexture(
        "Interface\\CharacterFrame\\TempPortraitAlphaMask",
        "CLAMPTOBLACKADDITIVE",
        "CLAMPTOBLACKADDITIVE"
    )
    mask:SetAllPoints(b.icon)
    b.icon:AddMaskTexture(mask)
function b:UpdateTooltip()
    Tooltip:Clear()

    Tooltip:AddLine(
        L["CONCENTRATION_MODE"] or "Concentration Mode",
        14, 1, 1, 1
    )

    Tooltip:AddLine(" ")

if PT.config.concentrationMode == "DF" then
    Tooltip:AddLine("Dragonflight (S4)", 12, 0.4, 0.8, 1)
elseif PT.config.concentrationMode == "MID" then
    Tooltip:AddLine("Midnight", 12, 0.7, 0.6, 1)
else
    Tooltip:AddLine("The War Within", 12, 1, 0.6, 0.2)
end
    Tooltip:ShowAtCursor()
end

function b:Update()
    if PT.config.concentrationMode == "DF" then
        b.icon:SetTexture(ICON_DF)
    elseif PT.config.concentrationMode == "MID" then
        b.icon:SetTexture(ICON_MID)
    else
        b.icon:SetTexture(ICON_TWW)
    end
end

b:SetScript("OnClick", function()
    UI:ToggleConcentrationMode()
    b:Update()

    if b:IsMouseOver() then
        b:UpdateTooltip()
    end
end)

b:SetScript("OnEnter", function()
	b:UpdateTooltip()
end)

b:SetScript("OnLeave", function()
    Tooltip:Clear()
end)

    b:Update()
    return b
end

local function CreateBar(parent, index)
    local bar = CreateFrame("StatusBar", nil, parent)
    bar:SetSize(230, 30)
    bar:SetFrameLevel(parent:GetFrameLevel() + 1)

    bar.bg = bar:CreateTexture(nil, "BACKGROUND")
    bar.bg:SetAllPoints()
    bar.bg:SetColorTexture(0, 0, 0, 0.7)

bar:SetMinMaxValues(0, MAX_CONCENTRATION)
bar:SetValue(0)

bar:SetStatusBarTexture("Interface\\Buttons\\WHITE8x8")
bar:GetStatusBarTexture():SetVertexColor(64/255, 186/255, 255/255, 1)

bar.iconHolder = CreateFrame("Frame", nil, bar, "BackdropTemplate")
bar.iconHolder:SetSize(30, 30)
bar.iconHolder:SetPoint("LEFT", bar, "LEFT", -32, 0)
bar.iconHolder:SetFrameLevel(bar:GetFrameLevel() + 5)

bar.icon = bar.iconHolder:CreateTexture(nil, "ARTWORK", nil, 5)
bar.icon:SetPoint("CENTER")
bar.icon:SetSize(32, 32)
bar.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)

CreateIconBorder(bar.iconHolder)

    bar.iconHolder:SetScript("OnEnter", function(self)
        if self.profData then
            Tooltip:Clear()

            Tooltip:AddLine(self.profData.name, 14, 1, 1, 1)

            Tooltip:AddLine(" ")

            Tooltip:AddLine(L["CLICK_TO_TOGGLE_PROFESSIONS"], 12, 1, 0.8, 0)

            Tooltip:ShowAtCursor()
        end
    end)

    bar.iconHolder:SetScript("OnLeave", function()
        Tooltip:Clear()
    end)

bar.iconHolder:SetScript("OnMouseDown", function(self, button)
    if button == "LeftButton" then
        bar.icon:SetPoint("CENTER", 1, -1)

        if self.profData then
            OpenProfessions(self.profData)
        end
    end
end)

    bar.iconHolder:SetScript("OnMouseUp", function(self, button)
        if button == "LeftButton" then
            bar.icon:SetPoint("CENTER", 0, 0)
        end
    end)
	
    bar.iconText = bar.iconHolder:CreateFontString(nil, "OVERLAY", nil, 7)
    bar.iconText:SetFont(FONT, 13, "OUTLINE")
    bar.iconText:SetTextColor(1, 1, 1)
    bar.iconText:SetPoint("CENTER")

    bar.timers = {}
    local segmentWidth = bar:GetWidth() / NUM_SEGMENTS

    for s = 1, NUM_SEGMENTS do
        local fs = bar:CreateFontString(nil, "OVERLAY", nil, 8)
        fs:SetFont(FONT, 11)
        fs:SetTextColor(1, 1, 1)
		fs:SetShadowOffset(1, -1)
        fs:SetPoint("CENTER", bar, "LEFT", (s - 0.5) * segmentWidth, 0)
        bar.timers[s] = fs
    end
	
bar.dividers = {}

local barWidth = bar:GetWidth()

for s = 1, NUM_SEGMENTS - 1 do
    local fs = bar:CreateFontString(nil, "OVERLAY")
    fs:SetFont(FONT, 35)  
    fs:SetText("|") 
    fs:SetTextColor(1,1,1,1)

    local x = (barWidth / NUM_SEGMENTS) * s
    fs:SetPoint("CENTER", bar, "LEFT", x, -1)

    bar.dividers[s] = fs
end

    CreateBarBorder(bar)

    if index == 1 then
        bar:SetPoint("TOP", parent, "TOP", 0, -10)
    else
        bar:SetPoint("TOP", parent.bars[1], "BOTTOM", 0, -1)
    end

    return bar
end

local function GetPlayerConcentrationProfessions()
    local list = {}
    local p1, p2 = GetProfessions()

local map = DFConcentrationCurrencyId

if PT.config.concentrationMode == "TWW" then
    map = TWWConcentrationCurrencyId
elseif PT.config.concentrationMode == "MID" and PT.IS_MID_AVAILABLE then
    map = MIDConcentrationCurrencyId
end

    for _, slot in ipairs({ p1, p2 }) do
        if slot then
            local name, icon, _, _, _, _, skillLine = GetProfessionInfo(slot)
            local cid = map[skillLine]
            
                table.insert(list, {
                    name       = name,
                    icon       = icon,
                    skillLine  = skillLine,
                    currencyId = cid,
                })       
        end
    end

    return list
end


local function UpdateBars(self)
	if not C_CurrencyInfo
		or not C_CurrencyInfo.GetCurrencyInfo
		or not GetProfessions
	then
		return
	end

    if not PT.config.showConcentration then
        self:Hide()
        return
    end

    local profs = GetPlayerConcentrationProfessions()
	if not profs then return end
    if self.modeToggle then
        self.modeToggle:SetShown(#profs > 0)
    end

if #profs == 0 then
    for i = 1, 2 do
        self.bars[i]:Hide()
    end
    return
end

self:Show()

    for i = 1, 2 do
        local bar = self.bars[i]
        local prof = profs[i]

        if not prof then
            bar:Hide()
	else
		bar.icon:SetTexture(prof.icon)
		bar.iconHolder.profData = prof

		if not prof.currencyId then
			bar.iconText:SetText("")
			bar:SetValue(0)
			
			bar.bg:Hide()

			for s = 1, NUM_SEGMENTS do
				bar.timers[s]:SetText("")
				bar.timers[s]:Hide()
			end

			if bar.dividers then
				for _, d in ipairs(bar.dividers) do
					d:Hide()
				end
			end
			if bar.border then
				bar.border:Hide()
			end
			
			bar:Show()
		else

			local info = C_CurrencyInfo.GetCurrencyInfo(prof.currencyId)
			local value = info.quantity or 0

			bar.iconText:SetText(value)
			bar:SetValue(value)

			bar.bg:Show()
			if bar.border then
				bar.border:Show()
			end

			for s = 1, NUM_SEGMENTS do
				local required = s * SEGMENT_VALUE
				if value >= required then
					bar.timers[s]:SetText("")
				else
					local minutes = math.floor((required - value) / FILL_RATE)
					bar.timers[s]:SetText(FormatTime(minutes))
				end
				bar.timers[s]:Show()
			end
	
			if bar.dividers then
				for _, d in ipairs(bar.dividers) do
					d:Show()
				end
			end
	
			bar:Show()
		end
	end
end
end
function UI:CreateFrame()
    if self.frame then return self.frame end

    local f = CreateFrame("Frame", "AhUIConcentrationFrame", UIParent)
    f:SetSize(300, 100)

    LoadPosition(f)

    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", function(s)
        s:StopMovingOrSizing()
        SavePosition(s)
    end)

    f.bars = {}
    f.bars[1] = CreateBar(f, 1)
    f.bars[2] = CreateBar(f, 2)

	f.modeToggle = CreateModeToggleButton(f)
	f.modeToggle:SetPoint(
		"RIGHT",
		f.bars[1].iconHolder,
		"LEFT",
		2,
		-15
	)

    f:SetScript("OnUpdate", function(self, dt)
        self.t = (self.t or 0) + dt
        if self.t > 1 then
            UpdateBars(self)
            self.t = 0
        end
    end)
	
	AhUI_CharConfig.concentrationAlpha = AhUI_CharConfig.concentrationAlpha or 100
    f:SetAlpha(AhUI_CharConfig.concentrationAlpha / 100)

    self.frame = f
	
	PT:RegisterHideInCombat(f, "hideConcentrationInCombat")
    return f
end

function UI:Show()
    if PT.config.showConcentration then
        local frame = self:CreateFrame()
        AhUI_CharConfig.concentrationAlpha = AhUI_CharConfig.concentrationAlpha or 100
        frame:SetAlpha(AhUI_CharConfig.concentrationAlpha / 100)
        frame:Show()
    end
end
function UI:Hide()
    if self.frame then self.frame:Hide() end
end

function UI:Toggle()
    if PT.config.showConcentration then
        self:Show()
    else
        self:Hide()
    end
end

function UI:SetConcentrationMode(mode)
    if mode == "MID" and not PT.IS_MID_AVAILABLE then
    return
end

if mode ~= "DF" and mode ~= "TWW" and mode ~= "MID" then
    return
end

    PT.config.concentrationMode = mode

if self.frame then
    UpdateBars(self.frame)
    if self.frame.modeToggle then
        self.frame.modeToggle:Update()
    end
end

end

function UI:ToggleConcentrationMode()
    local order

    if PT.IS_MID_AVAILABLE then
        order = { "DF", "TWW", "MID" }
    else
        order = { "DF", "TWW" }
    end

    local current = PT.config.concentrationMode

    for i, v in ipairs(order) do
        if v == current then
            local nextMode = order[i + 1] or order[1]
            self:SetConcentrationMode(nextMode)
            return
        end
    end

    self:SetConcentrationMode(order[1])
end

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:SetScript("OnEvent", function()
    if UI.frame then
        UpdateBars(UI.frame)
        UI.frame:Show()
    end
end)