package com.atsuishio.superbwarfare.client.tooltip;

import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent;
import com.atsuishio.superbwarfare.perk.AmmoPerk;
import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.tools.FormatTool;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;

public class ClientShotgunImageTooltip extends ClientGunImageTooltip {

    public ClientShotgunImageTooltip(GunImageComponent tooltip) {
        super(tooltip);
    }

    @Override
    protected Component getDamageComponent() {
        boolean slug = false;

        var perk = data.perk.get(Perk.Type.AMMO);
        if (perk instanceof AmmoPerk ammoPerk && ammoPerk.slug) {
            slug = true;
        }

        if (slug) {
            double damage = data.damage() * data.projectileAmount();
            double extraDamage = -1;
            for (var type : Perk.Type.values()) {
                var instance = data.perk.getInstance(type);
                if (instance != null) {
                    damage = instance.perk().getDisplayDamage(damage, data, instance);
                    if (instance.perk().getExtraDisplayDamage(damage, data, instance) >= 0) {
                        extraDamage = instance.perk().getExtraDisplayDamage(damage, data, instance);
                    }
                }
            }

            return Component.translatable("des.superbwarfare.guns.damage").withStyle(ChatFormatting.GRAY)
                    .append(Component.literal("").withStyle(ChatFormatting.RESET))
                    .append(Component.literal(FormatTool.format1D(damage) + (extraDamage >= 0 ? " + " + FormatTool.format1D(extraDamage) : "")).withStyle(ChatFormatting.GREEN));
        } else {
            double damage = data.damage();
            double extraDamage = -1;
            for (var type : Perk.Type.values()) {
                var instance = data.perk.getInstance(type);
                if (instance != null) {
                    damage = instance.perk().getDisplayDamage(damage, data, instance);
                    if (instance.perk().getExtraDisplayDamage(damage, data, instance) >= 0) {
                        extraDamage = instance.perk().getExtraDisplayDamage(damage, data, instance);
                    }
                }
            }

            return Component.translatable("des.superbwarfare.guns.damage").withStyle(ChatFormatting.GRAY)
                    .append(Component.literal("").withStyle(ChatFormatting.RESET))
                    .append(Component.literal(FormatTool.format1D(damage) + " * " + FormatTool.format0D(data.projectileAmount())).withStyle(ChatFormatting.GREEN))
                    .append(Component.literal(extraDamage >= 0 ?
                            ("(" + FormatTool.format1D(damage) + " + " + FormatTool.format1D(extraDamage) + ") * " + FormatTool.format0D(data.projectileAmount()))
                            : FormatTool.format1D(damage, " * ") + FormatTool.format0D(data.projectileAmount())
                    ).withStyle(ChatFormatting.GREEN));
        }
    }
}
