-- Author      : MrExillion
-- Create Date : 6/18/2021 11:18:30 AM

--local _, addonTable = ... -- i think this is wrong and i should use a iStableMaster = iStableMaster or {} here or instead of it.
iStableMaster = iStableMaster or {};
activeStatusColor = "none"
QeuedLayout = "Default";
--local StartQuedLayout = "Default";

function iStableMasterSettingsFrame_SetLayout(layoutName) 
	iStableMasterOptionsDB.Layout = layoutName;
end
function iStableMasterSettingsFrame_GetLayout() 
			iStableMasterOptionsDB = iStableMasterOptionsDB or {}
	return iStableMasterOptionsDB.Layout or "nil";
end
function iStableMasterSettingsFrame_OnLoad(self,...)
	--local table = ...;
	--function self:OnCommit() end
	--function self:OnDefault() end
	--function self:OnRefresh() end
	--local iStableMasterOptionsDB = ... or {};
	
	self.name = "|TInterface/Addons/iStableMaster/iStableMasterLogo-notitle:16:16|t|cff4b705aiStableMaster|r"; -- Should be handled by colour schemes
	
	iStableMasterSettingsFrame = self;

end

	hooksecurefunc("iStableMasterSettingsFrame_OnLoad", function() iStableMaster:AddSettings(iStableMasterSettingsFrame)end);

 function iStableMaster:AddSettings(self)
	local ISMODB = {}
	local iStableMasterOptionsDB = {}
	local category, layout  = Settings.RegisterVerticalLayoutCategory(self.name);
	 --self:SetParent(category);	

	--InterfaceOptions_AddCategory(self);
	iStableMasterSettingsFrame.category = category;
	local subcatFrame = CreateFrame("Frame","TestFrame",self,BackdropTemplateMixin and "BackdropTemplate");--"iStableMasterSettingsTypeDefaultTemplate");
	local subcategory, layout  = Settings.RegisterCanvasLayoutSubcategory(category,self,"Layouts");
	layout:AddAnchorPoint("TOPLEFT", 10, -10);
	layout:AddAnchorPoint("BOTTOMRIGHT", -10, 10);
	--function subcatFrame:OnCommit() end
	--function subcatFrame:OnDefault() end
	--function subcatFrame:OnRefresh() end
--	local subcategory, layout = Settings.RegisterCanvasLayoutSubcategory(category, subcatFrame, "Layouts");
	--local subcategory, layout = Settings.RegisterVerticalLayoutSubcategory(category, "Layouts");
	----layout:AddAnchorPoint("TOPLEFT", 10, -10);
	----layout:AddAnchorPoint("BOTTOMRIGHT", -10, 10);
	----InterfaceOptions_AddCategory(self);
	
	
	--Settings.RegisterAddOnCategory(subcategory);
	--local iStableMasterOptionsDB = {};
--	local variable, name, tooltip = "Layout", "Layout Selector", "Select between Compact and Default Layout";
--	local function GetOptions()
--    local container = Settings.CreateControlTextContainer();
--    container:Add("Default", "Default");
--    container:Add("Compact", "Compact");
--   -- container:Add("Other", "Option C");
--    return container:GetData();
--	end
--	local defaultValue = "Default";
--	local setting = Settings.RegisterProxySetting(subcategory, variable, iStableMasterOptionsDB, Settings.VarType.String, name, defaultValue,iStableMasterSettingsFrame_GetLayout,QeuedLayout,iStableMasterLayout_Commit);
--	Settings.CreateDropDown(subcategory, setting, GetOptions, tooltip);

	
	

--	local subcategory, layout = Settings.RegisterVerticalLayoutSubcategory(category, "Pet Scroll Frame");
--	--layout:AddAnchorPoint("TOPLEFT", 10, -10);
--	--layout:AddAnchorPoint("BOTTOMRIGHT", -10, 10);
--	--InterfaceOptions_AddCategory(self);
	
	
--	Settings.RegisterAddOnCategory(subcategory);
--		--iStableMasterLayout_OnLoad();
--	--local myVariableTable = {} --or iStableMasterOptionsDB; -- Saved in your .toc
--	--local iStableMasterOptionsDB = {};
--	--local iStableMasterOptionsDB = {ScrollList = {ShowActiveSlots = true;}} or iStableMasterOptionsDB;
--	--myVariableTable.ShowActiveSlots = true;
--	local variable, name, tooltip = "ShowActiveSlots", "Show Active Slots", "Show Active Slots if Checked";
--	local setting = Settings.RegisterProxySetting(subcategory, variable, iStableMasterOptionsDB.ScrollList, Settings.VarType.Boolean, name, Settings.Default.True);
--	Settings.CreateCheckBox(subcategory, setting, tooltip);
	
