-- ******************************** Constants *******************************
local TP_MeleeStats_ID                              = "MeleeStats"
local TP_MeleeStats_Version                         = "1.0.3"
local TP_MeleeStats_CRef1                           = "|cffffffff"
local TP_MeleeStats_CRef2                           = "|cff00ff00"
local TP_MeleeStats_CRef3                           = "|cff999999"
local TP_MeleeStats_CRef4                           = "|cffffcc00"
local TP_MeleeStats_CRef5                           = "|cff0082b2"
local TP_MeleeStats_Shaman_Flurry1                 = 16257
local TP_MeleeStats_Shaman_Flurry2                 = 16277
local TP_MeleeStats_Shaman_Flurry3                 = 16278
local TP_MeleeStats_Shaman_Windfury                = 8515
local TP_MeleeStats_Shaman_Bloodlust               = 2825
local TP_MeleeStats_Shaman_Devastation             = 29178
local TP_MeleeStats_Troll_Beserking                = 26297
local TP_MeleeStats_Paladin_JudgementsOfThePure1   = 53655
local TP_MeleeStats_Paladin_JudgementsOfThePure2   = 53656
local TP_MeleeStats_Paladin_JudgementsOfThePure3   = 53657
local TP_MeleeStats_Druid_Stampede1                = 81016
local TP_MeleeStats_Druid_Stampede2                = 81017
local TP_MeleeStats_Warrior_Flurry1                = 12966
local TP_MeleeStats_Warrior_Flurry2                = 12967
local TP_MeleeStats_Warrior_Flurry3                = 12968
local TP_MeleeStats_Shaman_Heroism                 = 32182
-- ******************************** Variables *******************************
local TP_MeleeStats_Haste
local TP_MeleeStats_AP
local TP_MeleeStats_Crit, TP_MeleeStats_Report_Index
local _G = getfenv(0);
local mxAP, mxCrit, mxHaste, miAP, miCrit, miHaste
local intL
local waitTable = {};
local waitFrame = nil;
local TP_MeleeStats_Auras={
	{1,"Shaman - Flurry","[10/20/30% Haste]"},
	{2,"Shaman - Windfury Totem","[10% Haste]"},
	{3,"Shaman - Bloodlust","[30% Haste]"},
	{4,"Shaman - Devastation","[9% Crit]"},
    {5,"Shaman - Heroism","[30% Haste]"},
	{6,"Troll - Beserking","[20% Haste]"},
	{7,"Paladin - Judgements of the Pure","[3/6/9% Haste]"},
    {8,"Druid - Stampede Rank:1","[15% Haste]"},
	{9,"Druid - Stampede Rank:2","[30% Haste]"},
    {10,"Warrior - Flurry","[8/16/25% Haste]"}
}
-- **************************************************************************
-- NAME : TitanPanelCasterStatsButton_CreateWhisperWindow()
-- DESC : Create Report to Whisper frame
-- **************************************************************************
function TitanPanelMeleeStatsButton_CreateWhisperWindow()
	local theFrame = CreateFrame("Frame", "TPMS_WhisperFrame", TitanPanelMeleeStatsButton);
	theFrame:ClearAllPoints();
	theFrame:SetPoint("LEFT", TitanPanelMeleeStatsButton);
	theFrame:SetHeight(72);
	theFrame:SetWidth(200);
    theFrame:SetPoint("LEFT", 0, -48);
    --Set edge/background image/size
	theFrame:SetBackdrop({
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 16,
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16,
		insets = {left = 3, right = 5, top = 3, bottom = 5},
	})
	theFrame:SetBackdropBorderColor(0.5, 0.5, 0.0);
	theFrame:SetBackdropColor(24/255, 24/255, 24/255);
	theFrame:EnableMouse(true);
	theFrame:SetMovable(false);
    --Create text
	theFrame.Text=theFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
	theFrame.Text:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 10, -6);
	theFrame.Text:SetTextColor(1.0, 1.0, 1.0);
	theFrame.Text:SetText("Enter name to whisper:");
    --Create editbox
	theFrame.Whisper=CreateFrame("EditBox", nil, theFrame, "InputBoxTemplate");
	theFrame.Whisper:SetWidth(178);
	theFrame.Whisper:SetHeight(13);
	theFrame.Whisper:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 14, -21);
	theFrame.Whisper:SetAutoFocus(false);
    --Create Report button
	theFrame.YesButton=CreateFrame("Button", nil, theFrame, "OptionsButtonTemplate")
	theFrame.YesButton:SetWidth(72);
	theFrame.YesButton:SetHeight(24);
	theFrame.YesButton:SetPoint("BOTTOMRIGHT", theFrame, "BOTTOMRIGHT", -10, 8);
	theFrame.YesButton:SetScript("OnClick", function() 
        TPMS_Whisper(theFrame.Whisper:GetText()); 
        theFrame:Hide(); 
    end);
	theFrame.YesButton:SetText("Report");
    --Create Cancel button
	theFrame.NoButton=CreateFrame("Button", nil, theFrame, "OptionsButtonTemplate");
	theFrame.NoButton:SetWidth(72);
	theFrame.NoButton:SetHeight(24);
	theFrame.NoButton:SetPoint("BOTTOMLEFT", theFrame, "BOTTOMLEFT", 10, 8);
	theFrame.NoButton:SetScript("OnClick", function() theFrame:Hide(); end);
	theFrame.NoButton:SetText("Cancel");
    theFrame:Hide();
