SMART_GROUPICON_ENABLE = true;

function SmartGroupIcon_OnEvent(event)
	if (event == "PLAYER_ENTERING_WORLD") then
		SmartGroupIcon_Update(PlayerFrameGroupIcon,SMART_PLAYER_Config.groupicon);
		SmartGroupIcon_Update(TargetFrameGroupIcon,SMART_TARGET_Config.groupicon);
		local chkMember = GetNumPartyMembers();
		if chkMember > 0 then
			for id=1, chkMember do
				SmartGroupIcon_Update(getglobal("PartyMemberFrame"..id.."GroupIcon"),SMART_PARTY_Config.groupicon);
			end
		end
	elseif (event == "PLAYER_TARGET_CHANGED") then
		SmartGroupIcon_Update(TargetFrameGroupIcon,SMART_TARGET_Config.groupicon);
	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
			for id=1, chkMember do
				SmartGroupIcon_Update(getglobal("PartyMemberFrame"..id.."GroupIcon"),SMART_PARTY_Config.groupicon);
			end
		end
	end
end

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

	local unitClass, _ = UnitClass(frame.unit);
	if unitClass then
		getglobal(frame:GetName().."Class"):SetTexture("Interface\\AddOns\\SmartGroupIcon\\Icon\\Class\\"..unitClass);
	else
		getglobal(frame:GetName().."Class"):SetTexture("");
	end
	if (frame.unit ~= "party1") and (frame.unit ~= "party2") and (frame.unit ~= "party3") and (frame.unit ~= "party4") then
		local unitGender = UnitSex(frame.unit);
		local unitRace = UnitRace(frame.unit);
		if unitRace and unitGender then
			if unitGender == 3 then
				getglobal(frame:GetName().."Race"):SetTexture("Interface\\AddOns\\SmartGroupIcon\\Icon\\Female\\"..UnitRace(frame.unit));
			elseif unitGender == 2 then
				getglobal(frame:GetName().."Race"):SetTexture("Interface\\AddOns\\SmartGroupIcon\\Icon\\Male\\"..UnitRace(frame.unit));
			else
				getglobal(frame:GetName().."Race"):SetTexture("");
			end
		else
			getglobal(frame:GetName().."Race"):SetTexture("");
		end
	end
	local unitRank;
	if UnitIsUnit(frame.unit,"player") then
		_, unitRank = GetPVPLifetimeStats();
	else
		unitRank = UnitPVPRank(frame.unit);
	end
	local	_, rankNumber = GetPVPRankInfo(unitRank);
	if ( rankNumber > 0 ) then
		getglobal(frame:GetName().."Rank"):SetTexture(format("%s%02d","Interface\\AddOns\\SmartGroupIcon\\Icon\\Rank\\PvPRank",rankNumber));
	else
		local unitFaction = UnitFactionGroup(frame.unit);
		if unitFaction == "Alliance" or unitFaction == "Horde" then
			getglobal(frame:GetName().."Rank"):SetTexture("Interface\\AddOns\\SmartGroupIcon\\Icon\\Rank\\"..UnitFactionGroup(frame.unit));
		else
			getglobal(frame:GetName().."Rank"):SetTexture("");
		end
	end
	if not frame:IsShown() then
		frame:Show();
	end

end

function SmartGroupIcon_OnEnter()
	GameTooltip:SetOwner(this, "ANCHOR_BOTTOMRIGHT");
	local userName = UnitPVPName(this.unit);
	local unitName, unitServer = UnitName(this.unit);
	if not userName then
		userName = unitName;
	end
	if unitServer then
		GameTooltip:AddLine(userName.." - "..unitServer,1,1,1);
	else
		GameTooltip:AddLine(userName,1,1,1);
	end
	GameTooltip:AddLine("레벨 "..UnitLevel(this.unit).." "..UnitRace(this.unit).." "..UnitClass(this.unit),1,1,1);
	local unitGuildName, unitGuildRankName, _ = GetGuildInfo(this.unit);
	if unitGuildName then
		GameTooltip:AddLine(unitGuildName.." ("..unitGuildRankName..")",1,0.82,0);
	end
	local unitRank;
	if UnitIsUnit(this.unit,"player") then
		_, unitRank = GetPVPLifetimeStats();
	else
		unitRank = UnitPVPRank(this.unit);
	end
	local	rankName, _ = GetPVPRankInfo(unitRank);
	if rankName then
		GameTooltip:AddLine(rankName,1,0.82,0);
	end
	GameTooltip:Show();
end
