
function tCC_GetDefault()
	return {
		version = tonumber(GetAddOnMetadata("tdCoolDownCount", "Version")),
		Action = {
			config = true,  min = 3, font = "Fonts\\K_Damage.TTF", size = 28, shine = true,
			time = 1, scale = 200, style = 1, center = true,  cscale = 80, ctime = 1, calpha = 0.9, cmode = true, hide = false
		},
		Pet = {
			config = true,  min = 3, font = "Fonts\\K_Damage.TTF", size = 28, shine = true,
			time = 1, scale = 200, style = 3, center = true,  cscale = 80, ctime = 1, calpha = 0.9, cmode = true, hide = false
		},
		Buff = {
			config = true,  max = 60, font = "Fonts\\K_Damage.TTF", size = 18,
			point = 3, hide = true
		},
		Spell = {
			config = false, min = 3, font = "Fonts\\K_Damage.TTF", size = 28, shine = true,
			time = 1, scale = 200, style = 3, hide = false
		},
		Item = {
			config = true,  min = 3, font = "Fonts\\K_Damage.TTF", size = 28, shine = true,
			time = 1, scale = 200, style = 3, hide = false
		},
		Color = {
			Range =  {r = 0.5, g = 0.1, b = 0.1},
			Mana =   {r = 0.5, g = 0.5, b = 1.0},
			Usable = {r = 0.4, g = 0.4, b = 0.4},
		},
	}
end

tCC_Type = {
	action = "Action",
	spell = "Spell",
	buff = "Buff",
	item = "Item",
	pet = "Pet",
	unkown = "Unkown",
}

local tCC_Point = {
	"TOPLEFT",
	"TOP",
	"TOPRIGHT",
	"LEFT",
	"CENTER",
	"RIGHT",
	"BOTTOMLEFT",
	"BOTTOM",
	"BOTTOMRIGHT"
}

tCC_Style = {
	"",
	"Interface\\Cooldown\\star4",
	"Interface\\Cooldown\\ping4",
	"Interface\\Cooldown\\starburst",
	"Interface\\AddOns\\tdCoolDownCount\\heart",
}

function tCC_CenterOnUpdate()
	this.finish = this.finish + arg1
	local alpha = tCC[this.type].calpha
	if this.finish > tCC[this.type].ctime / 2 then
		alpha = (1 - this.finish / tCC[this.type].ctime) * 2 * alpha
	end
	this.shine:SetAlpha(alpha)

	if this.finish >= tCC[this.type].ctime then
		this.finish = 0
		this:Hide()
	end
end

function tCC_GetCoolDownStyle(time)
	if (time <= 0.2) then
		return "", 1, 1, 1, 1
	elseif (time < 9) then		--10s
		local t = time - math.floor(time)
		if t > 0.5 then t = 0.12 else t = 0.82 end
		return math.ceil(time), 1.3, 1.0,t,0.12
	elseif (time < 60) then		--1m
		return math.ceil(time), 1.0, 1.0,0.82,0.0
	elseif (time < 600) then	--10m
		if tCC.type then
			local s = math.floor(mod(time , 60))
			s = s >= 10 and s or "0"..s
			return math.floor(time / 60) .. ":" .. s, 0.8, 1.0,0.8,0.0
		else
			return ( math.ceil(time / 60).."m"), 0.85, 1.0, 0.8, 0.0
		end
	elseif (time < 3600) then	--1h
		return ( math.ceil(time / 60).."m"), 0.7, 0.8, 0.6, 0.0
	elseif (time < 86400) then
		return ( math.ceil(time / 3600).."h"), 0.6, 0.6, 0.4, 0.0
	else
		return ( math.ceil(time / 86400).."d"), 0.6, 0.4, 0.4, 0.4
	end
end