end
-- **************************************************************************
-- NAME : TitanPanelCasterStatsButton_CreateOptionsFrame()
-- DESC : Create Options form
-- **************************************************************************
function TitanPanelMeleeStatsButton_CreateOptionsFrame()
	local theFrame = CreateFrame("Frame", "TPMS_OptionsFrame", UIParent);
	theFrame:ClearAllPoints();
	theFrame:SetHeight(226);
	theFrame:SetWidth(380);
    theFrame:SetPoint("CENTER");

	theFrame:SetBackdrop({
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 16,
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", edgeSize = 16,
		insets = {left = 3, right = 5, top = 3, bottom = 5},
	})
	theFrame:SetBackdropBorderColor(0.5, 0.5, 0.0);
	theFrame:SetBackdropColor(24/255, 24/255, 24/255);
	theFrame:EnableMouse(true);
	theFrame:SetMovable(true);

	theFrame.Text=theFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
	theFrame.Text:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 10, -2);
	theFrame.Text:SetTextColor(1.0, 1.0, 1.0);
    theFrame.Text:SetWidth(370);
	theFrame.Text:SetHeight(20);
    theFrame.Text:SetJustifyH("LEFT");
	theFrame.Text:SetText("Ignore the following Auras/Procs when calculating statistics:");
    local oID = 1
    for i=1, 10 do
        theFrame.CheckOpt=CreateFrame("CheckButton", "Chk_Opt"..oID, theFrame);
        theFrame.CheckOpt:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 10, -12 - (16 * oID));
        theFrame.CheckOpt:SetWidth(16);
        theFrame.CheckOpt:SetHeight(16);
        theFrame.CheckOpt:SetChecked(TitanGetVar(TP_MeleeStats_ID, "Chk_Opt"..oID));
        theFrame.CheckOpt:SetScript("OnClick",function(this)
        if this:GetChecked() then
            TitanSetVar(TP_MeleeStats_ID, this:GetName(), true);
        else
            TitanSetVar(TP_MeleeStats_ID, this:GetName(), false);
        end
        end)
        theFrame.CheckOpt:SetNormalTexture("Interface\\Buttons\\UI-CheckBox-Up");
        theFrame.CheckOpt:SetPushedTexture("Interface\\Buttons\\UI-CheckBox-Down");
        theFrame.CheckOpt:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check");
        theFrame.CheckOpt:SetHighlightTexture("Interface\\Buttons\\UI-CheckBox-Highlight");
        
        theFrame.Text=theFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
        theFrame.Text:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 30, -12 - (16 * oID))
        theFrame.Text:SetTextColor(1.0, 1.0, 1.0);
        theFrame.Text:SetWidth(270);
        theFrame.Text:SetHeight(16);
        theFrame.Text:SetJustifyH("LEFT");
        theFrame.Text:SetText(TP_MeleeStats_Auras[oID][2]);
        
        theFrame.Text2=theFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
        theFrame.Text2:SetPoint("TOPLEFT", theFrame, "TOPLEFT", 255, -12 - (16 * oID))
        theFrame.Text2:SetTextColor(0.1, 0.9, 0.1);
        theFrame.Text2:SetWidth(144);
        theFrame.Text2:SetHeight(16);
        theFrame.Text2:SetJustifyH("LEFT");
        theFrame.Text2:SetText(TP_MeleeStats_Auras[oID][3]);
        oID = oID + 1
    end
    
    --Create Close button
	theFrame.YesButton=CreateFrame("Button", nil, theFrame, "OptionsButtonTemplate")
	theFrame.YesButton:SetWidth(72);
	theFrame.YesButton:SetHeight(24);
	theFrame.YesButton:SetPoint("BOTTOMRIGHT", theFrame, "BOTTOMRIGHT", -10, 8);
	theFrame.YesButton:SetScript("OnClick", function() 
        theFrame:Hide(); 
    end);
	theFrame.YesButton:SetText("Close");
    theFrame:Hide();
