local _G = getfenv(0)
local min = _G.min
local max = _G.max
local ceil = _G.ceil
local floor = _G.floor
local select = _G.select
local UnitStat = _G.UnitStat
local UnitDamage = _G.UnitDamage
local UnitAttackSpeed = _G.UnitAttackSpeed
local UnitAttackPower = _G.UnitAttackPower
local GetCombatRating = _G.GetCombatRating
local GetCritChance = _G.GetCritChance
local GetExpertise = _G.GetExpertise
local HasWandEquipped = _G.HasWandEquipped
local UnitRangedDamage = _G.UnitRangedDamage
local UnitRangedAttackPower = _G.UnitRangedAttackPower
local GetRangedCritChance = _G.GetRangedCritChance
local GetSpellBonusDamage = _G.GetSpellBonusDamage
local GetSpellBonusHealing = _G.GetSpellBonusHealing
local GetSpellCritChance = _G.GetSpellCritChance
local GetManaRegen = _G.GetManaRegen
local UnitArmor = _G.UnitArmor
local UnitDefense = _G.UnitDefense
local GetDodgeChance = _G.GetDodgeChance
local GetParryChance = _G.GetParryChance
local GetBlockChance = _G.GetBlockChance

local minDamage, maxDamage, base, posBuff, negBuff, modifier, rangedAttackSpeed, spellDamage, spellCrit, minResilience

InvenSimulatorUI.currentStatValues = {
	["힘"] = function() return max(select(2, UnitStat("player", 1)), 0) end,
	["민첩성"] = function() return max(select(2, UnitStat("player", 2)), 0) end,
	["체력"] = function() return max(select(2, UnitStat("player", 3)), 0) end,
	["지능"] = function() return max(select(2, UnitStat("player", 4)), 0) end,
	["정신력"] = function() return max(select(2, UnitStat("player", 5)), 0) end,
	["공격력"] = function()
		minDamage, maxDamage = UnitDamage("player")
		return (floor(minDamage) + ceil(maxDamage)) / 2 / UnitAttackSpeed("player")
	end,
	["속도"] = function() return UnitAttackSpeed("player") end,
	["전투력"] = function()
		base, posBuff, negBuff = UnitAttackPower("player")
		return max((base + posBuff + negBuff), 0)
	end,
	["적중도"] = function() return GetCombatRating(CR_HIT_MELEE) or 0 end,
	["치명타율"] = function() return GetCritChance() or 0 end,
	["숙련"] = function() return GetExpertise() end,
	["원거리 공격력"] = function()
		rangedAttackSpeed, minDamage, maxDamage = 2, UnitRangedDamage("player")
		return (floor(minDamage) + ceil(maxDamage)) / 2 / rangedAttackSpeed
	end,
	["원거리 속도"] = function() return UnitRangedDamage("player") or 0 end,
	["원거리 전투력"] = function()
		base, posBuff, negBuff = UnitRangedAttackPower("player")
		return max((base + posBuff + negBuff), 0)
	end,
	["원거리 적중도"] = function() return GetCombatRating(CR_HIT_RANGED) or 0 end,
	["원거리 치명타율"] = function() return GetRangedCritChance() or 0 end,
	["주문 공격력"] = function()
		spellDamage = GetSpellBonusDamage(2)
		for i = 3, MAX_SPELL_SCHOOLS do
			spellDamage = min(spellDamage, GetSpellBonusDamage(i))
		end
		return spellDamage
	end,
	["주문 치유량"] = function() return GetSpellBonusHealing() or 0 end,
	["주문 적중도"] = function() return GetCombatRating(CR_HIT_SPELL) or 0 end,
	["극대화율"] = function()
		spellCrit = GetSpellCritChance(2)
		for i = 3, MAX_SPELL_SCHOOLS do
			spellCrit = min(spellCrit, GetSpellCritChance(i))
		end
		return spellCrit
	end,
	["주문 가속도"] = function() return GetCombatRating(CR_HASTE_SPELL) or 0 end,
	["5초당 마나 회복량"] = function() return GetManaRegen() * 5 end,
	["방어도"] = function() return select(2, UnitArmor("player")) or 0 end,
	["방어 숙련"] = function()
		base, modifier = UnitDefense("player")
		return base + modifier
	end,
	["회피율"] = function() return GetDodgeChance() or 0 end,
	["무기 막기"] = function() return GetParryChance() or 0 end,
	["방패 막기"] = function() return GetBlockChance() or 0 end,
	["탄력도"] = function()
		minResilience = min(GetCombatRating(CR_CRIT_TAKEN_MELEE), GetCombatRating(CR_CRIT_TAKEN_RANGED))
		return min(minResilience, GetCombatRating(CR_CRIT_TAKEN_SPELL))
	end,
}

local playerStatUpdater = CreateFrame("Frame")
playerStatUpdater:Hide()
playerStatUpdater:RegisterEvent("CHARACTER_POINTS_CHANGED")
playerStatUpdater:RegisterEvent("PLAYER_DAMAGE_DONE_MODS")
playerStatUpdater:RegisterEvent("SKILL_LINES_CHANGED")
playerStatUpdater:RegisterEvent("COMBAT_RATING_UPDATE")
playerStatUpdater:RegisterEvent("UNIT_LEVEL")
playerStatUpdater:RegisterEvent("UNIT_STATS")
playerStatUpdater:RegisterEvent("UNIT_DAMAGE")
playerStatUpdater:RegisterEvent("UNIT_RANGEDDAMAGE")
playerStatUpdater:RegisterEvent("UNIT_RESISTANCES")
playerStatUpdater:RegisterEvent("UNIT_ATTACK")
playerStatUpdater:RegisterEvent("UNIT_ATTACK_SPEED")
playerStatUpdater:RegisterEvent("UNIT_ATTACK_POWER")
playerStatUpdater:RegisterEvent("UNIT_RANGED_ATTACK_POWER")
playerStatUpdater:RegisterEvent("UNIT_MAXHEALTH")
playerStatUpdater:RegisterEvent("UNIT_MAXMANA")

playerStatUpdater:SetScript("OnEvent", function(self, event, unit)
	if event:find("^UNIT") then
		if unit == "player" then
			if InvenSimulatorUI:IsShown() then
				self.timer = 0
				self:Show()
			elseif self:IsShown() then
				self:Hide()
			end
		end
	elseif InvenSimulatorUI:IsShown() then
		self.timer = 0
		self:Show()
	elseif self:IsShown() then
		self:Hide()
	end
end)
playerStatUpdater:SetScript("OnUpdate", function(self, timer)
	self.timer = self.timer + timer
	if self.timer > 0.1 then
		if InvenSimulatorUI:IsShown() then
			InvenSimulatorUI:UpdateStatList("left")
			InvenSimulatorUI:UpdateStatList("right")
			InvenSimulatorUI:UpdatePlayerHealthAndManaText()
		end
		self:Hide()
	end
end)