package sh.grimmer.bettershulkers.integration;

import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import sh.grimmer.bettershulkers.BetterShulkers;
import sh.grimmer.bettershulkers.config.BetterShulkersConfig;
import sh.grimmer.bettershulkers.config.ShulkerItemDisplayType;
import sh.grimmer.bettershulkers.config.TooltipPreviewMode;

import java.util.Optional;

public class ClothConfigScreen {

    public static Screen create(Screen parent) {
        BetterShulkersConfig cfg = BetterShulkers.CONFIG;

        ConfigBuilder builder = ConfigBuilder.create()
                .setParentScreen(parent)
                .setTitle(Component.translatable("cloth_config.better_shulkers.settings"))
                .setSavingRunnable(BetterShulkers::saveConfig);

        ConfigEntryBuilder eb = builder.entryBuilder();

        ConfigCategory tooltips = builder.getOrCreateCategory(
                Component.translatable("cloth_config.better_shulkers.category.tooltips"));

        tooltips.addEntry(eb
                .startEnumSelector(t("tooltip_preview"), TooltipPreviewMode.class, cfg.tooltipPreview)
                .setTooltip(d("tooltip_preview"))
                .setDefaultValue(TooltipPreviewMode.WHEN_FILLED)
                .setSaveConsumer(v -> cfg.tooltipPreview = v)
                .setEnumNameProvider(v -> ((TooltipPreviewMode) v).getDisplayName())
                .setTooltipSupplier(v -> Optional.of(new Component[]{
                        d("tooltip_preview"),
                        Component.translatable("cloth_config.better_shulkers.tooltip_preview.selected",
                                v.getDisplayName().copy().withStyle(ChatFormatting.GREEN),
                                v.getDescription().copy().withStyle(ChatFormatting.GREEN))
                }))
                .build());

        ConfigCategory hints = builder.getOrCreateCategory(
                Component.translatable("cloth_config.better_shulkers.category.hints"));

        hints.addEntry(eb
                .startBooleanToggle(t("show_item_hint"), cfg.showItemHint)
                .setTooltip(d("show_item_hint"))
                .setDefaultValue(true)
                .setSaveConsumer(v -> cfg.showItemHint = v)
                .build());

        hints.addEntry(eb
                .startBooleanToggle(t("show_block_hint"), cfg.showBlockHint)
                .setTooltip(d("show_block_hint"))
                .setDefaultValue(true)
                .setSaveConsumer(v -> cfg.showBlockHint = v)
                .build());

        hints.addEntry(eb
                .startEnumSelector(t("item_display_type"), ShulkerItemDisplayType.class, cfg.itemDisplayType)
                .setTooltip(d("item_display_type"))
                .setDefaultValue(ShulkerItemDisplayType.SINGLE_TYPE)
                .setSaveConsumer(v -> cfg.itemDisplayType = v)
                .setEnumNameProvider(v -> ((ShulkerItemDisplayType) v).getDisplayName())
                .setTooltipSupplier(v -> Optional.of(new Component[]{
                        d("item_display_type"),
                        Component.translatable("cloth_config.better_shulkers.item_display_type.selected",
                                v.getDisplayName().copy().withStyle(ChatFormatting.GREEN),
                                v.getDescription().copy().withStyle(ChatFormatting.GREEN))
                }))
                .build());

        hints.addEntry(eb
                .startBooleanToggle(t("show_labels"), cfg.showLabels)
                .setTooltip(d("show_labels"))
                .setDefaultValue(true)
                .setSaveConsumer(v -> cfg.showLabels = v)
                .build());

        hints.addEntry(eb
                .startBooleanToggle(t("hide_model_behind_hint"), cfg.hideModelBehindHint)
                .setTooltip(d("hide_model_behind_hint"))
                .setDefaultValue(false)
                .setSaveConsumer(v -> cfg.hideModelBehindHint = v)
                .build());

        hints.addEntry(eb
                .startIntField(t("hint_item_scale"), cfg.hintItemScale)
                .setTooltip(d("hint_item_scale"))
                .setMin(50).setMax(200)
                .setDefaultValue(100)
                .setSaveConsumer(v -> cfg.hintItemScale = v)
                .build());

        hints.addEntry(eb
                .startBooleanToggle(t("show_fill_bar"), cfg.showFillBar)
                .setTooltip(d("show_fill_bar"))
                .setDefaultValue(true)
                .setSaveConsumer(v -> cfg.showFillBar = v)
                .build());

        return builder.build();
    }

    private static Component t(String key) {
        return Component.translatable("cloth_config.better_shulkers." + key);
    }

    private static Component d(String key) {
        return Component.translatable("cloth_config.better_shulkers." + key + ".description");
    }
}