end

function TPMS_Whisper(target)
    if target == "" then return; end
    AP_DoReport("WHISPER", target, TP_MeleeStats_Report_Index);
end
-- **************************************************************************
-- NAME : TitanPanelMeleeStatsButton_OnLoad()
-- DESC : Registers the plugin upon it loading
-- **************************************************************************
function TitanPanelMeleeStatsButton_OnLoad(self)
    intL = 0;
    self.registry = {
	id = TP_MeleeStats_ID,
        category = "Combat",
	version = TP_MeleeStats_Version,
        menuText = "Melee Statistics",
	buttonTextFunction = "TitanPanelMeleeStatsButton_GetButtonText",
        tooltipTitle = TP_MeleeStats_CRef4.."Melee Statistics",		
        tooltipTextFunction = "TitanPanelMeleeStatsButton_GetTooltipText",
        icon = "Interface\\AddOns\\TitanMeleeStats\\TitanMeleeStats",
        iconButtonWidth = 16,        
        iconWidth = 16,        
		controlVariables = {
			ShowIcon = true,
			ShowLabelText = true,
			ShowRegularText = false,
			ShowColoredText = true,
			DisplayOnRightSide = false,
		},
		savedVariables = {
               		ShowIcon = 1,
               		ShowLabelText = 1,
               		ShowColoredText = 1,
                    MaxAP = 0,
                    MaxCrit = 0,
                    MaxHaste = 0,
                    MinAP = 99999,
                    MinCrit = 99999,
                    MinHaste = 99999,
                    Chk_Opt1 = false,
                    Chk_Opt2 = false,
                    Chk_Opt3 = false,
                    Chk_Opt4 = false,
                    Chk_Opt5 = false,
                    Chk_Opt6 = false,
                    Chk_Opt7 = false,
                    Chk_Opt8 = false,
                    Chk_Opt9 = false,
                    Chk_Opt10 = false,
		}
	};
	TitanPanelMeleeStatsButton_CreateWhisperWindow();
end

-- **************************************************************************
-- NAME : TitanPanelMeleeStatsButton_OnClick(self, button)
-- DESC : Opens Report Dialog
-- VARS : button = value of action
-- **************************************************************************
function TitanPanelMeleeStatsButton_OnClick(self, button)
	if button == "LeftButton" then
        TPMS_OptionsFrame:Show();
	end
end

