-- Class /who listing
-- Created by Thott

ClassWhoClasses = {
	"Druid",
	"Hunter",
	"Mage",
	"Paladin",
	"Priest",
	"Rogue",
	"Shaman",
	"Warlock",
	"Warrior"
};
ClassWhoClassesNumber = { };
ClassWhoClassesNumberMax = 0;

function ClassWho_OnLoad()
  this:RegisterEvent("WHO_LIST_UPDATE");
  local id = "TWHO";
  local func = function()
        ClassWho_ClassDistribution();
  end
  Cosmos_RegisterChatCommand ( id, CLASSWHO_COMM, func, CLASSWHO_COMM_INFO );
end
function ClassWho_ClassDistribution()
  ClassWho.WhoState = 1;
  ClassWho.WhoReport = {};
  ClassWho.Total = 0;
  SendWho("c-"..ClassWhoClasses[ClassWho.WhoState]);
end
function ClassWho_Compare(a,b)
  return a.Count > b.Count;
end
function ClassWho_OnEvent()
  if(ClassWho.WhoState) then
    local numWhos, totalCount = GetNumWhoResults();
    local WhoRec = {};
    WhoRec.Class = ClassWho.WhoState;
    WhoRec.Count = totalCount;
    ClassWho.WhoReport[ClassWho.WhoState] = WhoRec;
    ClassWho.Total = ClassWho.Total + totalCount;
    ClassWho.WhoState = ClassWho.WhoState + 1;
    if(ClassWhoClasses[ClassWho.WhoState]) then
      SendWho("c-"..ClassWhoClasses[ClassWho.WhoState]);
    else
      table.sort(ClassWho.WhoReport,ClassWho_Compare);
      --Print(CLASSWHO_BAR);
      local faction = UnitFaction("player");
      if (faction == 0) then 
        faction = ALLIANCE; 
      else 
        faction = HORDE; 
      end
      Print(format(CLASSWHO_GLOBAL, faction, GetDate(), GetTimeString(GetTime())));
      for id,rec in ClassWho.WhoReport do
        local percent = rec.Count * 100 / ClassWho.Total;
	local text = ClassWhoClasses[rec.Class];
	if(not (id == 9 and rec.Count == 0)) then
	  Print(format(CLASSWHO_EACHCLASS, id, text, rec.Count, percent));
	end
      end
      Print(format(CLASSWHO_TOTAL, ClassWho.Total));
      --Print(CLASSWHO_BAR);
      HideUIPanel(FriendsFrame);
      ClassWho.WhoState = nil;
      ClassWho.WhoReport = nil;
    end
  end
end
