/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.minecraft.class_437
 */
package meteordevelopment.meteorclient.gui.tabs.builtin;

import java.lang.invoke.StringConcatFactory;
import java.util.Comparator;
import java.util.List;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.tabs.Tab;
import meteordevelopment.meteorclient.gui.tabs.TabScreen;
import meteordevelopment.meteorclient.gui.tabs.Tabs;
import meteordevelopment.meteorclient.gui.tabs.WindowTabScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList;
import meteordevelopment.meteorclient.gui.widgets.containers.WTable;
import meteordevelopment.meteorclient.gui.widgets.containers.WVerticalList;
import meteordevelopment.meteorclient.gui.widgets.input.WTextBox;
import meteordevelopment.meteorclient.gui.widgets.pressable.WButton;
import meteordevelopment.meteorclient.systems.publicconfigs.PublicConfig;
import meteordevelopment.meteorclient.systems.publicconfigs.PublicConfigs;
import net.minecraft.class_437;

public class PublicConfigsTab
extends Tab {
    public PublicConfigsTab() {
        super("Public Configs");
    }

    @Override
    public TabScreen createScreen(GuiTheme theme) {
        return new PublicConfigsScreen(theme, this);
    }

    @Override
    public boolean isScreen(class_437 screen) {
        return screen instanceof PublicConfigsScreen || screen instanceof UploadConfigScreen || screen instanceof CommentsScreen;
    }

    public static class PublicConfigsScreen
    extends WindowTabScreen {
        public PublicConfigsScreen(GuiTheme theme, Tab tab) {
            super(theme, tab);
        }

        @Override
        public void initWidgets() {
            this.add(this.theme.label("Share and download community configs!")).expandX().center();
            this.add(this.theme.label("Divine made by Vyn?!")).expandX().center();
            this.add(this.theme.horizontalSeparator()).expandX();
            WButton upload = this.add(this.theme.button("Upload Config")).expandX().widget();
            upload.action = () -> MeteorClient.mc.method_1507((class_437)new UploadConfigScreen(this.theme, this.parent));
            this.add(this.theme.horizontalSeparator()).expandX();
            PublicConfigs system = PublicConfigs.get();
            List<PublicConfig> configs = system.configs;
            configs.sort(Comparator.comparing(c -> !c.pinned).thenComparing(c -> c.name));
            if (configs.isEmpty()) {
                this.add(this.theme.label("No configs uploaded yet.")).expandX().center();
            } else {
                WTable table = this.add(new WTable()).expandX().widget();
                for (PublicConfig config : configs) {
                    table.add(this.theme.label(config.name + (config.pinned ? " [PINNED]" : ""))).expandX();
                    table.add(this.theme.label("by " + config.author)).expandX();
                    WButton download = table.add(this.theme.button("Download")).widget();
                    download.action = () -> {};
                    WButton comments = table.add(this.theme.button("Comments (" + config.comments.size() + ")")).widget();
                    comments.action = () -> MeteorClient.mc.method_1507((class_437)new CommentsScreen(this.theme, this.parent, config));
                    if (system.isAdmin()) {
                        WButton pin = table.add(this.theme.button(config.pinned ? "Unpin" : "Pin")).widget();
                        pin.action = () -> {
                            config.pinned = !config.pinned;
                            system.save();
                            MeteorClient.mc.method_1507((class_437)this.tab.createScreen(this.theme));
                        };
                        WButton delete = table.add(this.theme.button("Delete")).widget();
                        delete.action = () -> {
                            system.remove(config);
                            MeteorClient.mc.method_1507((class_437)this.tab.createScreen(this.theme));
                        };
                    }
                    table.row();
                }
            }
            this.add(this.theme.horizontalSeparator()).expandX();
            this.add(this.theme.label("Max configs: 100 (" + configs.size() + "/100)")).expandX().center();
        }
    }

    public static class UploadConfigScreen
    extends WindowTabScreen {
        public UploadConfigScreen(GuiTheme theme, class_437 parent) {
            super(theme, Tabs.get(PublicConfigsTab.class));
            this.parent = parent;
        }

        @Override
        public void initWidgets() {
            WVerticalList list = this.add(this.theme.verticalList()).expandX().widget();
            list.add(this.theme.label("Config Name:"));
            WTextBox name = list.add(this.theme.textBox("")).expandX().widget();
            list.add(this.theme.label("Config Data:"));
            WTextBox content = list.add(this.theme.textBox("")).expandX().widget();
            WHorizontalList buttons = list.add(this.theme.horizontalList()).expandX().widget();
            WButton upload = buttons.add(this.theme.button("Upload")).expandX().widget();
            WButton back = buttons.add(this.theme.button("Back")).expandX().widget();
            upload.action = () -> {
                String validation = PublicConfigs.get().validate(name.get(), content.get());
                if (validation == null) {
                    PublicConfigs.get().add(new PublicConfig(name.get(), content.get(), MeteorClient.mc.method_1548().method_1676()));
                    MeteorClient.mc.method_1507(this.parent);
                } else {
                    name.set(validation);
                }
            };
            back.action = () -> MeteorClient.mc.method_1507(this.parent);
        }
    }

    public static class CommentsScreen
    extends WindowTabScreen {
        private final PublicConfig config;

        public CommentsScreen(GuiTheme theme, class_437 parent, PublicConfig config) {
            super(theme, Tabs.get(PublicConfigsTab.class));
            this.parent = parent;
            this.config = config;
        }

        @Override
        public void initWidgets() {
            this.add(this.theme.label("Comments for " + this.config.name)).expandX().center();
            this.add(this.theme.horizontalSeparator()).expandX();
            WVerticalList list = this.add(this.theme.verticalList()).expandX().widget();
            if (this.config.comments.isEmpty()) {
                list.add(this.theme.label("No comments yet.")).expandX().center();
            } else {
                for (PublicConfig.Comment comment : this.config.comments) {
                    WHorizontalList row = list.add(this.theme.horizontalList()).expandX().widget();
                    row.add(this.theme.label((String)((Object)StringConcatFactory.makeConcatWithConstants("makeConcatWithConstants", new Object[]{"\u0001: "}, (String)comment.author)))).widget().color = this.theme.textColor();
                    row.add(this.theme.label(comment.text)).expandX();
                    if (!PublicConfigs.get().isAdmin()) continue;
                    WButton delete = row.add(this.theme.button("X")).widget();
                    delete.action = () -> {
                        this.config.comments.remove(comment);
                        PublicConfigs.get().save();
                        MeteorClient.mc.method_1507((class_437)new CommentsScreen(this.theme, this.parent, this.config));
                    };
                }
            }
            this.add(this.theme.horizontalSeparator()).expandX();
            WTextBox commentBox = this.add(this.theme.textBox("")).expandX().widget();
            WHorizontalList buttons = this.add(this.theme.horizontalList()).expandX().widget();
            WButton post = buttons.add(this.theme.button("Post Comment")).expandX().widget();
            WButton back = buttons.add(this.theme.button("Back")).expandX().widget();
            post.action = () -> {
                String validation = PublicConfigs.get().validateComment(commentBox.get());
                if (validation == null) {
                    this.config.comments.add(new PublicConfig.Comment(MeteorClient.mc.method_1548().method_1676(), commentBox.get()));
                    PublicConfigs.get().save();
                    MeteorClient.mc.method_1507((class_437)new CommentsScreen(this.theme, this.parent, this.config));
                } else {
                    commentBox.set(validation);
                }
            };
            back.action = () -> MeteorClient.mc.method_1507(this.parent);
        }
    }
}