-- **************************************************************************
-- NAME : TitanPanelMeleeStatsButton_GetTooltipText()
-- DESC : Display tooltip text
-- **************************************************************************
function TitanPanelMeleeStatsButton_GetTooltipText()
    local dLine1, dLine2, dLine3, dLine4
	dLine1 = "\n"..TP_MeleeStats_CRef1.."Attack Power\n "..TP_MeleeStats_CRef4.."Minimun: \t"..TP_MeleeStats_CRef1..miAP.."\n "..
        TP_MeleeStats_CRef4.."Current: \t"..TP_MeleeStats_CRef1..TP_MeleeStats_AP.."\n "..
        TP_MeleeStats_CRef4.."Maximum: \t"..TP_MeleeStats_CRef1..mxAP.."\n\n"
    dLine2 = TP_MeleeStats_CRef1.."Melee Crit\n "..TP_MeleeStats_CRef4.."Minimun: \t"..TP_MeleeStats_CRef1..miCrit.."%\n "..
        TP_MeleeStats_CRef4.."Current: \t"..TP_MeleeStats_CRef1..TP_MeleeStats_Crit.."%\n "..
        TP_MeleeStats_CRef4.."Maximum: \t"..TP_MeleeStats_CRef1..mxCrit.."%\n\n"
    dLine3 = TP_MeleeStats_CRef1.."Melee Haste\n "..TP_MeleeStats_CRef4.."Minimun: \t"..TP_MeleeStats_CRef1..miHaste.."%\n "..
        TP_MeleeStats_CRef4.."Current: \t"..TP_MeleeStats_CRef1..TP_MeleeStats_Haste.."%\n "..
        TP_MeleeStats_CRef4.."Maximum: \t"..TP_MeleeStats_CRef1..mxHaste.."%\n"
        
    dLine4 = TitanUtils_GetGreenText("Hint: Left click to open Config.");
    return dLine1..dLine2..dLine3..dLine4;
end

-- **************************************************************************
-- NAME : TitanPanelMeleeStatsButton_GetButtonText(id)
-- DESC : Calculate statistics then display data on button
-- DESC : Updates Min/Max saved statistics, if needed
-- VARS : id = button ID
-- **************************************************************************
function TitanPanelMeleeStatsButton_GetButtonText(id)

    local hasteRichText, critRichText, spellRichText, a1, a2
    TP_MeleeStats_Haste = round(GetCombatRatingBonus(18), 2);
    TP_MeleeStats_AP = TPMS_GetAP();
    --a1 = GetCombatRatingBonus(9);
    --a2 = GetCritChanceFromAgility("player");
    --TP_MeleeStats_Crit = round(a1 + a2, 2)
    TP_MeleeStats_Crit = round(GetCritChance(), 2)
    a1, a2 = TPMS_Aura_Check();
    TP_MeleeStats_Haste = TP_MeleeStats_Haste + a1
    TP_MeleeStats_Crit = TP_MeleeStats_Crit + a2
    if intL == 0 then
        mxAP = TitanGetVar(TP_MeleeStats_ID, "MaxAP");
        mxCrit = TitanGetVar(TP_MeleeStats_ID, "MaxCrit");
        mxHaste = TitanGetVar(TP_MeleeStats_ID, "MaxHaste");
        miAP = TitanGetVar(TP_MeleeStats_ID, "MinAP");
        miCrit = TitanGetVar(TP_MeleeStats_ID, "MinCrit");
        miHaste = TitanGetVar(TP_MeleeStats_ID, "MinHaste");
        intL = 1;
    end
    if TP_MeleeStats_AP > mxAP then 
        TitanSetVar(TP_MeleeStats_ID, "MaxAP", TP_MeleeStats_AP);
        mxAP = TP_MeleeStats_AP
    end
    if TP_MeleeStats_Crit > mxCrit then 
        TitanSetVar(TP_MeleeStats_ID, "MaxCrit", TP_MeleeStats_Crit);
        mxCrit = TP_MeleeStats_Crit
    end
    if TP_MeleeStats_Haste > mxHaste then 
        TitanSetVar(TP_MeleeStats_ID, "MaxHaste", TP_MeleeStats_Haste);
        mxHaste = TP_MeleeStats_Haste
    end
    if TP_MeleeStats_AP < miAP then 
        TitanSetVar(TP_MeleeStats_ID, "MinAP", TP_MeleeStats_AP);
        miAP = TP_MeleeStats_AP
    end
    if TP_MeleeStats_Crit < miCrit then 
        TitanSetVar(TP_MeleeStats_ID, "MinCrit", TP_MeleeStats_Crit);
        miCrit = TP_MeleeStats_Crit
    end
    if TP_MeleeStats_Haste < miHaste then 
        TitanSetVar(TP_MeleeStats_ID, "MinHaste", TP_MeleeStats_Haste);
        miHaste = TP_MeleeStats_Haste
    end
	if ( TitanGetVar(TP_MeleeStats_ID, "ShowColoredText") ) then     
		spellRichText = TPMS_GetColouredText_SP(TP_MeleeStats_AP);
        critRichText = TPMS_GetColouredText_Crit(TP_MeleeStats_Crit);
        hasteRichText = TPMS_GetColouredText_Haste(TP_MeleeStats_Haste);
	else
		spellRichText = TitanUtils_GetHighlightText(TP_MeleeStats_AP);
        critRichText = TitanUtils_GetHighlightText(TP_MeleeStats_Crit.."%");
        hasteRichText = TitanUtils_GetHighlightText(TP_MeleeStats_Haste.."%");
	end
    return "Stats: ", TP_MeleeStats_CRef1.."AP: "..spellRichText..TP_MeleeStats_CRef1.." C: "..critRichText..TP_MeleeStats_CRef1.." H: "..hasteRichText
