package ru.dbotthepony.kstarbound.defs.tile

import com.google.common.collect.ImmutableList
import ru.dbotthepony.kstarbound.Globals
import ru.dbotthepony.kstarbound.defs.AssetReference
import ru.dbotthepony.kstarbound.defs.IThingWithDescription
import ru.dbotthepony.kstarbound.defs.ThingDescription
import ru.dbotthepony.kstarbound.json.builder.JsonFactory
import ru.dbotthepony.kstarbound.json.builder.JsonFlat
import ru.dbotthepony.kstarbound.json.builder.JsonIgnore

@JsonFactory
data class TileModifierDefinition(
	val modId: Int,
	val modName: String,
	val itemDrop: String? = null,
	val health: Double? = null,
	val requiredHarvestLevel: Int? = null,
	val breaksWithTile: Boolean = true,
	val grass: Boolean = false,
	val tilled: Boolean = false,
	val miningParticle: String? = null,

	val footstepSound: String? = null,
	val miningSounds: ImmutableList<String> = ImmutableList.of(),

	@Deprecated("", replaceWith = ReplaceWith("this.actualDamageTable"))
	val damageTable: AssetReference<TileDamageConfig> = AssetReference(Globals::tileDamage),

	@JsonFlat
	val descriptionData: ThingDescription,

	// meta tiles are treated uniquely by many systems
	// such as players/projectiles unable to break them, etc
	@JsonIgnore
	val isMeta: Boolean = false,

	override val renderTemplate: AssetReference<RenderTemplate>,
	override val renderParameters: RenderParameters
) : IRenderableTile, IThingWithDescription by descriptionData {
	init {
		require(modId > 0) { "Invalid tile modifier ID $modId" }
	}

	val actualDamageTable: TileDamageConfig by lazy {
		val dmg = damageTable.value ?: TileDamageConfig.EMPTY

		return@lazy if (health == null && requiredHarvestLevel == null) {
			dmg
		} else if (health != null && requiredHarvestLevel != null) {
			dmg.copy(totalHealth = health, harvestLevel = requiredHarvestLevel)
		} else if (health != null) {
			dmg.copy(totalHealth = health)
		} else {
			dmg.copy(harvestLevel = requiredHarvestLevel!!)
		}
	}
}
