package com.r3944realms.modernlifepatch.mixin.block.common;

import com.dairymoose.modernlife.blocks.LightBulbBlock;
import com.dairymoose.modernlife.blocks.WarningAlarmBlock;
import com.r3944realms.modernlifepatch.datagen.lang.ModLangKeyValue;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
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.block.EntityBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.function.ToIntFunction;

import static com.dairymoose.modernlife.blocks.WarningAlarmBlock.ALARM_TYPE;

@Mixin(WarningAlarmBlock.class)
public abstract class MixinWarningAlarm extends LightBulbBlock implements EntityBlock {

    public MixinWarningAlarm(Properties props, ToIntFunction<BlockState> lightLevelFn, StatePredicate emissiveRenderFn) {
        super(props, lightLevelFn, emissiveRenderFn);
    }
    @Inject(method = "appendHoverText", at = @At("HEAD"), cancellable = true)
    public void appendHoverText(ItemStack itemStack, BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag, CallbackInfo ci) {
        list.add(Component.translatable(ModLangKeyValue.WARNING_ALARM_HOVER_FIRST.getKey()));
        list.add(Component.translatable(ModLangKeyValue.WARNING_ALARM_HOVER_SECOND.getKey()));
        ci.cancel();
    }

    /**
     * @author R3944Realms
     * @reason i18n
     */
    @Overwrite
    public InteractionResult use(@NotNull BlockState pState, Level pLevel, @NotNull BlockPos pPos, @NotNull Player pPlayer, @NotNull InteractionHand pHand, @NotNull BlockHitResult pHit) {
        if (!pLevel.isClientSide && pHand == InteractionHand.MAIN_HAND && pPlayer.getItemInHand(pHand).isEmpty()) {
            BlockState newAlarmType = pState.cycle(ALARM_TYPE);
            pLevel.setBlock(pPos, newAlarmType, 3);
            switch (newAlarmType.getValue(ALARM_TYPE)) {
                case 0 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE0.getKey()));
                case 1 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE1.getKey()));
                case 2 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE2.getKey()));
                case 3 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE3.getKey()));
                case 4 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE4.getKey()));
                case 5 -> pPlayer.sendSystemMessage(Component.translatable(ModLangKeyValue.ALARM_SOUND_TYPE5.getKey()));
            }
        }

        return super.use(pState, pLevel, pPos, pPlayer, pHand, pHit);
    }
}
