-------------------------------------------------------------------------------
--
--  Mod Name : BlinkCrits 2.8
--  Author   : Blink
--  Date     : 2005/06/28
--  LastUpdate : 2009/08/12
--
-------------------------------------------------------------------------------
local major = 2.0;
local minor = .8;

local tankerClass = {
	["WARRIOR"] = 1,
	["DRUID"] = 1,
	["PALADIN"] = 1,
	["SHAMAN"] = 1, -- ?
	["HUNTER"] = 1, -- ?
	["DEATHKNIGHT"] = 1,
};

local meleeClass = {
	["WARRIOR"] = 1,
	["ROGUE"] = 1,
	["DRUID"] = 1,
	["PALADIN"] = 1,
	["SHAMAN"] = 1,
	["HUNTER"] = 1,
	["DEATHKNIGHT"] = 1,
};

local rangedClass = {
	["WARRIOR"] = 1, -- ?
	["ROGUE"] = 1, -- ?
	["HUNTER"] = 1,
};

local manaClass = {
	["DRUID"] = 1,
	["PALADIN"] = 1,
	["SHAMAN"] = 1,
	["HUNTER"] = 1,
	["MAGE"] = 1,
	["WARLOCK"] = 1,
	["PRIEST"] = 1,
};

local casterClass = {
	["DRUID"] = 1,
	["PALADIN"] = 1,
	["SHAMAN"] = 1,
	["MAGE"] = 1,
	["WARLOCK"] = 1,
	["PRIEST"] = 1,
};

local healerClass = {
	["DRUID"] = 1,
	["PALADIN"] = 1,
	["SHAMAN"] = 1,
	["PRIEST"] = 1,
};

local hasPetClass = {
	["HUNTER"] = 1,
	["WARLOCK"] = 1,
	["SHAMAN"] = 1,
	["MAGE"] = 1,
	["DEATHKNIGHT"] = 1,
};

local showStat = {};

local tooltip = CreateFrame("GameTooltip")
do
	if not tooltip.left then
		tooltip.left = {}
		tooltip.right = {}
	end
	for i = #tooltip.left+1, 30 do
		local left, right = tooltip:CreateFontString(), tooltip:CreateFontString()
		tooltip.left[i] = left
		tooltip.right[i] = right
		left:SetFontObject(GameFontNormal)
		right:SetFontObject(GameFontNormal)
		tooltip:AddFontStrings(left, right)
	end
	tooltip:SetOwner(UIParent, "ANCHOR_NONE")
end

-- resilience
local getResilience = function ()
--	local melee = GetCombatRating(CR_CRIT_TAKEN_MELEE);
--	local ranged = GetCombatRating(CR_CRIT_TAKEN_RANGED);
--	local spell = GetCombatRating(CR_CRIT_TAKEN_SPELL);

--	local minResilience = min(melee, ranged);
--	minResilience = min(minResilience, spell);
	return 0.0; --minResilience;

--	local lowestRating = CR_CRIT_TAKEN_MELEE;
--	if ( melee == minResilience ) then
--		lowestRating = CR_CRIT_TAKEN_MELEE;
--	elseif ( ranged == minResilience ) then
--		lowestRating = CR_CRIT_TAKEN_RANGED;
--	else
--		lowestRating = CR_CRIT_TAKEN_SPELL;
--	end
--	return GetCombatRatingBonus(lowestRating),GetCombatRatingBonus(lowestRating)*2;
end

-- armor
local getArmor = function (unit)
	if ( not unit ) then
		unit = "player";
	end
	local base, effectiveArmor, armor, posBuff, negBuff = UnitArmor(unit);
	local armorReduction = PaperDollFrame_GetArmorReduction(effectiveArmor, UnitLevel(unit));
	return ColorPaperDollStat(base, posBuff, negBuff), string.format("%0.2f%%",armorReduction);
end

