--dofile("scripts/bg_decals.lua");
--dofile("scripts/bg_partsys.lua");

CURRENT_TABLE_BG = nil;
CURRENT_TABLE_BG_INDEX = {};
EDITOR_WIDGET_C_BG = 36;
ADD_NAME_BG = false;
Decals = {};
Bipeds = {};

function 	HideEditorBG()

end
function 	ShowEditorBG()

end
function	SetupBGBackgrounds()
	Backgrounds = {};
	local FileList = SearchDir("./backgrounds/*.jpg");
	local index = 1;
	for i= 0, FileList:GetFileC()-1, 1 do
		local name = FileList:GetFileName(i);
		Backgrounds[index] = {};
		Backgrounds[index].Name =	name;
		Backgrounds[index].Sprite =	name;
		index = index + 1
	end	
	local FileList = SearchDir("./backgrounds/*.png");
	for i= 0, FileList:GetFileC()-1, 1 do
		local name = FileList:GetFileName(i);
		Backgrounds[index] = {};
		Backgrounds[index].Name =	name;
		Backgrounds[index].Sprite =	name;
		index = index + 1
	end	
	local FileList = SearchDir("./img/*.jpg");
	for i= 0, FileList:GetFileC()-1, 1 do
		local name = FileList:GetFileName(i);
		Backgrounds[index] = {};
		Backgrounds[index].Name =	name;
		Backgrounds[index].Sprite =	name;
		index = index + 1
	end	
end

function	AddObjectSetBG(Name, Set, FirstIndex)
	local TableSet = dofile("info/"..Name..".lua");
	if not TableSet then return end;
	local Count = table.getn(TableSet);
	local FinalIndex = FirstIndex;
	for i=1, Count, 1 do
		FinalIndex = FirstIndex + i;
		Set[FinalIndex] = TableSet[i];
		Set[FinalIndex].Name = Name.."["..Set[FinalIndex].Name.."]";
		print("Added new element : " .. Set[FinalIndex].Name);
	end
	return FinalIndex;
end

--------------------------------------------------
-- Environment editor stuff

function RefreshBGEditor()
	local Panel = GetUIMan():GetPanel('editor_panel');
	local Widget
	
	-- fix those damned scroll buttons
	Widget = Panel:GetWidgetByName("ArrowUp")
	if Widget then
		Widget:SetReleaseLMBEvent("BgEditScrollEnvUp()")
	end
	Widget = Panel:GetWidgetByName("ArrowDown")
	if Widget then
		Widget:SetReleaseLMBEvent("BgEditScrollEnvDown()")
	end
	Widget = Panel:GetWidgetByName("Exit")
	if Widget then
		Widget:SetReleaseLMBEvent("CloseEditorBG()")
	end
	
	for i=1, EDITOR_WIDGET_C_BG, 1 do
		local Widget = Panel:GetWidgetByName("Entry"..i);
		if (Widget) then
			local Obj = CURRENT_TABLE_BG[i + CURRENT_TABLE_BG_INDEX[CURRENT_ACTION]];
			print("Checking out object " .. i + CURRENT_TABLE_BG_INDEX[CURRENT_ACTION]);
			if (Obj ) then
				print("Valid object " .. Obj.Name);
				if (not Obj.Sprite) then Obj.Sprite = Obj.Name; end
				Widget:SetOffImage(Obj.Sprite);
				Widget:SetReleaseLMBEvent("CloseEditorBG(); GetBGEditor():"..CURRENT_ACTION.."('"..Obj.Name.."')");
				local	RECT_SIZE = Vec2(0.1, 0.1)
				Widget:FitToRect(RECT_SIZE);
				if (ADD_NAME_BG) then
					Widget:SetText(Obj.Name);
				else
					Widget:SetText("");
				end
				Widget:SetIsVisible(true);
			else
				Widget:SetIsVisible(false);
			end
		end
	end
end

function BgEditScrollEnvUp()
	CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] = CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] + 6;
	if (CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] > table.getn(CURRENT_TABLE_BG) - EDITOR_WIDGET_C_BG) then
		CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] = table.getn(CURRENT_TABLE_BG) - EDITOR_WIDGET_C_BG;
	end
	RefreshBGEditor()
end

function BgEditScrollEnvDown()
	CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] = CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] - 6;
	if (CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] < 0) then
		CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] = 0;
	end
	RefreshBGEditor()
end

