-- Author      : Peder
-- Create Date : 5/21/2022 2:36:11 PM

iStableMaster = iStableMaster or {}

function BeastSquadsManager_OnLoad(self)
	BeastSquadsManagerFrame.Loaded = false;

	self:SetTitle("Beast Squad Manager")
	--SquadNameEditBox:SetText("My Squad Name");
	TemporarySquadSlotInstantiater(self)
	self.caller = {Pet1 = {CurrentSlot = 0, NewSquad = "NewSquad"}, index = 0};
	--print("self is: "..self:GetName());
	BeastSquadsManagerFrame.Loaded = true;
	BeastSquadsManagerFrame:Hide();


	SquadNameEditBox:HookScript("OnEditFocusLost",function() 
		--iStableMaster:Print("Debug:: ".."EditBox Lost focus");

		iStableMaster:RenameSquad(); 
	end);



end

function BeastSquadsManager_OnShow(caller)
	--TemporarySquadSlotInstantiater(BeastSquadsManagerFrame);
	--iStableMaster:Print(caller);
	--iStableMaster:Print(caller["Pet"..1].CurrentSlot);
	if(BeastSquadsManagerFrame.Loaded == false) then 
		return;
	end
	hooksecurefunc(iStableMaster,"RenameSquad", function()  
		--iStableMaster:Print("Debug:: ".."Hooked on to iStableMaster:RenameSquad()"..(tostring(BeastSquadsManagerFrame.caller) or "nil"));
		iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller); 
	end );
	hooksecurefunc(iStableMaster,"ApplyQueuedSquadChanges", function() iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller) end );
	hooksecurefunc(iStableMaster,"EditSquad", function() 
		iStableMaster:RevertSquadName(BeastSquadsManagerFrame.caller.index); -- this line is here to prevent name change when just changing squad by right clicking in the scroll list
		iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller); 
	end);
	hooksecurefunc(iStableMaster,"RevertSquadName", function() iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller) end );
	hooksecurefunc(iStableMaster,"CloseSquadIconSelector", function() iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller) end );
	hooksecurefunc(iStableMaster,"RevertIconChangeToCurrentIcon", function() iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller) end );
	hooksecurefunc(iStableMaster,"InitExistingValuesToSquadQueueChanges", function() iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller) end );
	iStableMaster:UpdateBeastSquadManagerVisibleState(BeastSquadsManagerFrame.caller);
	--local centerInfo = BSMgrInnerFrame.PetFramesContainer.backdropInfo;
	--print(centerInfo.bgFile);
	--BSMgrInnerFrame.backdropInfo.bgFile = "";
	--print(centerInfo.bgFile);
	--BSMgrInnerFrame.PetFramesContainer.backdropInfo.bgFile = centerInfo;
	--BSMgrInnerFrame:
	--if(caller.Pet1.NewSquad == "NewSquad") then
	-- DEPRECATED
	--SquadNameEditBox:SetText(SquadChangesQueueTable.Name);
	--	for i=1,6 do
	--		if iStableMaster.ScrollFrameArrElem["ScrollElementChild"..i]["PetInfo"]["icon"] ~= nil then
	--		BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetTexture(iStableMaster.ScrollFrameArrElem["ScrollElementChild"..i]["PetInfo"]["icon"]);
	--			--BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetMask(
	--		else
	--		--BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetTexture("Interface\\SPELLBOOK\\UI-Spellbook-SpellBackground.blp");
	--		BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetAtlas("ui-hud-actionbar-iconframe-slot");
	--		end
	--	end
	--else
	--iStableMaster:Print("DEBUG:: - SquadChangesQueueTable.Name = "..(SquadChangesQueueTable.Name or "nil"), "Caller:: SquadNameEditBox:SetText()");
	SquadNameEditBox:SetText(SquadChangesQueueTable.Name);
	for i=1,6 do
		if caller["Pet"..i] ~= nil then
			BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetTexture(iStableMaster.ScrollFrameArrElem["ScrollElementChild"..caller["Pet"..i].CurrentSlot]["PetInfo"]["icon"]);
		else
			--BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetTexture("Interface\\SPELLBOOK\\UI-Spellbook-SpellBackground.blp");
			--BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetTexture("Interface\\HUD\\UI-ActionBar.blp");
			BeastSquadsManagerFrame.PetFrames["frame"..i].IconFrame.icon:SetAtlas("ui-hud-actionbar-iconframe-slot");
		end
	end
	--end
	BeastSquadsManagerFrame:SetPortraitToAsset("Interface\\Icons\\"..SquadChangesQueueTable.Icon)
end

--TemporarySquadSlotInstantiater(BeastSquadsManagerFrame);
function TemporarySquadSlotInstantiater(self)
	self.PetFrames = {frame1,frame2,frame3,frame4,frame5,frame6}
	for i=1,5 do 
		self.PetFrames["frame"..i] = CreateFrame("Frame","SquadPetFrame"..i,self,"PetSlotInSquadTemplate");
		self.PetFrames["frame"..i]:SetPoint("TOPLEFT",BSMgrInnerFrame.PetFramesContainer.ActivePensLabel,"LEFT",-96+64*(i-1),-8); --115
		self.PetFrames["frame"..i]:SetSize(64,64);
		self.PetFrames["frame"..i]:SetAlpha(1);	

	end
	self.PetFrames["frame"..6] = CreateFrame("Frame","SquadPetFrame"..6,self,"PetSlotInSquadTemplate");
	self.PetFrames["frame"..6]:SetSize(64,64);	
	self.PetFrames["frame"..6]:ClearAllPoints();
	self.PetFrames["frame"..6]:SetPoint("TOP",BSMgrInnerFrame.PetFramesContainer.AnimalCompanionLabel,"CENTER",0,-8);
	self.PetFrames["frame"..6]:SetAlpha(1);

end

function BeastSquadsManager_SaveAndClose() -- handled from StablesCollection.lua


end

function BeastSquadsManager_DisbandSquad(caller)
	
	
	iStableMaster:ManagerDisbandSquad(caller.index)

end

function BeastSquadsManager_UndoNameChange()


end

function BeastSquadsManager_EditIcon()


end
function BeastSquadsManager_UndoIconChange()


end
function BeastSquadsManager_UndoPetChange(index)


end

do
	local nextFrame = 1;
	local tick = 1;
	function BeastSquadsManagerFrame_Animator_OnUpdate(self,frameslist,speed)

		-- establish a tickrate
		-- establish how many ticks per cycle and or cycles per second
		-- iterate over a list of frames each tick, such that the cycle contains all frames
		-- then wrap the list at end of index to start over, using Modulation.
		-- This function should take a list of frames, a "self" which is the target of the animation texture, a speed,
	    -- and then be placed on the texture's on update script.
		local tickrate = speed*100; -- a timer to check for ticks per second could be added here to get a proper speed modifier.

		if math.fmod(tick,tickrate) == 0 then


			--			if math.fmod(nextFrame,#frameslist) == 0 then 
			if nextFrame == #frameslist then 
				nextFrame = 1;
			end

			self:SetTexture(frameslist[nextFrame]);

			nextFrame = nextFrame + 1;
		else

		end
		tick = tick + 1;

	end
	
	

end