local function buildShowStat()
	local class = select(2,UnitClass("player"));

	for k,v in pairs(showStat) do
		showStat[k] = nil;
	end

	-- common
	showStat["block"] = 1;
	showStat["dodge"] = 1;
	showStat["parry"] = 1;
	--showStat["resilience"] = 1;
	showStat["durability"] = 1;

	if ( tankerClass[class] ) then
		showStat["armor"] = 1;
		showStat["armorReduction"] = 1;
		showStat["defense"] = 1;
	end

	if ( hasPetClass[class] ) then
		showStat["petArmor"] = 1;
		showStat["petArmorReduction"] = 1;
	end

	if ( meleeClass[class] ) then
		showStat["crit"] = 1;
		showStat["attackpower"] = 1;
		showStat["ratingmelee"] = 1;
		showStat["hastemelee"] = 1;
	end

	if ( rangedClass[class] ) then
		showStat["rangedcrit"] = 1;
		showStat["attackpowerranged"] = 1;
		showStat["ratingranged"] = 1;
		showStat["hasteranged"] = 1;
	end

	if ( casterClass[class] ) then
		showStat["spellcrit"] = 1;
		showStat["hastespell"] = 1;
		showStat["spellbonusdamage"] = 1;
		showStat["ratingspell"] = 1;
		showStat["spellpenetration"] = 1;
	end

	if ( healerClass[class] ) then
		showStat["spellbonushealing"] = 1;
	end

	if ( manaClass[class] ) then
		showStat["manaregen"] = 1;
	end
	showStat["honor"] = 1;

	if UnitLevel("player") < 80 then
		showStat["experience"] = 1;
	end

	showStat["reputation"] = 1;
end


--[[

autocrit = default
crit
rangedcrit
spellcrit

durability

block
dodge
parry
resilience
armor
armorReduction
defense

attackpower
attackpowerranged
spellbonusdamage
spellbonushealing

ratingmelee
ratingranged
ratingspell

spellpenetration
manaregen
]]

function BlinkCrits_OnLoad(self)
	self:RegisterEvent("PLAYER_ENTERING_WORLD");
	self:RegisterEvent("UNIT_LEVEL");
	self:RegisterEvent("UNIT_INVENTORY_CHANGED");
	self:RegisterEvent("UNIT_ATTACK_POWER");
	self:RegisterEvent("UPDATE_BONUS_ACTIONBAR");
	self:RegisterEvent("UNIT_STATS");
	self:RegisterEvent("PLAYER_AURAS_CHANGED");
	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("PLAYER_XP_UPDATE");
	self:RegisterEvent("UPDATE_FACTION");
	self:RegisterEvent("COMBAT_RATING_UPDATE");

	self:RegisterForClicks("RightButtonUp");

	BlinkCritsDB = {};
end

function BlinkCrits_OnEvent(self, event, ...)
	if ( event == "VARIABLES_LOADED" ) then
		if ( not BlinkCritsDB.major or BlinkCritsDB.major < major ) then
			BlinkCritsDB = {
				major = major,
				show = "autocrit"
			};
		end
		self:UnregisterEvent("VARIABLES_LOADED");
		buildShowStat();
	end

	local value = BlinkCrits_ScanCrits();
	if ( value ) then
		BlinkCritsText:SetText(value);
		BlinkCrits:SetWidth(BlinkCritsText:GetWidth()+28);
		BlinkCrits:Show();
	else
		BlinkCrits:Hide();
	end
end

