package gay.pizza.CavesOfJolk

import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.assets.loaders.TextureLoader
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver
import com.badlogic.gdx.audio.Sound
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader
import com.badlogic.gdx.graphics.g3d.Model


class Resources private constructor()
{
	companion object
	{
		@Volatile private var _instance: Resources? = null

		val instance get() = _instance ?: synchronized(this) { _instance ?: Resources().also { _instance = it } }
		val assetManager get() = instance.assetManager
	}

	private val assetManager: AssetManager = AssetManager()

	init
	{
		val resolver = InternalFileHandleResolver()
		assetManager.setLoader(FreeTypeFontGenerator::class.java, FreeTypeFontGeneratorLoader(resolver))
		assetManager.setLoader(BitmapFont::class.java, ".ttf", FreetypeFontLoader(resolver))
	}

		val linearMipped = TextureLoader.TextureParameter().apply {
			minFilter = Texture.TextureFilter.MipMapLinearLinear
			magFilter = Texture.TextureFilter.Linear
			wrapU = Texture.TextureWrap.Repeat
			wrapV = Texture.TextureWrap.Repeat
			genMipMaps = true
		}

	fun loadAssets()
	{
		assetManager.load("colin.png", Texture::class.java)
		assetManager.load("jolkmeup.jpg", Texture::class.java)
		assetManager.loadFont("Comic Sans MS.ttf", 20)
		assetManager.load("suzanne.g3db", Model::class.java)
		assetManager.load("nut.wav", Sound::class.java)
		assetManager.load("cobblestone.png", Texture::class.java, linearMipped)
		assetManager.load("cobblestone_normal.png", Texture::class.java, linearMipped)
		assetManager.load("cobblestone_specular.png", Texture::class.java, linearMipped)
		assetManager.load("rock.g3db", Model::class.java)
		assetManager.load("knux.g3db", Model::class.java)
	}
}

fun AssetManager.loadFont(filePath: String, size: Int)
{
	val fontParam = FreetypeFontLoader.FreeTypeFontLoaderParameter()
	fontParam.fontFileName = filePath
	fontParam.fontParameters.size = size
	this.load(filePath, BitmapFont::class.java, fontParam)
}