--	--local iStableMasterOptionsDB = {};
--	--myVariableTable.ShowActiveSlots = true;
--	local variable, name, tooltip = "HideEmptyStableSlots", "Show Empty Stable Slots", "Show Empty Stable Slots if Checked";
--	local setting = Settings.RegisterProxySetting(subcategory, variable, iStableMasterOptionsDB.ScrollList, Settings.VarType.Boolean, name, Settings.Default.False);
--	Settings.CreateCheckBox(subcategory, setting, tooltip);

	 Settings.RegisterAddOnCategory(category);
	end

function iStableMasterLayout_OnLoad()

	--print(iStableMasterOptionsDB.Layout);	
	iStableMaster:InitOptionsDB();


	if(iStableMasterSettingsFrame_GetLayout() == "Default") then
		iStableMasterSettingsFrameLayoutSelection.DefaultLayout.CheckButton:SetChecked(true);
		iStableMasterSettingsFrameLayoutSelection.CompactLayout.CheckButton:SetChecked(false);
		QeuedLayout = "Default";
	elseif (iStableMasterSettingsFrame_GetLayout() == "Compact") then

		iStableMasterSettingsFrameLayoutSelection.DefaultLayout.CheckButton:SetChecked(false);
		iStableMasterSettingsFrameLayoutSelection.CompactLayout.CheckButton:SetChecked(true);
		QeuedLayout = "Compact";
	else
		iStableMaster:Print("ERROR: Incorrect Layout Saved in DB, returning to default, value was: "..iStableMasterSettingsFrame_GetLayout());
		iStableMasterSettingsFrameLayoutSelection.DefaultLayout.CheckButton:SetChecked(true);
		iStableMasterSettingsFrameLayoutSelection.CompactLayout.CheckButton:SetChecked(false);
		--iStableMasterSettingsFrame_SetLayout("Default");
		QeuedLayout = "Default";
	end

	OrderSettings() 

end


function iStableMasterLayout_OnShow()
	--StartQuedLayout = QeuedLayout;


end



--hooksecurefunc("HideUIPanel",function()
--if( InterfaceOptionsFrame:IsShown() == false) then

--QeuedLayout = QeuedLayout;
--print("Hiding InterfaceOptionsFrame");
--end
-- end);


function DefaultCheckButton_OnClick(self)
	local value = self:GetChecked();
	self:SetChecked(true);
	if(value) then 
		iStableMasterSettingsFrameLayoutSelection.CompactLayout.CheckButton:SetChecked(false);
	end
	if(self:GetChecked()) then
		QeuedLayout = "Default";
	end
end

function CompactCheckButton_OnClick(self)
	local value = self:GetChecked();	
	self:SetChecked(true);
	if(value) then
		iStableMasterSettingsFrameLayoutSelection.DefaultLayout.CheckButton:SetChecked(false);
	end
	if(self:GetChecked()) then
		QeuedLayout = "Compact";
	end
end





--[[

Template infor for intrinsic frame type data etc.


]]
local templateNamePrefix = "iStableMasterSettingsType";
local templateNameSuffix = "Template";
iStableMasterSettingsTypes = {
	--type{frametype,name,template}
	Default = {frametype="Frame",name="Default",template=templateNamePrefix.."Default"..templateNameSuffix },

	RadioOptions = {frametype="Frame",name="Default",template=templateNamePrefix.."Default"..templateNameSuffix }


}

iStableMasterSettingsList = {
	{name=Layouts,frame=iStableMasterSettingsFrameLayoutSelection},
	{name=FilterSortSystem,frame=iStableMasterSettingsFrameNewTemplatesTest},
	{name=ColourScheme,frame=nil},


}

function OrderSettings() 
	iStableMasterSettingsList[1].frame:ClearAllPoints();
	iStableMasterSettingsList[1].frame:SetPoint("TOPLEFT",iStableMasterSettingsFrame,"TOPLEFT",5,-5);
	--iStableMasterSettingsList[1].frame:SetPoint("BOTTOMLEFT",0,iStableMasterSettingsList[1].frame:GetHeight());
	for i=2,iStableMaster:StringKeyTableLengthI(iStableMasterSettingsList) do
		
		if(iStableMasterSettingsList[i].frame == nil) then
			break;
		end
		
		iStableMasterSettingsList[i].frame:ClearAllPoints();
		iStableMasterSettingsList[i].frame:SetPoint("TOPLEFT",iStableMasterSettingsList[i-1].frame,"BOTTOMLEFT",0,-10);
		--iStableMasterSettingsList[i].frame:SetPoint("BOTTOMLEFT",0,iStableMasterSettingsList[i].frame:GetHeight());
	end

end

function iStableMasterLayout_Commit()
iStableMasterSettingsFrame_SetLayout(QeuedLayout);

ReloadUI();

end