function tCC_OnUpdate()
	local button = this:GetParent()
	local cd = button.cd
	local type = cd.type

	-- 刷新闪光
	if tCC[type].shine and this.shine and this.shine:IsVisible() then
		this.finish = this.finish + arg1
		local alpha = 1 - this.finish / tCC[type].time
		local scale = tCC[type].scale * ( 1 - this.finish / tCC[type].time)
		this.shine:SetWidth(scale)
		this.shine:SetHeight(scale)
		this.shine:SetAlpha(alpha)

		local texture = this.shine:GetTexture()
		if tCC[type].style == 1 then
			local icon = getglobal(this:GetParent():GetName().."Icon"):GetTexture()
			if icon ~= texture then
				this.shine:SetTexture(icon)
			end
		elseif texture ~= tCC_Style[tCC[type].style] then
			this.shine:SetTexture(tCC_Style[tCC[type].style])
		end

		if this.finish >= tCC[type].time then
			this.finish = 0
			this.flashing = false
			this.shine:Hide()
			this:SetScale(1)
			if this.start == 0 then
				this:Hide()
			end
		end
	end

	local frame = button.frame
	local time = cd.start + cd.duration - GetTime()

	-- 系统动画
	local alpha = 1
	if tCC[type].hide then
		alpha = 0
	end
	if cd:GetAlpha() ~= alpha then
		cd:SetAlpha(alpha)
	end

	-- 验证开启
	if tCC[type].config then
		-- 验证上限
		if tCC[type].max and time > tCC[type].max and tCC[type].max ~= 0 then
			if frame.font:IsVisible() then
				frame.font:Hide()
			end
			return
		else
			if not frame.font:IsVisible() then
				frame.font:Show()
			end
		end
	else
		-- 恢复系统动画
		if cd:GetAlpha() ~= 1 then
			cd:SetAlpha(1)
		end
		frame:Hide()
		return
	end

	-- 验证冷却
	if not cd:IsVisible() or cd.duration <= (tCC[type].min or 0) then
		frame.font:SetText("")
		cd.start = 0
		cd.duration = 0
		-- 恢复系统动画
		if cd:GetAlpha() ~= 1 then
			cd:SetAlpha(1)
		end
		-- 是否闪光中
		if not frame.flashing then
			frame:Hide()
		end
		return
	end

	-- 验证位置
	local _,_,point = frame.font:GetPoint()
	if tCC_Point[tCC[type].point or 5] ~= point then
		frame.font:SetPoint("CENTER", button, tCC_Point[tCC[type].point or 5], 0, 0)
	end

	-- 减少刷新
	this.last = this.last - arg1
	if this.last <= 0 then
		this.last = 0.2
	else
		return
	end

	local text, scale, r, g, b = tCC_GetCoolDownStyle(time)

	-- 验证字体与大小
	local font, size = frame.font:GetFont()
	if font ~= tCC[type].font or size ~= tCC[type].size then
		frame.font:SetFont(tCC[type].font, tCC[type].size , "OUTLINE")
	end

	--验证缩放
	if not frame.scale or frame.scale ~= scale then
		frame:SetScale(scale)
		frame.scale = scale
	end

	-- 验证时间
	if not frame.time or frame.time ~= time then
		frame.font:SetText(text)
		frame.time = time
	end

	-- 验证颜色
	if not frame.g or frame.g ~= g then
		frame.font:SetTextColor(r, g, b)
		frame.g = g
	end

	-- 将结束
	if time < 0.2 then
		cd.start = 0
		cd.duration = 0
		if cd:GetAlpha() ~= 1 then
			cd:SetAlpha(1)
		end

		-- 创建闪光
		if tCC[type].shine then
			if not this.shine then
				frame.shine = frame:CreateTexture(nil, "OVERLAY")
				frame.shine:SetPoint("CENTER" , button, "CENTER", 0, 0)
				frame.shine:SetTexture(tCC_Style[tCC[cd.type].style])
				frame.shine:SetBlendMode("ADD")
				frame.shine:Hide()

				frame.finish = 0
			end
			frame.flash = 0
			frame.flashing = true
			frame.shine:Show()
		end

		-- 创建中部提示
		if tCC[type].center then
			if not tCC_Center.shine then
				tCC_Center.shine = tCC_Center:CreateTexture(nil, "OVERLAY")
				tCC_Center.finish = 0
			end

			tCC_Center:ClearAllPoints()
			if tCC[type].p then
				tCC_Center.shine:SetPoint(tCC[type].p,UIParent,tCC[type].s,tCC[type].x,tCC[type].y)
			else
				tCC_Center.shine:SetPoint("CENTER",UIParent,"CENTER",0,0)
			end
			if tCC_Center:GetWidth() ~= tCC[type].cscale then
				tCC_Center.shine:SetWidth(tCC[type].cscale)
				tCC_Center.shine:SetHeight(tCC[type].cscale)
			end

			local mode = "BLEND"
			if tCC[type].cmode then
				mode = "ADD"
			end
			if mode ~= tCC_Center.shine:GetBlendMode() then
				tCC_Center.shine:SetBlendMode(mode)
			end

			local texture = getglobal(button:GetName().."Icon"):GetTexture()
			tCC_Center.shine:SetTexture(texture)

			tCC_Center.type = type
			tCC_Center:Show()
		end
	end
end

function tCC_ButtonGetType(button)
	local name = button:GetName()
	if string.find(name, "^SpellButton") then
		return tCC_Type.spell
	elseif getglobal(name.."AutoCastable") then
		return tCC_Type.pet
	elseif getglobal(name.."HotKey") then
		return tCC_Type.action
	elseif getglobal(name.."Stock") then
		return tCC_Type.item
	else
		return tCC_Type.buff
	end
