local addonName, core = ...;
core.cpgn = {};


local function ApplySpellVisualOnUnit(frame, SpellVisual, oneShot)
	if (SpellVisual ~= 0) then
		for i = 1, #SpellVisual do
			frame:ApplySpellVisualKit(SpellVisual[i], oneShot);
		end
	end
end
core.cpgn.ApplySpellVisualOnUnit = ApplySpellVisualOnUnit;



local function ChangeBgFight(bg)
	_G["FightScene"]:SetBackdrop({bgFile = bg,
					 edgeFile = "", 
					 tile = false, edgeSize = 16,
					 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
end
core.cpgn.ChangeBgFight = ChangeBgFight;



StaticPopupDialogs["BetaVersion"] = {
	text = "WARNING! This is a beta version, there may be bugs and non/bad-translate word. Don't hesitate to report bugs on the Discord!",
	button1 = core.L["Next"],
	OnAccept = function()
		PlaySound("31756", "sfx");
		DBProfile.firstVisit = false;
		_G["WelcomeFrame"]:Hide();
	end,
	hideOnEscape = false,
	whileDead = true,
}
local function CreateFrameFirstVisit()
	local WelcomeFrame = CreateFrame("Frame", "WelcomeFrame", core.Tab.t1.content, nil);
	WelcomeFrame:SetSize(800,450);
	WelcomeFrame:SetPoint("CENTER", core.Tab.t1.content, "CENTER");
	WelcomeFrame:SetFrameLevel(11);
	WelcomeFrame:EnableMouse(true)
	WelcomeFrame:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bg_firstVisit",
								 edgeFile = "", 
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	WelcomeFrame.titlebg = CreateFrame("Frame", nil, WelcomeFrame);
	WelcomeFrame.titlebg:SetSize(256,64);
	WelcomeFrame.titlebg:SetPoint("TOP", WelcomeFrame, "TOP", 0, 5);
	WelcomeFrame.titlebg:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/annoucement/bg_title"});
	WelcomeFrame.titlebg.text = WelcomeFrame.titlebg:CreateFontString(nil, "OVERLAY");
	if GetLocale() == "ruRU" then
		WelcomeFrame.titlebg.text:SetFont("Interface\\AddOns\\WoWChallenger\\fonts\\FRIZQT__CYR.TTF", 16, "OUTLINE");
	else
		WelcomeFrame.titlebg.text:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE");
	end
	WelcomeFrame.titlebg.text:SetText(core.L["Campaigns"]);
	WelcomeFrame.titlebg.text:SetPoint("CENTER", WelcomeFrame.titlebg, "CENTER", 0, -5);
	
	WelcomeFrame.Model1 = CreateFrame("CinematicModel", "WelcomeFrameModel1", WelcomeFrame, nil);
	WelcomeFrame.Model1:SetSize(800,450);
	WelcomeFrame.Model1:SetPoint("CENTER", WelcomeFrame, "CENTER");
	WelcomeFrame.Model1:SetDisplayInfo(46471);
	WelcomeFrame.Model1:SetAnimation(1260);
	WelcomeFrame.Model1:SetRotation(-1.3);
	WelcomeFrame.Model1:SetPosition(-2.10, 0.7, 0.28);
	WelcomeFrame.Model2 = CreateFrame("CinematicModel", "WelcomeFrameModel3", WelcomeFrame, nil);
	WelcomeFrame.Model2:SetSize(800,450);
	WelcomeFrame.Model2:SetPoint("CENTER", WelcomeFrame, "CENTER");
	WelcomeFrame.Model2:SetDisplayInfo(53852);
	WelcomeFrame.Model2:SetAnimation(26);
	WelcomeFrame.Model2:SetRotation(-1.3);
	WelcomeFrame.Model2:SetPosition(-5.25, 7, -0.25);
	WelcomeFrame.Model3 = CreateFrame("CinematicModel", "WelcomeFrameModel4", WelcomeFrame, nil);
	WelcomeFrame.Model3:SetSize(800,450);
	WelcomeFrame.Model3:SetPoint("CENTER", WelcomeFrame, "CENTER");
	WelcomeFrame.Model3:SetDisplayInfo(80339);
	WelcomeFrame.Model3:SetAnimation(26);
	WelcomeFrame.Model3:SetRotation(-1.3);
	WelcomeFrame.Model3:SetPosition(-3, 4.5, 0.6);
	WelcomeFrame.Model4 = CreateFrame("CinematicModel", "WelcomeFrameModel2", WelcomeFrame, nil);
	WelcomeFrame.Model4:SetSize(800,450);
	WelcomeFrame.Model4:SetPoint("CENTER", WelcomeFrame, "CENTER");
	WelcomeFrame.Model4:SetDisplayInfo(45716);
	WelcomeFrame.Model4:SetAnimation(26);
	WelcomeFrame.Model4:SetRotation(-1.3);
	WelcomeFrame.Model4:SetPosition(-4, 7.75, 1.5);
	
	WelcomeFrame.textContent = CreateFrame("Frame", nil, WelcomeFrame);
	WelcomeFrame.textContent:SetSize(250,280);
	WelcomeFrame.textContent:SetPoint("CENTER", WelcomeFrame, "CENTER");
	WelcomeFrame.textContent:SetBackdrop({bgFile = "",
								edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
								tile = false, edgeSize = 16, 
								insets = { left = 0, right = 0, top = 0, bottom = 0 }});
								
	WelcomeFrame.textContent.bg = WelcomeFrame.textContent:CreateTexture(nil, "BACKGROUND");
	WelcomeFrame.textContent.bg:SetAllPoints(true);
	WelcomeFrame.textContent.bg:SetPoint("TOPLEFT", WelcomeFrame.textContent, "TOPLEFT", 5, -5);	
	WelcomeFrame.textContent.bg:SetPoint("BOTTOMRIGHT", WelcomeFrame.textContent, "BOTTOMRIGHT", -5, 5);	
	WelcomeFrame.textContent.bg:SetColorTexture(0, 0, 0, 0.55);

	
	WelcomeFrame.textContent.ScrollFrame = CreateFrame("ScrollFrame",  nil, WelcomeFrame.textContent, "UIPanelScrollFrameTemplate");
	WelcomeFrame.textContent.ScrollFrame:SetPoint("TOPLEFT", WelcomeFrame.textContent, "TOPLEFT", 0, 0);
	WelcomeFrame.textContent.ScrollFrame:SetPoint("BOTTOMRIGHT", WelcomeFrame.textContent, "BOTTOMRIGHT");
	WelcomeFrame.textContent.ScrollFrame:SetClipsChildren(true);
	WelcomeFrame.textContent.ScrollFrame.child = CreateFrame("Frame", nil, WelcomeFrame.textContent.ScrollFrame);
	WelcomeFrame.textContent.ScrollFrame.child:SetSize(235, 275);
	WelcomeFrame.textContent.ScrollFrame:SetScrollChild(WelcomeFrame.textContent.ScrollFrame.child);
	WelcomeFrame.textContent.ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel);
	
	WelcomeFrame.textContent.desc = WelcomeFrame.textContent.ScrollFrame.child:CreateFontString(nil, "OVERLAY");
	WelcomeFrame.textContent.desc:SetFontObject("GameFontNormal");
	WelcomeFrame.textContent.desc:SetText(core.L["DescCampaigns"]);
	WelcomeFrame.textContent.desc:SetJustifyH("LEFT");
	WelcomeFrame.textContent.desc:SetPoint("TOPLEFT", WelcomeFrame.textContent.ScrollFrame.child, "TOPLEFT", 10, -10);
	WelcomeFrame.textContent.desc:SetSize(235, 0);
	
	WelcomeFrame.Closebtn = CreateFrame("Button", "WelcomeFrameBtnClose", WelcomeFrame, "UIPanelButtonTemplate");
	WelcomeFrame.Closebtn:SetSize(75, 25);
	WelcomeFrame.Closebtn:SetText(core.L["Next"]);
	WelcomeFrame.Closebtn:SetPoint("BOTTOM", WelcomeFrame, "BOTTOM", 0, 30);
	WelcomeFrame.Closebtn:SetScript("OnClick", function(self)
		StaticPopup_Show("BetaVersion");
	end);
	if (DBProfile.firstVisit == false) then
		WelcomeFrame:Hide();
	else
		PlaySound("77003", "sfx");
		WelcomeFrame:Show();
	end
end
core.cpgn.CreateFrameFirstVisit = CreateFrameFirstVisit;

local function CreateFrameTuto()
	DBProfile.showTuto = false;
	
	----- FRAME 1 -----
	local TutoFrame1 = CreateFrame("Frame", "TutoFrame1", _G["FightScene"], nil);
	TutoFrame1:SetSize(250,200);
	TutoFrame1:SetPoint("LEFT", _G["FightScene"], "LEFT", 50, 0);
	TutoFrame1:SetFrameLevel(20);
	TutoFrame1:EnableMouse(true)
	TutoFrame1:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bgTip",
								 edgeFile = "", 
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	TutoFrame1.bgBlack = TutoFrame1:CreateTexture(nil, "BACKGROUND");
	TutoFrame1.bgBlack:SetAllPoints(true);
	TutoFrame1.bgBlack:SetPoint("TOPLEFT", TutoFrame1, "TOPLEFT", 6, -6);	
	TutoFrame1.bgBlack:SetPoint("BOTTOMRIGHT", TutoFrame1, "BOTTOMRIGHT", -10, 4);
	TutoFrame1.bgBlack:SetColorTexture(0, 0, 0, 0.8);
	
	TutoFrame1.text = TutoFrame1:CreateFontString(nil, "OVERLAY");
	TutoFrame1.text:SetFontObject("GameFontNormal");
	TutoFrame1.text:SetText(core.L["TutoFrame1Text"]);
	TutoFrame1.text:SetJustifyH("LEFT");
	TutoFrame1.text:SetPoint("TOPLEFT", TutoFrame1, "TOPLEFT", 20, -20);
	TutoFrame1.text:SetSize(TutoFrame1:GetWidth()-40, 0);
	
	TutoFrame1.btnNext = CreateFrame("Button", "TutoFrame1BtnClose", TutoFrame1, "UIPanelButtonTemplate");
	TutoFrame1.btnNext:SetSize(75, 25);
	TutoFrame1.btnNext:SetText(core.L["Next"]);
	TutoFrame1.btnNext:SetPoint("BOTTOM", TutoFrame1, "BOTTOM", 0, 20);
	TutoFrame1.btnNext:SetScript("OnClick", function(self)
		PlaySound("83", "sfx", false);
		self:GetParent():Hide();
		_G["TutoFrame2"]:Show();
	end);
	
	
	
	----- FRAME 2 -----
	local TutoFrame2 = CreateFrame("Frame", "TutoFrame2", _G["FightScene"], nil);
	TutoFrame2:SetSize(225,125	);
	TutoFrame2:SetPoint("TOP", _G["FightScene"], "TOP", 15, -60);
	TutoFrame2:SetFrameLevel(20);
	TutoFrame2:EnableMouse(true)
	TutoFrame2:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bgTip",
								 edgeFile = "",
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	TutoFrame2.Arrow = CreateFrame("Frame", "TutoFrame2Arrow", TutoFrame2, nil);
	TutoFrame2.Arrow:SetSize(75,35);
	TutoFrame2.Arrow:SetPoint("TOP", TutoFrame2, "TOP", 0, 32);
	TutoFrame2.Arrow:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/ArrowTip_up"});

	TutoFrame2.bgBlack = TutoFrame2:CreateTexture(nil, "BACKGROUND");
	TutoFrame2.bgBlack:SetAllPoints(true);
	TutoFrame2.bgBlack:SetPoint("TOPLEFT", TutoFrame2, "TOPLEFT", 6, -4);
	TutoFrame2.bgBlack:SetPoint("BOTTOMRIGHT", TutoFrame2, "BOTTOMRIGHT", -9, 4);
	TutoFrame2.bgBlack:SetColorTexture(0, 0, 0, 0.8);
	
	TutoFrame2.text = TutoFrame2:CreateFontString(nil, "OVERLAY");
	TutoFrame2.text:SetFontObject("GameFontNormal");
	TutoFrame2.text:SetText(core.L["TutoFrame2Text"]);
	TutoFrame2.text:SetJustifyH("LEFT");
	TutoFrame2.text:SetPoint("TOPLEFT", TutoFrame2, "TOPLEFT", 20, -20);
	TutoFrame2.text:SetSize(TutoFrame2:GetWidth()-40, 0);
	
	TutoFrame2.btnNext = CreateFrame("Button", "TutoFrame2BtnClose", TutoFrame2, "UIPanelButtonTemplate");
	TutoFrame2.btnNext:SetSize(75, 25);
	TutoFrame2.btnNext:SetText(core.L["Next"]);
	TutoFrame2.btnNext:SetPoint("BOTTOM", TutoFrame2, "BOTTOM", 0, 20);
	TutoFrame2.btnNext:SetScript("OnClick", function(self)
		PlaySound("83", "sfx", false);
		self:GetParent():Hide();
		_G["TutoFrame3"]:Show();
	end);
	TutoFrame2:Hide();
	
	
	
	----- FRAME 3 -----
	local TutoFrame3 = CreateFrame("Frame", "TutoFrame3", _G["FightScene"], nil);
	TutoFrame3:SetSize(225,135);
	TutoFrame3:SetPoint("RIGHT", _G["FightScene"], "RIGHT", -110, -20);
	TutoFrame3:SetFrameLevel(20);
	TutoFrame3:EnableMouse(true)
	TutoFrame3:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bgTip",
								 edgeFile = "",
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	TutoFrame3.Arrow = CreateFrame("Frame", "TutoFrame3Arrow", TutoFrame3, nil);
	TutoFrame3.Arrow:SetSize(75,35);
	TutoFrame3.Arrow:SetPoint("BOTTOM", TutoFrame3, "BOTTOM", 0, -33);
	TutoFrame3.Arrow:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/ArrowTip_down"});

	TutoFrame3.bgBlack = TutoFrame3:CreateTexture(nil, "BACKGROUND");
	TutoFrame3.bgBlack:SetAllPoints(true);
	TutoFrame3.bgBlack:SetPoint("TOPLEFT", TutoFrame3, "TOPLEFT", 6, -4);
	TutoFrame3.bgBlack:SetPoint("BOTTOMRIGHT", TutoFrame3, "BOTTOMRIGHT", -9, 4);
	TutoFrame3.bgBlack:SetColorTexture(0, 0, 0, 0.8);
	
	TutoFrame3.text = TutoFrame3:CreateFontString(nil, "OVERLAY");
	TutoFrame3.text:SetFontObject("GameFontNormal");
	TutoFrame3.text:SetText(core.L["TutoFrame3Text"]);
	TutoFrame3.text:SetJustifyH("LEFT");
	TutoFrame3.text:SetPoint("TOPLEFT", TutoFrame3, "TOPLEFT", 20, -20);
	TutoFrame3.text:SetSize(TutoFrame3:GetWidth()-40, 0);
	
	TutoFrame3.btnNext = CreateFrame("Button", "TutoFrame3BtnClose", TutoFrame3, "UIPanelButtonTemplate");
	TutoFrame3.btnNext:SetSize(75, 25);
	TutoFrame3.btnNext:SetText(core.L["Next"]);
	TutoFrame3.btnNext:SetPoint("BOTTOM", TutoFrame3, "BOTTOM", 0, 20);
	TutoFrame3.btnNext:SetScript("OnClick", function(self)
		PlaySound("83", "sfx", false);
		self:GetParent():Hide();
		_G["TutoFrame4"]:Show();
	end);
	TutoFrame3:Hide();
	
	
	
	----- FRAME 4 -----
	local TutoFrame4 = CreateFrame("Frame", "TutoFrame4", _G["FightScene"], nil);
	TutoFrame4:SetSize(225,250);
	TutoFrame4:SetPoint("RIGHT", _G["FightScene"], "RIGHT", -110, -45);
	TutoFrame4:SetFrameLevel(20);
	TutoFrame4:EnableMouse(true)
	TutoFrame4:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bgTip",
								 edgeFile = "",
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	TutoFrame4.Arrow = CreateFrame("Frame", "TutoFrame4Arrow", TutoFrame4, nil);
	TutoFrame4.Arrow:SetSize(75,35);
	TutoFrame4.Arrow:SetPoint("BOTTOM", TutoFrame4, "BOTTOM", 0, -33);
	TutoFrame4.Arrow:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/ArrowTip_down"});

	TutoFrame4.bgBlack = TutoFrame4:CreateTexture(nil, "BACKGROUND");
	TutoFrame4.bgBlack:SetAllPoints(true);
	TutoFrame4.bgBlack:SetPoint("TOPLEFT", TutoFrame4, "TOPLEFT", 6, -4);
	TutoFrame4.bgBlack:SetPoint("BOTTOMRIGHT", TutoFrame4, "BOTTOMRIGHT", -9, 4);
	TutoFrame4.bgBlack:SetColorTexture(0, 0, 0, 0.8);
	
	TutoFrame4.text = TutoFrame4:CreateFontString(nil, "OVERLAY");
	TutoFrame4.text:SetFontObject("GameFontNormal");
	TutoFrame4.text:SetText(core.L["TutoFrame4Text"]);
	TutoFrame4.text:SetJustifyH("LEFT");
	TutoFrame4.text:SetPoint("TOPLEFT", TutoFrame4, "TOPLEFT", 20, -20);
	TutoFrame4.text:SetSize(TutoFrame4:GetWidth()-40, 0);
	
	TutoFrame4.btnNext = CreateFrame("Button", "TutoFrame4BtnClose", TutoFrame4, "UIPanelButtonTemplate");
	TutoFrame4.btnNext:SetSize(75, 25);
	TutoFrame4.btnNext:SetText(core.L["Next"]);
	TutoFrame4.btnNext:SetPoint("BOTTOM", TutoFrame4, "BOTTOM", 0, 20);
	TutoFrame4.btnNext:SetScript("OnClick", function(self)
		PlaySound("83", "sfx", false);
		self:GetParent():Hide();
		_G["TutoFrame5"]:Show();
	end);
	TutoFrame4:Hide();
	
	----- FRAME 5 -----
	local TutoFrame5 = CreateFrame("Frame", "TutoFrame5", _G["FightScene"], nil);
	TutoFrame5:SetSize(275,85);
	TutoFrame5:SetPoint("CENTER", _G["FightScene"], "CENTER");
	TutoFrame5:SetFrameLevel(20);
	TutoFrame5:EnableMouse(true)
	TutoFrame5:SetBackdrop({bgFile = "Interface/AddOns/WoWChallenger/images/campaign/bgTip",
								 edgeFile = "",
								 tile = false, edgeSize = 16,
								 insets = { left = 0, right = 3, top = 0, bottom = 0 }});
	TutoFrame5.bgBlack = TutoFrame5:CreateTexture(nil, "BACKGROUND");
	TutoFrame5.bgBlack:SetAllPoints(true);
	TutoFrame5.bgBlack:SetPoint("TOPLEFT", TutoFrame5, "TOPLEFT", 6, -4);
	TutoFrame5.bgBlack:SetPoint("BOTTOMRIGHT", TutoFrame5, "BOTTOMRIGHT", -9, 4);
	TutoFrame5.bgBlack:SetColorTexture(0, 0, 0, 0.8);
	
	TutoFrame5.text = TutoFrame5:CreateFontString(nil, "OVERLAY");
	TutoFrame5.text:SetFontObject("GameFontWhite");
	TutoFrame5.text:SetText(core.L["TutoFrame5Text"]);
	TutoFrame5.text:SetJustifyH("LEFT");
	TutoFrame5.text:SetPoint("TOPLEFT", TutoFrame5, "TOPLEFT", 20, -20);
	TutoFrame5.text:SetSize(TutoFrame5:GetWidth()-40, 0);
	
	TutoFrame5.btnNext = CreateFrame("Button", "TutoFrame5BtnClose", TutoFrame5, "UIPanelButtonTemplate");
	TutoFrame5.btnNext:SetSize(75, 25);
	TutoFrame5.btnNext:SetText(core.L["Close"]);
	TutoFrame5.btnNext:SetPoint("BOTTOM", TutoFrame5, "BOTTOM", 0, 20);
	TutoFrame5.btnNext:SetScript("OnClick", function(self)
		PlaySound("83", "sfx", false);
		self:GetParent():Hide();
	end);
	TutoFrame5:Hide();
end
core.cpgn.CreateFrameTuto = CreateFrameTuto;

local function DamageToPlayer(damage, renverse)
	local savePdv = DBProfile.pdv;
	DBProfile.pdv = DBProfile.pdv - damage;
	if (DBProfile.pdv <= 0) then
		if (savePdv > 0) then
			PlaySound(core.DBPlayer.sound.death, "sfx", false);
			_G["FightScenePlayer"]:SetAnimation(1);
		end
		EffectCurrPlayer = {};
	else
		PlaySound(core.DBPlayer.sound.hit, "sfx", false);
		if (renverse == false) then
			_G["FightScenePlayer"]:SetAnimation(9);
			core.cpgn.WC_wait(0.50, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
		else
			_G["FightScenePlayer"]:SetAnimation(121);
			core.cpgn.WC_wait(1.5, core.cpgn.setWaitAnimation, _G["FightScenePlayer"], core.DBPlayer.animBase, 0);
		end
	end
	core.cpgn.refreshPlayerLifeFrame(_G["FSPlayerLifeText"], false);
end
core.cpgn.DamageToPlayer = DamageToPlayer;



local function DieObjectUnit(frame)
	frame:SetPosition(0, 100, 0)
end
core.cpgn.DieObjectUnit = DieObjectUnit;



local function findInArray(var, array)
	for i = 1, #array do
		if (array[i] == var) then
			return true
		end
	end
	return false;
end
core.cpgn.findInArray = findInArray;



local function ManyPlaySound(sound)
	if (sound ~= 0) then
		for i = 1, #sound do
			PlaySound(sound[i], "sfx", false);
		end
	end
end
core.cpgn.ManyPlaySound = ManyPlaySound;

local function refreshPlayerDamageFrame(frame)
	if (DBProfile.damage < DBProfile.damageMax) then
		frame:SetFontObject("GameFontRedLarge");
	elseif (DBProfile.damage == DBProfile.damageMax) then
		frame:SetFontObject("GameFontHighlightLarge");
	elseif (DBProfile.damage > DBProfile.damageMax) then
		frame:SetFontObject("GameFontGreenLarge");
	end
	frame:SetText(DBProfile.damage);
end
core.cpgn.refreshPlayerDamageFrame = refreshPlayerDamageFrame;


local function refreshPlayerLifeFrame(frame, profile)
	if (DBProfile.pdv < DBProfile.pdvMax) then
		frame:SetFontObject("GameFontRedLarge");
	elseif (DBProfile.pdv == DBProfile.pdvMax) then
		frame:SetFontObject("GameFontHighlightLarge");
	elseif (DBProfile.pdv > DBProfile.pdvMax) then
		frame:SetFontObject("GameFontGreenLarge");
	end
	if (profile == true) then
		frame:SetText(DBProfile.pdv.."/"..DBProfile.pdvMax);
	else
		frame:SetText(DBProfile.pdv);
	end
end
core.cpgn.refreshPlayerLifeFrame = refreshPlayerLifeFrame;



local function refreshPlayerSpellFrameForCD()
	for i = 1, #CdSpellPlayer do
		if (CdSpellPlayer[i] == 0) then
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Hide();
		else
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:SetText(CdSpellPlayer[i]);
		end
	end
end
core.cpgn.refreshPlayerSpellFrameForCD = refreshPlayerSpellFrameForCD;



local function ToggleCanPlayPlayerSpell(playerCanPlay)
	if (playerCanPlay == 0) then
		for i = 1, #core.DBPlayer.spells do
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Show();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Hide();
		end
	else
		for i = 1, #core.DBPlayer.spells do
			_G["FSPlayerSpell"..i.."FrameSpellCD"]:Hide();
			_G["FSPlayerSpell"..i.."FrameSpellCDText"]:Show();
		end
	end
end
core.cpgn.ToggleCanPlayPlayerSpell = ToggleCanPlayPlayerSpell;



local waitTable = {};
local waitFrame = nil;
function WC_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
core.cpgn.WC_wait = WC_wait;