package ru.dbotthepony.mc.otm.block.decorative

import com.mojang.serialization.MapCodec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.ChatFormatting
import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.BasePressurePlateBlock
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.SoundType
import net.minecraft.world.level.block.state.BlockBehaviour
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.block.state.StateDefinition
import net.minecraft.world.level.block.state.properties.BlockSetType
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import ru.dbotthepony.mc.otm.core.TooltipList
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.get
import java.util.Optional
import kotlin.jvm.optionals.getOrNull

class TritaniumPressurePlate(
	val color: DyeColor?,
	properties: Properties = Properties.of()
		.mapColor(color ?: DyeColor.LIGHT_BLUE)
		.sound(SoundType.METAL)
		.explosionResistance(80f)
		.noOcclusion()
		.destroyTime(3f)
		.requiresCorrectToolForDrops()
		.forceSolidOn()
		.noCollission(),
	typeSet: BlockSetType = BlockSetType.IRON,
) : BasePressurePlateBlock(properties, typeSet) {
	val tooltips = TooltipList()

	private val codec: MapCodec<TritaniumPressurePlate> = RecordCodecBuilder.mapCodec {
		it.group(
			DyeColor.CODEC
				.optionalFieldOf("color")
				.xmap({ it.getOrNull() }, { Optional.ofNullable(it) })
				.forGetter(TritaniumPressurePlate::color),
			propertiesCodec(),
			BlockSetType.CODEC.fieldOf("block_set_type").forGetter(TritaniumPressurePlate::type)
		).apply(it, ::TritaniumPressurePlate)
	}

	override fun codec(): MapCodec<out TritaniumPressurePlate> {
		return codec
	}

	override fun appendHoverText(
		itemStack: ItemStack,
		context: Item.TooltipContext,
		lines: MutableList<Component>,
		tooltipType: TooltipFlag
	) {
		super.appendHoverText(itemStack, context, lines, tooltipType)
		tooltips.assemble(itemStack, context, lines)
	}

	init {
		tooltips.add { TranslatableComponent("$descriptionId.description0").withStyle(ChatFormatting.DARK_GRAY) }
		tooltips.add { TranslatableComponent("$descriptionId.description1").withStyle(ChatFormatting.DARK_GRAY) }
		tooltips.painted(color)
		registerDefaultState(stateDefinition.any().setValue(BlockStateProperties.POWERED, false))
	}

	override fun createBlockStateDefinition(p_49915_: StateDefinition.Builder<Block, BlockState>) {
		super.createBlockStateDefinition(p_49915_)
		p_49915_.add(BlockStateProperties.POWERED)
	}

	override fun getSignalStrength(level: Level, pos: BlockPos): Int {
		if (level.getEntitiesOfClass(ServerPlayer::class.java, TOUCH_AABB.move(pos)).isNotEmpty()) {
			return 15
		} else {
			return 0
		}
	}

	override fun getSignalForState(state: BlockState): Int {
		return if (state[BlockStateProperties.POWERED]) 15 else 0
	}

	override fun setSignalForState(state: BlockState, signal: Int): BlockState {
		return state.setValue(BlockStateProperties.POWERED, signal > 0)
	}
}