function BlinkCrits_ScanStat(stattype)
	if ( stattype == "crit" and GetCritChance() > 0 ) then
		return string.format("%.2f%%", GetCritChance());
	elseif ( stattype == "rangedcrit" and GetRangedCritChance() > 0 ) then
		return string.format("%.2f%%", GetRangedCritChance());
	elseif ( stattype == "spellcrit" ) then
		local minsc = GetSpellCritChance(2);
		for i=3, MAX_SPELL_SCHOOLS do
			spellcrit = GetSpellCritChance(i);
			minsc = max(minsc, spellcrit);
		end
		if ( minsc > 0 ) then
			return string.format("%.2f%%", minsc);
		end
	elseif ( stattype == "block" and GetBlockChance() > 0 ) then
		return string.format("%.2f%%", GetBlockChance());
	elseif ( stattype == "dodge" and GetDodgeChance() > 0 ) then
		return string.format("%.2f%%", GetDodgeChance());
	elseif ( stattype == "parry" and GetParryChance() > 0 ) then
		return string.format("%.2f%%", GetParryChance());
	elseif ( stattype == "resilience" and getResilience() > 0 ) then
		return string.format("%d",getResilience());
	elseif ( stattype == "armor" ) then
		local armor, reduce = getArmor();
		return armor;
	elseif ( stattype == "armorReduction" ) then
		local armor, reduce = getArmor();
		return reduce;
	elseif ( stattype == "petArmor" ) then
		if ( UnitExists("pet") ) then
			local armor, reduce = getArmor("pet");
			return armor;
		end
	elseif ( stattype == "petArmorReduction" ) then
		if ( UnitExists("pet") ) then
			local armor, reduce = getArmor("pet");
			return reduce;
		end
	elseif ( stattype == "defense" ) then
		local base, defensemodifier = UnitDefense("player");
		local posBuff, negBuff = 0, 0;
		if ( defensemodifier < 0 ) then
			negBuff = defensemodifier;
		elseif ( defensemodifier > 0 ) then
			posBuff = defensemodifier;
		end
		return ColorPaperDollStat(base, posBuff, negBuff);
	elseif ( stattype == "attackpower" ) then
		local base, posBuff, negBuff = UnitAttackPower("player");
		if ( base + posBuff + negBuff > 0 ) then
			return ColorPaperDollStat(base, posBuff, negBuff);
		end
	elseif ( stattype == "attackpowerranged" ) then
		local base, posBuff, negBuff = UnitRangedAttackPower("player");
		if ( base + posBuff + negBuff > 0 ) then
			return ColorPaperDollStat(base, posBuff, negBuff);
		end
	elseif ( stattype == "spellbonusdamage" ) then
		local minbd = GetSpellBonusDamage(2);
		for i=3, MAX_SPELL_SCHOOLS do
			bonusDamage = GetSpellBonusDamage(i);
			minbd = min(minbd, bonusDamage);
		end
		if ( minbd > 0 ) then
			return minbd;
		end
	elseif ( stattype == "spellbonushealing" and GetSpellBonusHealing() > 0 ) then
		return GetSpellBonusHealing();
	elseif ( stattype == "ratingmelee" and GetCombatRatingBonus(CR_HIT_MELEE) > 0 ) then
		return string.format("%.2f%%", GetCombatRatingBonus(CR_HIT_MELEE));
	elseif ( stattype == "ratingranged" and GetCombatRatingBonus(CR_HIT_RANGED) > 0 ) then
		return string.format("%.2f%%", GetCombatRatingBonus(CR_HIT_RANGED));
	elseif ( stattype == "ratingspell" and GetCombatRatingBonus(CR_HIT_SPELL) > 0 ) then
		return string.format("%.2f%%", GetCombatRatingBonus(CR_HIT_SPELL));
	elseif ( stattype == "spellpenetration" and GetSpellPenetration() > 0 ) then
		return GetSpellPenetration();
	elseif ( stattype == "manaregen" and UnitHasMana("player") and GetManaRegen()>0 ) then
		local base, casting = GetManaRegen();
		base = floor( base * 5.0 );
		casting = floor( casting * 5.0);
		if ( casting > 0 ) then
			return base.."/"..casting;
		else
			return base;
		end
	elseif ( stattype == "hastemelee" ) then
		--return string.format("%d(+%.2f%%)", GetCombatRating(CR_HASTE_MELEE), GetCombatRatingBonus(CR_HASTE_MELEE));
		return string.format("%d", GetCombatRating(CR_HASTE_MELEE));
		--return string.format("+%.2f%", GetCombatRatingBonus(CR_HASTE_MELEE));
	elseif ( stattype == "hasteranged" ) then
		--return string.format("%d(+%.2f%%)", GetCombatRating(CR_HASTE_RANGED), GetCombatRatingBonus(CR_HASTE_RANGED));
		return string.format("%d", GetCombatRating(CR_HASTE_RANGED));
		--return string.format("+%.2f%", GetCombatRatingBonus(CR_HASTE_RANGED));
	elseif ( stattype == "hastespell" ) then
		--return string.format("%d(+%.2f%%)", GetCombatRating(CR_HASTE_SPELL), GetCombatRatingBonus(CR_HASTE_SPELL));
		return string.format("%d", GetCombatRating(CR_HASTE_SPELL));
		--return string.format("+%.2f%", GetCombatRatingBonus(CR_HASTE_SPELL));
	elseif ( stattype == "durability" ) then
		local curr, max, cost, broken = DurabilityCount_GetDurability();
		local dur = 100 * curr/max;
		local stat;
		if ( dur < 20 ) then
			stat = RED_FONT_COLOR_CODE..string.format("%d%%",dur)..FONT_COLOR_CODE_CLOSE
		elseif ( dur < 80 ) then
			stat = "|cffffff20"..string.format("%d%%",dur)..FONT_COLOR_CODE_CLOSE
		else
			stat = GREEN_FONT_COLOR_CODE..string.format("%d%%",dur)..FONT_COLOR_CODE_CLOSE
		end
		if ( broken > 0 ) then
			return string.format("(%d) %s", broken, stat);
		else
			return string.format("%s", stat);
		end
	elseif ( stattype == "autocrit" ) then
		local c, r, s = 0, 0, 0;
		if ( showStat["crit"] ) then
			c = GetCritChance();
		end
		if ( showStat["rangedcrit"] ) then
			r = GetRangedCritChance();
		end
		if ( showStat["spellcrit"] ) then
			s = GetSpellCritChance(2);
			local spellcrit;
			for i=3, MAX_SPELL_SCHOOLS do
				spellcrit = GetSpellCritChance(i);
				--s = min(s, spellcrit);
				s = max(s, spellcrit);
			end
		end
		local maxcrit = max(c,r,s);
		local primary;
		if ( maxcrit == c ) then
			primary = "crit";
		elseif ( maxcrit == r ) then
			primary = "rangedcrit";
		elseif ( maxcrit == s ) then
			primary = "spellcrit";
		end
		return string.format("%.2f%%", maxcrit), primary;
	elseif stattype == "honor" then
		local _, honorPoint = GetPVPSessionStats ();
		return honorPoint;
	elseif stattype == "experience" then
		local currXP = UnitXP("player");
		local nextXP = UnitXPMax("player");
		local eh = GetXPExhaustion();
		if eh and eh>0 then
			return "|cff0063e0"..string.format("%d%%", (currXP/nextXP)*100)..FONT_COLOR_CODE_CLOSE;
		else
			return "|cff93008c"..string.format("%d%%", (currXP/nextXP)*100)..FONT_COLOR_CODE_CLOSE;
		end
	elseif stattype == "reputation" then
		local name, reaction, min, max, value = GetWatchedFactionInfo();
		if name then
			max = max - min;
			value = value - min;
			min = 0;
			return string.format("%d%%", (value/max)*100);
		else
			return "N/A";
		end
	end
	return nil;
