

--ERR_NEW_LOOT_MASTER_S = "%s is now the loot master.";

FuBar_LootTyFu = FuBarPlugin:new({
	name          = "FuBar_LootTyFu",
	description   = "Shows party loot type and threshold.",
	version       = tonumber(string.sub("$Revision: 1609 $", 12, -3)),
	releaseDate   = string.sub("$Date: 2006-05-07 21:31:15 -0500 (Sun, 07 May 2006) $", 8, 17),
	aceCompatible = 103,
	fuCompatible  = 10000,
	author        = "Tekkub Stoutwrithe",
	email         = "tekkub@gmail.com",
	website       = "http://tekkub.wowinterface.com/",
	category      = "interface",
	db            = AceDatabase:new("FuBar_LootTyFuDB"),
	cmd           = AceChatCmd:new({}, {}),

	loc = GetLocale() == "frFR" and {
		label = "Butin : ",
		title = "Type de butin",
		mlshort = "ML: ",

		freeforall = "Acc\195\168s libre",
		roundrobin = "Chacun son tour",
		master = "Responsable du butin",
		group = "Butin de groupe",
		needbeforegreed = "Le besoin avant la cupidit\195\169",
		solo = "|cffddddddSolo",
		sololong = "Playing Solo",

		showlabel = "Show label",
		showmlname = "Show ML Name",
		performroll = "Perform roll when clicked",
	} or GetLocale() == "deDE" and {
		label = "Pl\195\188ndern: ",
		title = "Pl\195\188ndermethode",
		mlshort = "ML: ",

		freeforall = "Jeder gegen Jeden",
		roundrobin = "Reihum",
		master = "Pl\195\188ndermeister",
		group = "Pl\195\188ndern als Gruppe",
		needbeforegreed = "Bedarf vor Gier",
		solo = "|cffddddddSolo",
		sololong = "Playing Solo",

		showlabel = "Show label",
		showmlname = "Show ML Name",
		performroll = "Perform roll when clicked",
	} or {
		label = "Loot: ",
		title = "Loot Type",
		mlshort = "ML: ",

		freeforall = "Free for all",
		roundrobin = "Round robin",
		master = "Master looter",
		group = "Group Loot",
		needbeforegreed = "Need Before Greed",
		solo = "|cffddddddSolo",
		sololong = "Playing Solo",

		showlabel = "Show label",
		showmlname = "Show ML Name",
		performroll = "Perform roll when clicked",
	},

	hasIcon = true,

	colors = {},
})


function FuBar_LootTyFu:Initialize()
	self.compost = CompostLib:GetInstance("compost-1")
	for i=0,6 do
		local _, _, _, hex = GetItemQualityColor(i)
		self.colors[i] = {hex, getglobal("ITEM_QUALITY".. i.. "_DESC")}
	end
end


function FuBar_LootTyFu:Enable()
	self.timeSinceLastUpdate = 0

	self:RegisterEvent("CHAT_MSG_SYSTEM")
	self:RegisterEvent("PARTY_MEMBERS_CHANGED", "Update")
	self:RegisterEvent("PARTY_LOOT_METHOD_CHANGED", "Update")
	self:RegisterEvent("RAID_ROSTER_UPDATE", "Update")
end


function FuBar_LootTyFu:Disable()
	self.timeSinceLastUpdate = nil
end


function FuBar_LootTyFu:OnClick()
	if not self.data.supressrollclick then RandomRoll("1", "100") end
end


local babble = BabbleLib:GetInstance("Core 1.1")

function FuBar_LootTyFu:CHAT_MSG_SYSTEM()
	local mlname = babble:Deformat(arg1, ERR_NEW_LOOT_MASTER_S)
	if mlname then
		self.mlname = mlname
		self:Update()
	end
end

function FuBar_LootTyFu:UpdateData()
	if (GetNumPartyMembers() > 0) or (GetNumRaidMembers() > 0) then
		local ltype, ml = GetLootMethod()
--		self.mlname = ml and ml >= 0 and (ml == 0 and UnitName("player") or UnitName("party"..ml))
		self.loottype = self.loc[ltype]
		self.threshold = self.colors[GetLootThreshold()]
	else
		self.loottype, self.threshold = nil, nil
	end
end


function FuBar_LootTyFu:UpdateText()
	self:SetText(string.format("%s%s%s%s",
		self.data.showlabel and self.loc.label or "",
		self.threshold and self.threshold[1] or "|cff888888",
		self.data.showmlname and self.loottype == self.loc.master and self.loc.mlshort or self.threshold and self.loottype or self.loc.solo,
		self.data.showmlname and self.loottype == self.loc.master and self.mlname or ""))
end


function FuBar_LootTyFu:UpdateTooltip()
	self.tooltip:ClearLines()

	if (GetNumPartyMembers() == 0) and (GetNumRaidMembers() == 0) then
		self.tooltip:AddLine(nil, self.loc.sololong, 1, 1, 1, nil, "CENTER")
	elseif self.loottype and self.threshold then
		self.tooltip:AddDoubleLine(nil, LOOT_METHOD, self.loottype, nil,nil,nil, 1,1,1)
		self.tooltip:AddDoubleLine(nil, LOOT_THRESHOLD, self.threshold[1]..self.threshold[2])
		if self.mlname then self.tooltip:AddDoubleLine(nil, self.loc.master.. ":", self.mlname) end
	else self.tooltip:AddLine(nil, ERR_NOT_IN_GROUP) end
end


function FuBar_LootTyFu:MenuSettings()
	if (GetNumPartyMembers() > 0) or (GetNumRaidMembers() > 0) then
		local t = PlayerFrameDropDown
		t.unit = "player"
		t.name = UnitName("player")
		ToggleDropDownMenu(1, nil, t, "cursor")

		FuBarUtils.AddSpacerDropDownMenu()
	end

	local t = self.compost:Acquire()
	UIDropDownMenu_AddButton(self.compost:RecycleHash(t,
		"text", self.loc.showlabel,
		"func", function() self:ToggleOption("showlabel") end,
		"checked", self.data.showlabel,
		"keepShownOnClick", true
	))
	UIDropDownMenu_AddButton(self.compost:RecycleHash(t,
		"text", self.loc.showmlname,
		"func", function() self:ToggleOption("showmlname") end,
		"checked", self.data.showmlname,
		"keepShownOnClick", true
	))
	UIDropDownMenu_AddButton(self.compost:RecycleHash(t,
		"text", self.loc.performroll,
		"func", function() self:ToggleOption("supressrollclick") end,
		"checked", not self.data.supressrollclick,
		"keepShownOnClick", true
	))

	self.compost:Reclaim(t)
end


function FuBar_LootTyFu:ToggleOption(var)
	if self.data[var] then self.data[var] = nil
	else self.data[var] = true end
	self:Update()
end


FuBar_LootTyFu:RegisterForLoad()