end

-- **************************************************************************
-- NAME : TitanPanelMeleeStatsButton_OnShow()
-- DESC : Update data/display & start Update Timer
-- **************************************************************************
function TitanPanelMeleeStatsButton_OnShow()
    TitanPanelButton_UpdateButton(TP_MeleeStats_ID);
    TPMS_Wait(2, TPMS_Update);
    TitanPanelMeleeStatsButton_CreateOptionsFrame();
end
-- **************************************************************************
-- NAME : TPMS_Update()
-- DESC : Update data/display & restart Update Timer
-- **************************************************************************
function TPMS_Update()
    if TitanPanelMeleeStatsButton:IsShown() then
        TitanPanelButton_UpdateButton(TP_MeleeStats_ID);
        TPMS_Wait(1, TPMS_Update);
    end
end

-- **************************************************************************
-- NAME : TPMS_GetColouredText_SP() : TPMS_GetColouredText_Crit() : TPMS_GetColouredText_Haste()
-- DESC : Get text colour from saved min, max, current values
-- **************************************************************************
function TPMS_GetColouredText_SP(Value)
    local t1, rtnText, tVal1, tVal2, tVal3
    t1 = ((mxAP - miAP) / 10);
    tVal1 = (miAP + (t1 * 1));
    tVal2 = (miAP + (t1 * 3.5));
    tVal3 = (miAP + (t1 * 7));
    if Value <= tVal1 then
        rtnText = TitanUtils_GetRedText(Value);
    elseif Value > tVal1 and Value <= tVal2 then
        rtnText = TP_MeleeStats_CRef5..Value.._G["FONT_COLOR_CODE_CLOSE"];
    elseif Value > tVal2 and Value <= tVal3 then
	    rtnText = TitanUtils_GetGoldText(Value);
    else
        rtnText = TitanUtils_GetGreenText(Value);
    end
    return rtnText;
