-------------------------------------------------------
--
-- SMART - ClassIcon
--
-------------------------------------------------------
SMART_CLASSICON_ENABLE = true

SMART_CLASSICON_PLAYER = true
SMART_CLASSICON_TARGET = true
SMART_CLASSICON_PARTY = true

function SmartClassIcon_Update(frame,showFrame)
	if (not showFrame) or (not UnitExists(frame.unit)) or (not UnitIsPlayer(frame.unit)) then
		if frame:IsShown() then
			frame:Hide()
		end
		return
	end

	local _, unitClass = UnitClass(frame.unit)

	if unitClass then
		getglobal(frame:GetName().."Texture"):SetTexture("Interface\\AddOns\\SmartClassIcon\\Icons\\"..unitClass)
		if not frame:IsShown() then
			frame:Show()
		end
	else
		if frame:IsShown() then
			frame:Hide()
		end
	end
end

function SmartClassIcon_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		if SMART_FOCUS_ENABLE then
			this:RegisterEvent("PLAYER_FOCUS_CHANGED")
			if not getglobal("SmartFocusFrameClassTypeIcon") then
				CreateFrame("Frame","SmartFocusFrameClassTypeIcon",SmartFocusFrame,"SmartClassTypeIconTemplate2")
			end
		end
	elseif (event == "PLAYER_ENTERING_WORLD") then
		SmartClassIcon_Update(PlayerFrameClassTypeIcon, SMART_CLASSICON_PLAYER)
		SmartClassIcon_Update(TargetFrameClassTypeIcon, SMART_CLASSICON_TARGET)
		local chkMember = GetNumPartyMembers()
		if chkMember > 0 then
			local id
			for id = 1, chkMember do
				SmartClassIcon_Update(getglobal("PartyMemberFrame"..id.."ClassTypeIcon"),SMART_CLASSICON_PARTY)
			end
		end
	elseif (event == "PLAYER_TARGET_CHANGED") then
		SmartClassIcon_Update(TargetFrameClassTypeIcon, SMART_CLASSICON_TARGET)
	elseif (event == "PLAYER_FOCUS_CHANGED") and SMART_FOCUS_ENABLE then
		SmartClassIcon_Update(SmartFocusFrameClassTypeIcon,SMART_CLASSICON_TARGET)
	elseif (event == "PARTY_MEMBERS_CHANGED") or (event == "PARTY_MEMBER_ENABLE") or (event == "PARTY_MEMBER_DISABLE") or (event == "PARTY_LEADER_CHANGED") then
		local chkMember = GetNumPartyMembers()
		if chkMember > 0 then
			local id
			for id=1, chkMember do
				SmartClassIcon_Update(getglobal("PartyMemberFrame"..id.."ClassTypeIcon"),SMART_CLASSICON_PARTY)
			end
		end
	end
end