function OpenEditorBG()
	print(CURRENT_TABLE_BG_INDEX[CURRENT_ACTION]);
	if (CURRENT_TABLE_BG_INDEX[CURRENT_ACTION]==nil) then
		CURRENT_TABLE_BG_INDEX[CURRENT_ACTION] = 0;
		print("Resetting Counter!");
	end
	HideEditorBG();
	local p = GetUIMan():GetPanel('editor_panel');
	if (p) then 
		p:SetIsVisible(true);
		GetUIMan():PushPanelToFront(p);
		RefreshBGEditor();
	end
end

function CloseEditorBG()
	GetUIMan():GetPanel('editor_panel'):SetIsVisible(false);
	--GetUIMan():GetPanel('editor'):SetIsVisible(false);
end


function BuildSetBG(Name, Name2)
	Environment = {};
	local EnvCount = 0;
	EnvCount = AddObjectSetBG(Name, Environment, EnvCount) + EnvCount;
	if (Name2) then
		EnvCount = AddObjectSetBG(Name2, Environment, EnvCount) + EnvCount;
	end
end

function	EditBackgroundBG()
	SetupBGBackgrounds();
	CURRENT_TABLE_BG = Backgrounds;
	CURRENT_ACTION = "SetBackgroundTile";
	ADD_NAME_BG = false;
	OpenEditorBG()
end


function	EditDecalBG()
	ADD_NAME_BG = false;
	Sprites = {};
	Decals = {};
	dofile("decals/sprites2.lua");
	local Count = table.getn(Sprites);
	for i=1, Count, 1 do
		Decals[i] = {};
		Decals[i].Name = Sprites[i].Name;
		Decals[i].Sprite = Sprites[i].Name;
	end
	CURRENT_TABLE_BG = Decals;
	CURRENT_ACTION = "AddDecal";
	ADD_NAME_BG = false;
	OpenEditorBG()
end

function	EditPartBG()
	PartSys = {};
	local FileList = SearchDir("./effects/*.p2d");
	local index = 1;
	for i= 0, FileList:GetFileC()-1, 1 do
		local name = FileList:GetFileName(i);
		PartSys[index] = {};
		PartSys[index].Name =	name;
		PartSys[index].Sprite =	name;
		index = index + 1
	end	

	CURRENT_TABLE_BG = PartSys;
	CURRENT_ACTION = "AddPartSys";
	ADD_NAME_BG = true;
	OpenEditorBG()
end


function EditBipedBG()
	Bipeds = {};
	local FileList = SearchDir("./bganim/*.sbp");
	local index = 1;
	for i= 0, FileList:GetFileC()-1, 1 do
		local name = FileList:GetBaseFileName(i);
		Bipeds[index] = {};
		Bipeds[index].Name =	name;
		Bipeds[index].Sprite =	name;
		print("Found entity " .. name);
		index = index + 1
	end	

	CURRENT_TABLE_BG = Bipeds;
	CURRENT_ACTION = "AddBiped";
	ADD_NAME_BG = true;
	OpenEditorBG()

end


function OnStartBGEditor()
	-- load the central toolbar?
--	ShowEditorBG();	
end

function OnStopBGEditor()
	-- close any potential open ones
	CloseEditorBG()
end

function SaveBackgroundFile()
	local Filename = GetScript():GetSaveFile("lua", "Save BG File");
	if string.len(Filename) > 3 then
		GetBG():Save(Filename);
	end
end

function LoadBackgroundFile()
	local Filename = GetScript():GetOpenFile("lua", "Load BG File");
	if string.len(Filename) > 3 then
		GetBG():Load(Filename);
	end
end

function LoadBGImg()
	local Filename = GetScript():GetOpenFile("lua", "Load BG File");
	if string.len(Filename) > 3 then
		GetBG():SetBackground(Filename);
	end
end

function SaveEditorBG()
	local Filename = GetScript():GetSaveFile("lua", "Save BG File");
	if string.len(Filename) > 3 then
		GetBG():SaveLevel(Filename);
	end
end

function LoadEditorBG()
	local Filename = GetScript():GetOpenFile("lua", "Load BG File");
	if string.len(Filename) > 3 then
		GetBG:Load(Filename);
	end
end



function SaveGroveFile()
	local Filename = GetScript():GetSaveFile("lua", "Save BG File");
	if string.len(Filename) > 3 then
		GetGlade():Save(Filename);
	end
end

function LoadGroveFile()
	local Filename = GetScript():GetOpenFile("lua", "Load BG File");
	if string.len(Filename) > 3 then
		GetGlade():Load(Filename);
	end
end