end

function tCC_CooldownFrame_SetTimer(cd, start, duration, enable)
	if ( start > 0 and duration > 0 and enable > 0) then
		cd:SetCooldown(start, duration);
		cd:Show();

		local button = cd:GetParent()
		local type = tCC_ButtonGetType(button)

		if duration > (tCC[type].min or 0) and tCC[type].config then
			cd.start = start
			cd.duration = duration
			cd.type = type

			-- 隐藏系统动画
			local alpha = 1
			if tCC[type].hide then
				alpha = 0
			end
			if cd:GetAlpha() ~= alpha then
				cd:SetAlpha(alpha)
			end

			-- 创建计时
			if not button.frame then
				local frame = CreateFrame("frame", nil, button)
				frame:SetAllPoints(button)
				frame:SetFrameStrata("HIGH")

				frame.font = frame:CreateFontString(nil, "ATKWORK")
				frame.font:SetPoint("CENTER", button, tCC_Point[tCC[type].point], 0, 0)

				frame.font:SetFont(tCC[type].font , tCC[type].size , "OUTLINE")
				frame.font:SetJustifyH("CENTER")
				frame.font:SetText("")

				frame.last = 0
				frame:SetScript("OnUpdate", tCC_OnUpdate)

				button.frame = frame
				button.cd = cd
			end
			button.frame:Show()
		end
	else
		cd:Hide();
	end
end

hooksecurefunc("CooldownFrame_SetTimer", tCC_CooldownFrame_SetTimer)

if ( GetLocale() == "zhCN" ) then
	tCClocal = {
		Sorry = "此插件恢复成为默认设置,我对此感到非常抱歉!",
		Loaded = "tCC - 冷却计时 已载入. 输入/tCC进行设置.",
		DisOption = "tdCoolDownOption 模块已删除或被禁用!",
	}
elseif ( GetLocale() == "zhTW" ) then
	tCClocal = {
		Sorry = "此插件恢復成為默認設置,我對此感到非常抱歉!",
		Loaded = "tCC - 冷卻計時 已載入. 輸入/tCC進行設置. ",
		DisOption = "tdCoolDownOption 模塊已刪除或被禁用!",
	}
elseif ( GetLocale() == "koKR" ) then
	tCClocal = {
		Sorry = "이 애드온을 기본 설정으로 되돌립니다!",
		Loaded = "tCC - 재사용 대기시간을 불려옵니다. 설정 명령어는 /tCC 입니다.",
		DisOption = "tdCoolDownOption 모듈을 삭제하거나 사용이 금지되었습니다!",
	}
else
	tCClocal = {
		Sorry = "This AddOn back to the default settings, I'm so sorry!",
		Loaded = "tCC - Cooldown Count loaded. Input /tCC set it up.",
		DisOption = "tdCoolDownOption Module has been deleted or Prohibited!",
	}
end

tCC_Center = CreateFrame("frame", nil, UIParent)
tCC_Center:Hide()
tCC_Center:SetAllPoints(UIParent)
tCC_Center:SetFrameStrata("HIGH")
tCC_Center:RegisterEvent("VARIABLES_LOADED")
tCC_Center:SetScript("OnUpdate", tCC_CenterOnUpdate)
tCC_Center:SetScript("OnEvent", function()
	DEFAULT_CHAT_FRAME:AddMessage("|cff7fff7f"..tCClocal.Loaded.."|r")
	if tCC and tCC.version and tonumber(tCC.version) and tonumber(tCC.version) >= 3.36 then
		tCC.version = tonumber(GetAddOnMetadata("tdCoolDownCount", "Version"))
		return
	end
	DEFAULT_CHAT_FRAME:AddMessage("|cff7fff7ftCC: "..tCClocal.Sorry.."|r")
	tCC = tCC_GetDefault()
end)

SLASH_TCCSLASH1 = "/tCC"
SlashCmdList["TCCSLASH"] = function()
	if IsAddOnLoaded("tdCoolDownOption") or tCC_Option_Init then
		if tCC_Option.ready then
			if tCC_Option:IsVisible() then
				tCC_Option:Hide()
			else
				tCC_Option:Show()
			end
		else
			tCC_Option_Init()
			collectgarbage('collect')
		end
	else
		if LoadAddOn("tdCoolDownOption") then
			tCC_Option_Init()
			collectgarbage('collect')
		else
			DEFAULT_CHAT_FRAME:AddMessage("|cff7fff7ftCC: "..tCClocal.DisOption.."|r")
		end
	end
end