end
function TPMS_GetColouredText_Crit(Value)
    local t1, rtnText, tVal1, tVal2, tVal3
    t1 = ((mxCrit - miCrit) / 10);
    tVal1 = (miCrit + (t1 * 1));
    tVal2 = (miCrit + (t1 * 3.5));
    tVal3 = (miCrit + (t1 * 7));
    if Value <= tVal1 then
        rtnText = TitanUtils_GetRedText(Value.."%");
    elseif Value > tVal1 and Value <= tVal2 then
        rtnText = TP_MeleeStats_CRef5..Value.."%".._G["FONT_COLOR_CODE_CLOSE"];
    elseif Value > tVal2 and Value <= tVal3 then
	    rtnText = TitanUtils_GetGoldText(Value.."%");
    else
        rtnText = TitanUtils_GetGreenText(Value.."%");
    end
    return rtnText;
end
function TPMS_GetColouredText_Haste(Value)
    local t1, rtnText, tVal1, tVal2, tVal3
    t1 = ((mxHaste - miHaste) / 10);
    tVal1 = (miHaste + (t1 * 1));
    tVal2 = (miHaste + (t1 * 3.5));
    tVal3 = (miHaste + (t1 * 7));
    if Value <= tVal1 then
        rtnText = TitanUtils_GetRedText(Value.."%");
    elseif Value > tVal1 and Value <= tVal2 then
        rtnText = TP_MeleeStats_CRef5..Value.."%".._G["FONT_COLOR_CODE_CLOSE"];
    elseif Value > tVal2 and Value <= tVal3 then
	    rtnText = TitanUtils_GetGoldText(Value.."%");
    else
        rtnText = TitanUtils_GetGreenText(Value.."%");
    end
    return rtnText;
end

function round(n, precision)
  local m = 10^(precision or 0)
  return floor(m*n + 0.5)/m
