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

import net.minecraft.ChatFormatting
import net.minecraft.core.BlockPos
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundEvents
import net.minecraft.sounds.SoundSource
import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.level.BlockGetter
import net.minecraft.world.level.Level
import net.minecraft.world.level.LevelAccessor
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.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.TranslatableComponent
import ru.dbotthepony.mc.otm.core.get

class TritaniumPressurePlate(color: DyeColor?) : BasePressurePlateBlock(Properties.of().mapColor(color ?: DyeColor.LIGHT_BLUE).sound(SoundType.METAL).explosionResistance(80f).noOcclusion().destroyTime(3f).requiresCorrectToolForDrops(), BlockSetType.IRON) {
	override fun appendHoverText(
		p_49816_: ItemStack,
		p_49817_: BlockGetter?,
		p_49818_: MutableList<Component>,
		p_49819_: TooltipFlag
	) {
		super.appendHoverText(p_49816_, p_49817_, p_49818_, p_49819_)
		p_49818_.add(TranslatableComponent("$descriptionId.description0").withStyle(ChatFormatting.DARK_GRAY))
		p_49818_.add(TranslatableComponent("$descriptionId.description1").withStyle(ChatFormatting.DARK_GRAY))
	}

	init {
		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)
	}
}
