#include "helper.h"
#include "c.h"
#include "ld.h"
#include "runner.h"
#include "target.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"

void build_shader( const char *szName )
{
	filesystem2->MakeDirectory(CUtlString("build/funnygame/metal/shaders"));
	filesystem2->MakeDirectory(CUtlString("build/funnygame/vulkan/shaders"));

	CUtlVector<CUtlString> slang_args = {
		CUtlString("funnyassets/shaders/%s.slang", szName),
		"-g",
		"-target",
		"spirv",
		"-D",
		"__SPIRV__=1",
		"-o",
		CUtlString("build/funnygame/vulkan/shaders/%s.spv", szName),
	};

	runner->Run("slangc", slang_args);
	slang_args[3] = "metal";
	slang_args[5] = "__METAL__=1";
	slang_args[7] = CUtlString("build/funnygame/metal/shaders/%s.spv", szName);
	runner->Run("slangc", slang_args);
	runner->Wait();	
};

DECLARE_BUILD_STAGE(assets)
{
	if (CommandLine()->CheckParam("-noassets"))
		return 0;
	filesystem2->CopyDirectory("build", "tools");

	filesystem2->CopyDirectory(CUtlString("build/funnygame/assets"), "funnyassets/maps");
	filesystem2->CopyDirectory(CUtlString("build/funnygame/assets"), "funnyassets/fonts");
	filesystem2->CopyDirectory(CUtlString("build/funnygame/assets"), "funnyassets/textures");
	filesystem2->CopyDirectory(CUtlString("build/funnygame/assets"), "funnyassets/materials");
	/*
	build_shader("mesh_frag");
	build_shader("mesh_vert");
	build_shader("fgui_rect_frag");
	build_shader("fgui_rect_vert");
	build_shader("fgui_text_frag");
	build_shader("fgui_text_vert");
	*/
	CUtlVector<CUtlString> python_args = {
		"build/tools/makepak64.py",
		CUtlString("build/funnygame/assets"),
		bStaticBuild ? CUtlString("%s/bin/%s.pak", szOutputDir.GetString(), "rtt") : CUtlString("%s/funnygame/%s.pak", szOutputDir.GetString(), "rtt"),
	};
	runner->Run("python3", python_args);
	filesystem2->CopyDirectory(CUtlString("%s/funnygame/core", szOutputDir.GetString()),"build/funnygame/assets");
	/*
	if (Target_t::DefaultTarget().kernel == TARGET_KERNEL_IOS ||Target_t::DefaultTarget().kernel == TARGET_KERNEL_DARWIN)
	{
		python_args = {
			"build/tools/makepak64.py",
			CUtlString("build/funnygame/metal"),
			bStaticBuild ? CUtlString("%s/bin/%s.pak", szOutputDir.GetString(), "metal") : CUtlString("%s/funnygame/%s.pak", szOutputDir.GetString(), "metal"),
		};
		runner->Run("python3", python_args);
	} else {
		filesystem2->CopyDirectory(CUtlString("%s/funnygame", szOutputDir.GetString()),"build/funnygame/vulkan/shaders");
		python_args = {
			"build/tools/makepak64.py",
			CUtlString("build/funnygame/vulkan"),
			bStaticBuild ? CUtlString("%s/bin/%s.pak", szOutputDir.GetString(), "vulkan") : CUtlString("%s/funnygame/%s.pak", szOutputDir.GetString(), "vulkan"),
		};
		runner->Run("python3", python_args);
	}
	*/
	runner->Wait();	

	if (bStaticBuild)
		filesystem2->CopyFile(CUtlString("%s/bin", szOutputDir.GetString()), "funnyassets/default.cfg");
	else
		filesystem2->CopyFile(szOutputDir.GetString(), "funnyassets/default.cfg");
	return 0;
};
