package dev.dfonline.codeclient.mixin.world.block;

import dev.dfonline.codeclient.CodeClient;
import dev.dfonline.codeclient.config.Config;
import dev.dfonline.codeclient.location.Dev;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.block.entity.SignText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(SignBlockEntity.class)
public abstract class MSignBlockEntity {
    @Shadow
    private SignText frontText;

    @Inject(method = "getFrontText", at = @At("HEAD"), cancellable = true)
    public void getFrontText(CallbackInfoReturnable<SignText> cir) {
        if (CodeClient.location instanceof Dev plot) {
            if (plot.isInDev(((BlockEntity) (Object) this).getPos())) {
                SignText orig = this.frontText;
                Text line1 = orig.getMessage(0, false);
                Text line2 = orig.getMessage(1, false);
                Text line3 = orig.getMessage(2, false);
                Text line4 = orig.getMessage(3, false);

                Config config = Config.getConfig();
                if (config.Line1Color != 0)
                    line1 = Text.empty().append(line1).setStyle(Style.EMPTY.withColor(config.Line1Color));
                if (config.Line2Color != 0)
                    line2 = Text.empty().append(line2).setStyle(Style.EMPTY.withColor(config.Line2Color));
                if (config.Line3Color != 0)
                    line3 = Text.empty().append(line3).setStyle(Style.EMPTY.withColor(config.Line3Color));
                if (config.Line4Color != 0)
                    line4 = Text.empty().append(line4).setStyle(Style.EMPTY.withColor(config.Line4Color));


                cir.setReturnValue(
                        orig
                                .withMessage(0, line1)
                                .withMessage(1, line2)
                                .withMessage(2, line3)
                                .withMessage(3, line4)
                );
            }
        }
    }
}