end

function BlinkCrits_ScanCrits()
	if ( not BlinkCritsDB.show or not showStat[BlinkCritsDB.show] ) then
		BlinkCritsDB.show = "autocrit";
	end
	return BlinkCrits_ScanStat(BlinkCritsDB.show);
end

function BlinkCrits_OnEnter(frame)
	local values = {};
	GameTooltip:SetOwner(frame, "ANCHOR_BOTTOMRIGHT");

	for stat, _ in pairs(showStat) do
		value = BlinkCrits_ScanStat(stat);
		if ( value ) then
			values[stat] = value;
		end
	end

	if ( values["crit"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_CRIT_MELEE, values["crit"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["rangedcrit"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_CRIT_RANGED, values["rangedcrit"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["spellcrit"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_CRIT_SPELL, values["spellcrit"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["durability"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_DURABILITY, values["durability"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["armor"] ) then
		if ( values["armorReduction"] ) then
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_ARMOR, values["armor"].."("..values["armorReduction"]..")", 1.0,1.0,1.0, 1.0,0.82,0,1);
		else
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_ARMOR, values["armor"], 1.0,1.0,1.0, 1.0,0.82,0,1);
		end
	end

	if ( values["defense"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_DEFENSE, values["defense"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["petArmor"] ) then
		if ( values["petArmorReduction"] ) then
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_PET_ARMOR, values["petArmor"].."("..values["petArmorReduction"]..")", 1.0,1.0,1.0, 1.0,0.82,0,1);
		else
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_PET_ARMOR, values["petArmor"], 1.0,1.0,1.0, 1.0,0.82,0,1);
		end
	end

	if ( values["block"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_CHANCE_TO_BLOCK, values["block"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["dodge"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_CHANCE_TO_DODGE, values["dodge"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["parry"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_CHANCE_TO_PARRY, values["parry"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["resilience"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_RESILIENCE, values["resilience"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["attackpower"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_MELEE_ATTACK_POWER, values["attackpower"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["attackpowerranged"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_RANGED_ATTACK_POWER, values["attackpowerranged"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["ratingmelee"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_MELEE_HIT_RATING, values["ratingmelee"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["ratingranged"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_RANGED_HIT_RATING, values["ratingranged"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["ratingspell"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_SPELL_HIT_RATING, values["ratingspell"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["hastemelee"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_HASTE_RATING_MELEE, values["hastemelee"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["hasteranged"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_HASTE_RATING_RANGED, values["hasteranged"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["hastespell"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_HASTE_RATING_SPELL, values["hastespell"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["spellbonusdamage"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_BONUS_DAMAGE, values["spellbonusdamage"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["spellbonushealing"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_BONUS_HEALING, values["spellbonushealing"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end

	if ( values["spellpenetration"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_SPELL_PENETRATION, values["spellpenetration"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["manaregen"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_MANA_REGEN, values["manaregen"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["honor"] ) then
		GameTooltip:AddDoubleLine(BC_TEXT_MENU_HONOR, values["honor"], 1.0,1.0,1.0, 1.0,0.82,0,1);
	end
	if ( values["experience"] ) then
		local eh = GetXPExhaustion();
		if eh and eh>0 then
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_XP, values["experience"].."("..eh..")", 1.0,1.0,1.0, 1.0,0.82,0,1);
		else
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_XP, values["experience"], 1.0,1.0,1.0, 1.0,0.82,0,1);
		end
	end
	if ( values["reputation"] ) then
		local name, reaction, min, max, value = GetWatchedFactionInfo();
		if name then
			max = max - min;
			value = value - min;
			min = 0;
			GameTooltip:AddDoubleLine(name.." "..BC_TEXT_MENU_REPUTATION, value.." / "..max, 1.0,1.0,1.0, 1.0,0.82,0,1);
		else
			GameTooltip:AddDoubleLine(BC_TEXT_MENU_REPUTATION, "N/A", 1.0,1.0,1.0, 1.0,0.82,0,1);
		end
	end
	GameTooltip:Show();
end

function BlinkCrits_OnClick()
	HideDropDownMenu(1);
	UIDropDownMenu_Initialize(BlinkCritDropDown, function ()
		local values = {};
		for stat, _ in pairs(showStat) do
			value = BlinkCrits_ScanStat(stat);
			if ( value ) then
				values[stat] = value;
			end
		end

		local info = UIDropDownMenu_CreateInfo();
		info.isTitle = 1;
		info.text = "BlinkCrits v"..(major+minor);
		UIDropDownMenu_AddButton(info);

		info = UIDropDownMenu_CreateInfo();
		info.text = BC_TEXT_MENU_AUTOSELECT_CRIT;
		info.checked = BlinkCrits_IsChecked("autocrit");
		info.tooltipTitle = BC_TEXT_MENU_AUTOSELECT_CRIT_TOOLTIP_TITLE;
		info.tooltipText = BC_TEXT_MENU_AUTOSELECT_CRIT_TOOLTIP_DESC;
		info.func = function ()
			BlinkCrits_SetShow("autocrit");
		end
		UIDropDownMenu_AddButton(info);

		if ( values["crit"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_CRIT_MELEE;
			info.tooltipTitle = BC_TEXT_MENU_CRIT_MELEE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_CRIT_MELEE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("crit");
			info.func = function ()
				BlinkCrits_SetShow("crit");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["rangedcrit"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_CRIT_RANGED;
			info.tooltipTitle = BC_TEXT_MENU_CRIT_RANGED_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_CRIT_RANGED_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("rangedcrit");
			info.func = function ()
				BlinkCrits_SetShow("rangedcrit");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["spellcrit"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_CRIT_SPELL;
			info.tooltipTitle = BC_TEXT_MENU_CRIT_SPELL_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_CRIT_SPELL_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("spellcrit");
			info.func = function ()
				BlinkCrits_SetShow("spellcrit");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["durability"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_DURABILITY;
			info.tooltipTitle = BC_TEXT_MENU_DURABILITY_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_DURABILITY_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("durability");
			info.func = function ()
				BlinkCrits_SetShow("durability");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- tanker class
		if ( values["armor"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_ARMOR;
			info.tooltipTitle = BC_TEXT_MENU_ARMOR_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_ARMOR_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("armor");
			info.func = function ()
				BlinkCrits_SetShow("armor");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["armorReduction"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_ARMORAMOUNT;
			info.tooltipTitle = BC_TEXT_MENU_ARMORAMOUNT_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_ARMORAMOUNT_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("armorReduction");
			info.func = function ()
				BlinkCrits_SetShow("armorReduction");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["defense"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_DEFENSE;
			info.tooltipTitle = BC_TEXT_MENU_DEFENSE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_DEFENSE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("defense");
			info.func = function ()
				BlinkCrits_SetShow("defense");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- pet
		if ( values["petArmor"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_PET_ARMOR;
			info.tooltipTitle = BC_TEXT_MENU_PET_ARMOR_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_PET_ARMOR_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("petArmor");
			info.func = function ()
				BlinkCrits_SetShow("petArmor");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["petArmorReduction"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_PET_ARMORAMOUNT;
			info.tooltipTitle = BC_TEXT_MENU_PET_ARMORAMOUNT_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_PET_ARMORAMOUNT_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("petArmorReduction");
			info.func = function ()
				BlinkCrits_SetShow("petArmorReduction");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["block"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_CHANCE_TO_BLOCK;
			info.tooltipTitle = BC_TEXT_CHANCE_TO_BLOCK_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_CHANCE_TO_BLOCK_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("block");
			info.func = function ()
				BlinkCrits_SetShow("block");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["dodge"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_CHANCE_TO_DODGE;
			info.tooltipTitle = BC_TEXT_CHANCE_TO_DODGE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_CHANCE_TO_DODGE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("dodge");
			info.func = function ()
				BlinkCrits_SetShow("dodge");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["parry"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_CHANCE_TO_PARRY;
			info.tooltipTitle = BC_TEXT_CHANCE_TO_PARRY_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_CHANCE_TO_PARRY_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("parry");
			info.func = function ()
				BlinkCrits_SetShow("parry");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["resilience"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_RESILIENCE;
			info.tooltipTitle = BC_TEXT_MENU_RESILIENCE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_RESILIENCE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("resilience");
			info.func = function ()
				BlinkCrits_SetShow("resilience");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- melee class
		if ( values["attackpower"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_MELEE_ATTACK_POWER;
			info.tooltipTitle = BC_TEXT_MENU_MELEE_ATTACK_POWER_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_MELEE_ATTACK_POWER_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("attackpower");
			info.func = function ()
				BlinkCrits_SetShow("attackpower");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- ranged class
		if ( values["attackpowerranged"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_RANGED_ATTACK_POWER;
			info.tooltipTitle = BC_TEXT_MENU_RANGED_ATTACK_POWER_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_RANGED_ATTACK_POWER_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("attackpowerranged");
			info.func = function ()
				BlinkCrits_SetShow("attackpowerranged");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- melee class
		if ( values["ratingmelee"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_MELEE_HIT_RATING;
			info.tooltipTitle = BC_TEXT_MENU_MELEE_HIT_RATING_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_MELEE_HIT_RATING_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("ratingmelee");
			info.func = function ()
				BlinkCrits_SetShow("ratingmelee");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- ranged class
		if ( values["ratingranged"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_RANGED_HIT_RATING;
			info.tooltipTitle = BC_TEXT_MENU_RANGED_HIT_RATING_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_RANGED_HIT_RATING_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("ratingranged");
			info.func = function ()
				BlinkCrits_SetShow("ratingranged");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["ratingspell"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_SPELL_HIT_RATING;
			info.tooltipTitle = BC_TEXT_MENU_SPELL_HIT_RATING_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_SPELL_HIT_RATING_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("ratingspell");
			info.func = function ()
				BlinkCrits_SetShow("ratingspell");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["hastemelee"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_HASTE_RATING_MELEE;
			info.tooltipTitle = BC_TEXT_MENU_HASTE_RATING_MELEE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_HASTE_RATING_MELEE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("hastemelee");
			info.func = function ()
				BlinkCrits_SetShow("hastemelee");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["hasteranged"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_HASTE_RATING_RANGED;
			info.tooltipTitle = BC_TEXT_MENU_HASTE_RATING_RANGED_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_HASTE_RATING_RANGED_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("hasteranged");
			info.func = function ()
				BlinkCrits_SetShow("hasteranged");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["hastespell"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_HASTE_RATING_SPELL;
			info.tooltipTitle = BC_TEXT_MENU_HASTE_RATING_SPELL_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_HASTE_RATING_SPELL_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("hastespell");
			info.func = function ()
				BlinkCrits_SetShow("hastespell");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- caster class
		if ( values["spellbonusdamage"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_BONUS_DAMAGE;
			info.tooltipTitle = BC_TEXT_MENU_BONUS_DAMAGE_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_BONUS_DAMAGE_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("spellbonusdamage");
			info.func = function ()
				BlinkCrits_SetShow("spellbonusdamage");
			end
			UIDropDownMenu_AddButton(info);
		end

		-- healer class
		if ( values["spellbonushealing"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_BONUS_HEALING;
			info.tooltipTitle = BC_TEXT_MENU_BONUS_HEALING_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_BONUS_HEALING_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("spellbonushealing");
			info.func = function ()
				BlinkCrits_SetShow("spellbonushealing");
			end
			UIDropDownMenu_AddButton(info);
		end

		if ( values["spellpenetration"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_SPELL_PENETRATION;
			info.tooltipTitle = BC_TEXT_MENU_SPELL_PENETRATION_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_SPELL_PENETRATION_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("spellpenetration");
			info.func = function ()
				BlinkCrits_SetShow("spellpenetration");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["manaregen"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_MANA_REGEN;
			info.tooltipTitle = BC_TEXT_MENU_MANA_REGEN_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_MANA_REGEN_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("manaregen");
			info.func = function ()
				BlinkCrits_SetShow("manaregen");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["honor"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_HONOR;
			info.tooltipTitle = BC_TEXT_MENU_HONOR_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_HONOR_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("honor");
			info.func = function ()
				BlinkCrits_SetShow("honor");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["experience"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_XP;
			info.tooltipTitle = BC_TEXT_MENU_XP_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_XP_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("experience");
			info.func = function ()
				BlinkCrits_SetShow("experience");
			end
			UIDropDownMenu_AddButton(info);
		end
		if ( values["reputation"] ) then
			info = UIDropDownMenu_CreateInfo();
			info.text = BC_TEXT_MENU_REPUTATION;
			info.tooltipTitle = BC_TEXT_MENU_REPUTATION_TOOLTIP_TITLE;
			info.tooltipText = BC_TEXT_MENU_REPUTATION_TOOLTIP_DESC;
			info.checked = BlinkCrits_IsChecked("reputation");
			info.func = function ()
				BlinkCrits_SetShow("reputation");
			end
			UIDropDownMenu_AddButton(info);
		end
	end ,"MENU");
	ToggleDropDownMenu(1, nil, BlinkCritDropDown, "cursor");
end

function BlinkCrits_SetShow(show)
	if ( not show ) then
		show = "autocrit";
	end
	if ( not BlinkCritsDB ) then
		BlinkCritsDB = {
			major = major,
			show = "autocrit"
		};
	end
	if ( showStat[show] ) then
		BlinkCritsDB.show = show;
	else
		BlinkCritsDB.show = "autocrit";
	end
	BlinkCrits_OnEvent();
end

function BlinkCrits_IsChecked(show)
	if ( not show ) then
		return nil;
	end
	if ( not showStat[show] and show~="autocrit" ) then
		return nil;
	end
	if ( show == "autocrit" and not BlinkCritsDB ) then
		BlinkCritsDB = {
			major = major,
			show = "autocrit"
		};
		return 1;
	end
	if ( BlinkCritsDB.show == show ) then
		return 1;
	end
	return nil;
end

-- from DurabilityCount ADDON
function DurabilityCount_GetDurability()
	local curr, max, cost, broken = 0, 0, 0, 0;
	for itemid = 1,18 do
		tooltip:ClearLines()

		tooltip:SetInventoryItem("player", itemid);
		if not tooltip:IsOwned(UIParent) then
			tooltip:SetOwner(UIParent, "ANCHOR_NONE")
		end
		local hasItem, hasCooldown, repairCost = tooltip:SetInventoryItem("player", itemid);
		if ( hasItem ) then
			cost = cost + repairCost;
			for i = 1, tooltip:NumLines(), 1 do
				--local _, _, sMin, sMax = string.find(getglobal("BCTooltipTextLeft" .. i):GetText() or "", BC_TEXT_DURABILITY_TEMPLATE);
				local _, _, sMin, sMax = string.find(tooltip.left[i]:GetText() or "", BC_TEXT_DURABILITY_TEMPLATE);

				if ( sMin and sMax ) then
					curr = curr + tonumber(sMin);
					max = max + tonumber(sMax);

					if ( tonumber(sMin) == 0 ) then
						broken = broken + 1;
					end

					break;
				end				
			end
		end
	end
	return curr, max, cost, broken;
end