end
-- **************************************************************************
-- NAME : TitanPanelRightClickMenu_PrepareMeleeStatsMenu()
-- DESC : Display rightclick menu options
-- **************************************************************************
function TitanPanelRightClickMenu_PrepareMeleeStatsMenu()
	local info
	if _G["UIDROPDOWNMENU_MENU_LEVEL"] == 2 then
		if _G["UIDROPDOWNMENU_MENU_VALUE"] == "Report" then
            info = {};
			info.text = "Battleground";
			info.func = function() AP_DoReport("BATTLEGROUND", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
            info = {};
			info.text = "Guild";
			info.func = function() AP_DoReport("GUILD", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
			info = {};
			info.text = "Officer";
			info.func = function() AP_DoReport("OFFICER", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);

			info = {};
			info.text = "Party";
			info.func = function() AP_DoReport("PARTY", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
		    
			info = {};
			info.text = "Raid";
			info.func = function() AP_DoReport("RAID", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
			info = {};
			info.text = "Say";
			info.func = function() AP_DoReport("SAY", "Nil", 1); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            if UnitName("target") and UnitIsPlayer("target") then
                info = {};
			    info.text = "Whisper Target";
			    info.func = function() AP_DoReport("WHISPER_TARGET", "Nil", 1); end
			    UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            end
            info = {};
			info.text = "Whisper...";
			info.func = function() 
                TP_MeleeStats_Report_Index = 1
                TPMS_WhisperFrame:Show();
                TitanPanelRightClickMenu_Close();
            end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
		elseif _G["UIDROPDOWNMENU_MENU_VALUE"] == "Report2" then
            info = {};
			info.text = "Battleground";
			info.func = function() AP_DoReport("BATTLEGROUND", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
            info = {};
			info.text = "Guild";
			info.func = function() AP_DoReport("GUILD", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
			info = {};
			info.text = "Officer";
			info.func = function() AP_DoReport("OFFICER", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);

			info = {};
			info.text = "Party";
			info.func = function() AP_DoReport("PARTY", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
		    
			info = {};
			info.text = "Raid";
			info.func = function() AP_DoReport("RAID", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            
			info = {};
			info.text = "Say";
			info.func = function() AP_DoReport("SAY", "Nil", 2); end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            if UnitName("target") and UnitIsPlayer("target") then
                info = {};
			    info.text = "Whisper Target";
			    info.func = function() AP_DoReport("WHISPER_TARGET", "Nil", 2); end
			    UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
            end
            info = {};
			info.text = "Whisper...";
			info.func = function() 
                TP_MeleeStats_Report_Index = 2
                TPMS_WhisperFrame:Show();
                TitanPanelRightClickMenu_Close();
            end
			UIDropDownMenu_AddButton(info, _G["UIDROPDOWNMENU_MENU_LEVEL"]);
		end
		return
	end
	 info = {};
     
     TitanPanelRightClickMenu_AddTitle("Melee Statistics");
	 info.text = "Report Current Stats to...";
	 info.value = "Report"
	 info.hasArrow = 1;
	 UIDropDownMenu_AddButton(info);
     
	 info.text = "Report Maximum Stats to...";
	 info.value = "Report2"
	 info.hasArrow = 1;
	 UIDropDownMenu_AddButton(info);
     TitanPanelRightClickMenu_AddSpacer();
     
     TitanPanelRightClickMenu_AddToggleIcon(TP_MeleeStats_ID);
     TitanPanelRightClickMenu_AddToggleLabelText(TP_MeleeStats_ID);
     TitanPanelRightClickMenu_AddToggleColoredText(TP_MeleeStats_ID);
     
     TitanPanelRightClickMenu_AddSpacer();
     info = {};
     info.text = "Reset Stats";
     info.func = function() TPMS_ResetStats()
     DEFAULT_CHAT_FRAME:AddMessage("Statistics Reset.");
     end
     UIDropDownMenu_AddButton(info);
     info = {};
     info.text = "Config";
     info.func = function() TPMS_OptionsFrame:Show();
     end
     UIDropDownMenu_AddButton(info);
     
     TitanPanelRightClickMenu_AddSpacer();
     TitanPanelRightClickMenu_AddCommand("Hide", TP_MeleeStats_ID, TITAN_PANEL_MENU_FUNC_HIDE);
end
function TPMS_ResetStats()
    intL = 0;
    TitanPanelMeleeStatsButton:SetText("AP: ... C: ... H: ...");
    TitanSetVar(TP_MeleeStats_ID, "MaxAP", 0);
    TitanSetVar(TP_MeleeStats_ID, "MinAP", 9999);
    TitanSetVar(TP_MeleeStats_ID, "MaxCrit", 0);
    TitanSetVar(TP_MeleeStats_ID, "MinCrit", 9999);
    TitanSetVar(TP_MeleeStats_ID, "MaxHaste", 0);
    TitanSetVar(TP_MeleeStats_ID, "MinHaste", 9999);
    mxAP = 0
    mxCrit = 0
    mxHaste = 0
    miAP = 9999
    miCrit = 9999
    miHaste = 9999
    TitanPanelButton_UpdateButton(TP_MeleeStats_ID);
end
function TPMS_GetAP()
    local base, posBuff, negBuff = UnitAttackPower("player");
    local effective = base + posBuff + negBuff;
    return effective;
end
function AP_DoReport(rType, rTarget, tIndex)
    local tmpStr1

    if tIndex == 1 then
        tmpStr1 = "Current AP: "..TP_MeleeStats_AP.." Crit: "..TP_MeleeStats_Crit.."% Haste: "..TP_MeleeStats_Haste.."%"
    elseif tIndex == 2 then
        tmpStr1 = "Maximum AP: "..mxAP.." Crit: "..mxCrit.."% Haste: "..mxHaste.."%"
    end
    if rType == "SAY" then
        SendChatMessage(tmpStr1, "SAY", GetDefaultLanguage("player"));
    elseif rType == "GUILD" then
        SendChatMessage(tmpStr1, "GUILD", GetDefaultLanguage("player"));
    elseif rType == "OFFICER" then
        SendChatMessage(tmpStr1, "OFFICER", GetDefaultLanguage("player"));
    elseif rType == "WHISPER" then
        SendChatMessage(tmpStr1, "WHISPER", GetDefaultLanguage("player"), rTarget);
    elseif rType == "WHISPER_TARGET" then
        SendChatMessage(tmpStr1, "WHISPER", GetDefaultLanguage("player"), UnitName("target"));
    elseif rType == "PARTY" then
        SendChatMessage(tmpStr1, "PARTY", GetDefaultLanguage("player"));
    elseif rType == "RAID" then
        SendChatMessage(tmpStr1, "RAID", GetDefaultLanguage("player"));
    elseif rType == "BATTLEGROUND" then
        SendChatMessage(tmpStr1, "BATTLEGROUND", GetDefaultLanguage("player"));
    end
end
function TPMS_Aura_Check()
    local i, rtnHaste, rtnCrit
    rtnHaste = 0
    rtnCrit = 0
    for i = 1, 50 do
	    local name, rank, icon ,_ ,_ ,_ ,_ ,_ ,_ ,_ ,sID = UnitBuff("player", i); 
	    if (name == nil) then break; end
        if sID == TP_MeleeStats_Shaman_Flurry1 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt1") then rtnHaste = rtnHaste + 10; end
        if sID == TP_MeleeStats_Shaman_Flurry2 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt1") then rtnHaste = rtnHaste + 20; end
        if sID == TP_MeleeStats_Shaman_Flurry3 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt1") then rtnHaste = rtnHaste + 30; end
        if sID == TP_MeleeStats_Shaman_Windfury and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt2") then rtnHaste = rtnHaste + 10; end
        if sID == TP_MeleeStats_Shaman_Bloodlust and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt3") then rtnHaste = rtnHaste + 30; end
        if sID == TP_MeleeStats_Shaman_Devastation and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt4") then rtnCrit = rtnCrit + 9; end
        if sID == TP_MeleeStats_Shaman_Heroism and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt5") then rtnHaste = rtnHaste + 30; end
        if sID == TP_MeleeStats_Troll_Beserking and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt6") then rtnHaste = rtnHaste + 20; end
        if sID == TP_MeleeStats_Paladin_JudgementsOfThePure1 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt7") then rtnHaste = rtnHaste + 3; end
        if sID == TP_MeleeStats_Paladin_JudgementsOfThePure2 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt7") then rtnHaste = rtnHaste + 6; end
        if sID == TP_MeleeStats_Paladin_JudgementsOfThePure3 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt7") then rtnHaste = rtnHaste + 9; end
        if sID == TP_MeleeStats_Druid_Stampede1 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt8") then rtnHaste = rtnHaste + 15; end
        if sID == TP_MeleeStats_Druid_Stampede2 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt9") then rtnHaste = rtnHaste + 30; end
        if sID == TP_MeleeStats_Warrior_Flurry1 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt10") then rtnHaste = rtnHaste + 8; end
        if sID == TP_MeleeStats_Warrior_Flurry2 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt10") then rtnHaste = rtnHaste + 16; end
        if sID == TP_MeleeStats_Warrior_Flurry3 and not TitanGetVar(TP_MeleeStats_ID, "Chk_Opt10") then rtnHaste = rtnHaste + 25; end
    end
    return rtnHaste, rtnCrit;
end

function TPMS_Wait(delay, func, ...)
  if(type(delay)~="number" or type(func)~="function") then
    return false;
  end
  if(waitFrame == nil) then
    waitFrame = CreateFrame("Frame","WaitFrame", UIParent);
    waitFrame:SetScript("onUpdate",function (self,elapse)
      local count = #waitTable;
      local i = 1;
      while(i<=count) do
        local waitRecord = tremove(waitTable,i);
        local d = tremove(waitRecord,1);
        local f = tremove(waitRecord,1);
        local p = tremove(waitRecord,1);
        if(d>elapse) then
          tinsert(waitTable,i,{d-elapse,f,p});
          i = i + 1;
        else
          count = count - 1;
          f(unpack(p));
        end
      end
    end);
  end
  tinsert(waitTable,{delay,func,{...}});
  return true;
end