package one.oth3r.sit;

import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.BooleanControllerBuilder;
import dev.isxander.yacl3.api.controller.EnumControllerBuilder;
import dev.isxander.yacl3.api.controller.StringControllerBuilder;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.util.Formatting;

public class ModMenu implements ModMenuApi {
    private static MutableText lang(String key) {
        return Text.translatable("config.sit."+key);
    }
    private static MutableText lang(String key, Object... args) {
        return Text.translatable("config.sit."+key,args);
    }
    @Override
    public ConfigScreenFactory<?> getModConfigScreenFactory() {
        return parent -> YetAnotherConfigLib.createBuilder().save(config::save)
                .title(Text.of("Sit!"))
                .category(ConfigCategory.createBuilder()
                        .name(lang("category.general"))
                        .tooltip(lang("category.general.tooltip"))
                        .option(Option.<Boolean>createBuilder()
                                .name(lang("general.keep_active"))
                                .description(OptionDescription.of(lang("general.keep_active.description")))
                                .binding(config.defaults.keepActive, () -> config.keepActive, n -> config.keepActive = n)
                                .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                .build())
                        .option(Option.<Boolean>createBuilder()
                                .name(lang("general.sit_while_seated"))
                                .description(OptionDescription.of(lang("general.sit_while_seated.description")))
                                .binding(config.defaults.sitWhileSeated, () -> config.sitWhileSeated, n -> config.sitWhileSeated = n)
                                .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                .build())
                        .group(OptionGroup.createBuilder()
                                .name(lang("general.sittable"))
                                .description(OptionDescription.of(lang("general.sittable.description")))
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("general.sittable.stairs"))
                                        .binding(config.defaults.stairsOn, () -> config.stairsOn, n -> config.stairsOn = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("general.sittable.slabs"))
                                        .binding(config.defaults.slabsOn, () -> config.slabsOn, n -> config.slabsOn = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("general.sittable.carpets"))
                                        .binding(config.defaults.carpetsOn, () -> config.carpetsOn, n -> config.carpetsOn = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("general.sittable.full_blocks"))
                                        .binding(config.defaults.fullBlocksOn, () -> config.fullBlocksOn, n -> config.fullBlocksOn = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("general.sittable.custom"))
                                        .description(OptionDescription.of(lang("general.sittable.custom.description")))
                                        .binding(config.defaults.customOn, () -> config.customOn, n -> config.customOn = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).onOffFormatter())
                                        .build())
                                .build())
                        .group(ListOption.<String>createBuilder()
                                .name(lang("general.sittable_blocks"))
                                .description(OptionDescription.of(
                                        lang("general.sittable_blocks.description")
                                                .append("\n\n").append(lang("general.sittable_blocks.description_2"))
                                                .append(lang("general.sittable_blocks.description_3",
                                                        lang("general.sittable_blocks.description_3_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))),
                                                        lang("general.sittable_blocks.description_3_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))),
                                                        lang("general.sittable_blocks.description_3_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))),
                                                        lang("general.sittable_blocks.description_3_5").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GOLD)))).styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY))))
                                                .append("\n\n").append(lang("general.sittable_blocks.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))))
                                                .append("\n").append(lang("general.sittable_blocks.description_5").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))))
                                                .append("\n").append(lang("general.sittable_blocks.description_6").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))))
                                                .append("\n").append(lang("general.sittable_blocks.description_7").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GOLD))))
                                                .append("\n\n").append(lang("general.sittable_blocks.description_8").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.YELLOW))))))
                                .binding(config.defaults.customBlocks, () -> config.customBlocks, n -> config.customBlocks = n)
                                .controller(StringControllerBuilder::create)
                                .initial("")
                                .build())
                        .build())
                .category(ConfigCategory.createBuilder()
                        .name(lang("category.main_hand"))
                        .tooltip(lang("category.main_hand.tooltip"))
                        .option(Option.<config.HandRequirement>createBuilder()
                                .name(lang("hand.requirements"))
                                .description(OptionDescription.of(lang("hand.requirements.description")
                                        .append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))))
                                        .append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))))
                                        .append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))))))
                                .binding(config.defaults.mainReq, () -> config.mainReq, n -> config.mainReq = n)
                                .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class)
                                        .formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
                                .build())
                        .group(OptionGroup.createBuilder()
                                .name(lang("hand.restrictions"))
                                .description(OptionDescription.of(lang("hand.restrictions.description")))
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.blocks"))
                                        .binding(config.defaults.mainBlock,()-> config.mainBlock,n -> config.mainBlock = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.food"))
                                        .binding(config.defaults.mainFood,()-> config.mainFood,n -> config.mainFood = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.usable"))
                                        .description(OptionDescription.of(lang("hand.restrictions.usable.description")))
                                        .binding(config.defaults.mainUsable,()-> config.mainUsable,n -> config.mainUsable = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .build())
                        .group(ListOption.<String>createBuilder()
                                .name(lang("hand.whitelist"))
                                .description(OptionDescription.of(lang("hand.whitelist.description")
                                        .append("\n\n").append(lang("hand.list.description"))
                                        .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY))))))
                                .binding(config.defaults.mainWhitelist, () -> config.mainWhitelist, n -> config.mainWhitelist = n)
                                .controller(StringControllerBuilder::create)
                                .initial("")
                                .build())
                        .group(ListOption.<String>createBuilder()
                                .name(lang("hand.blacklist"))
                                .description(OptionDescription.of(lang("hand.blacklist.description")
                                        .append("\n\n").append(lang("hand.list.description"))
                                        .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY))))))
                                .binding(config.defaults.mainBlacklist, () -> config.mainBlacklist, n -> config.mainBlacklist = n)
                                .controller(StringControllerBuilder::create)
                                .initial("")
                                .build())
                        .build())
                .category(ConfigCategory.createBuilder()
                        .name(lang("category.off_hand"))
                        .tooltip(lang("category.off_hand.tooltip"))
                        .option(Option.<config.HandRequirement>createBuilder()
                                .name(lang("hand.requirements"))
                                .description(OptionDescription.of(lang("hand.requirements.description")
                                        .append("\n\n").append(lang("hand.requirements.description_2").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.AQUA))))
                                        .append("\n").append(lang("hand.requirements.description_3").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.GREEN))))
                                        .append("\n").append(lang("hand.requirements.description_4").styled(style -> style.withColor(TextColor.fromFormatting(Formatting.RED))))))
                                .binding(config.defaults.offReq, () -> config.offReq, n -> config.offReq = n)
                                .controller(opt -> EnumControllerBuilder.create(opt).enumClass(config.HandRequirement.class)
                                        .formatValue(v -> Text.translatable("config.sit."+v.name().toLowerCase())))
                                .build())
                        .group(OptionGroup.createBuilder()
                                .name(lang("hand.restrictions"))
                                .description(OptionDescription.of(lang("hand.restrictions.description")))
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.blocks"))
                                        .binding(config.defaults.offBlock,()-> config.offBlock,n -> config.offBlock = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.food"))
                                        .binding(config.defaults.offFood,()-> config.offFood,n -> config.offFood = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .option(Option.<Boolean>createBuilder()
                                        .name(lang("hand.restrictions.usable"))
                                        .description(OptionDescription.of(lang("hand.restrictions.usable.description")))
                                        .binding(config.defaults.offUsable,()-> config.offUsable,n -> config.offUsable = n)
                                        .controller(opt -> BooleanControllerBuilder.create(opt).trueFalseFormatter())
                                        .build())
                                .build())
                        .group(ListOption.<String>createBuilder()
                                .name(lang("hand.whitelist"))
                                .description(OptionDescription.of(lang("hand.whitelist.description")
                                        .append("\n\n").append(lang("hand.list.description"))
                                        .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY))))))
                                .binding(config.defaults.offWhitelist, () -> config.offWhitelist, n -> config.offWhitelist = n)
                                .controller(StringControllerBuilder::create)
                                .initial("")
                                .build())
                        .group(ListOption.<String>createBuilder()
                                .name(lang("hand.blacklist"))
                                .description(OptionDescription.of(lang("hand.blacklist.description")
                                        .append("\n\n").append(lang("hand.list.description"))
                                        .append(lang("hand.list.description_2").styled(style -> style.withItalic(true).withColor(TextColor.fromFormatting(Formatting.GRAY))))))
                                .binding(config.defaults.offBlacklist, () -> config.offBlacklist, n -> config.offBlacklist = n)
                                .controller(StringControllerBuilder::create)
                                .initial("")
                                .build())
                        .build())
                .build().generateScreen(parent);
    }
}
