package eu.mhsl.craftattack.teamLobby.util;

import eu.mhsl.craftattack.teamLobby.http.ReqResp;
import eu.mhsl.craftattack.teamLobby.http.Request;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.timer.TaskSchedule;

import java.net.http.HttpRequest;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ConnectInfo {
    public static int availableTickets = 0;
    public static void tickets(String teamName, Player player) {
        var task = new Runnable() {
            @Override
            public void run() {
                HttpRequest request = Request.builder(Request.server(
                    "playtimer/tickets",
                    uriBuilder -> uriBuilder.addParameter("team", teamName)
                )).GET().build();

                record Ticket(String team, int tickets)  {}
                record Status(String status, Ticket response) {}
                ReqResp<Status> ticket = Request.execute(request, Status.class);
                availableTickets = ticket.data().response.tickets;

                MinecraftServer.getSchedulerManager().submitTask(
                    () -> {
                        String playtimeOverview = String.format(
                            "Dein Team hat noch %d Beitritte, also %dx%d=%d Minuten übrig.",
                            availableTickets,
                            availableTickets,
                            30,
                            availableTickets * 30
                        );
                        player.sendMessage(Component.text(playtimeOverview, NamedTextColor.DARK_GREEN));
                        return TaskSchedule.stop();
                    }
                );
            }
        };

        new Thread(task).start();
    }

    record LocationDto(String world, double x, double y, double z, float yaw, float pitch) {
        public Pos toPos() {
            return new Pos(this.x, this.y, this.z, this.yaw, this.pitch);
        }
    }

    record LeakInfoDto(String name, LocationDto location, long leakedAt) {
        public Instant leakedAtInstant() {
            return Instant.ofEpochMilli(this.leakedAt);
        }

        public Pos pos() {
            return this.location.toPos();
        }
    }

    public static void cords(Player player) {
        var task = new Runnable() {
            @Override
            public void run() {
                HttpRequest request = Request.builder(Request.server(
                    "strike/cords",
                    uriBuilder -> {}
                )).GET().build();

                record Status(String status, Map<String, LeakInfoDto> response) {}
                ReqResp<Status> ticket = Request.execute(request, Status.class);

                System.out.println(ticket.data().response.toString());
                ComponentBuilder<TextComponent, TextComponent.Builder> text = Component.text()
                    .append(Component.text("Aktuell geleakte Koordinaten:", NamedTextColor.AQUA));

                ticket.data().response().forEach((uuidStr, leakInfo) -> {
                    appendCordLeak(text, leakInfo);
                });

                player.sendMessage(text.build());
            }
        };

        new Thread(task).start();
    }

    private static void appendCordLeak(ComponentBuilder<TextComponent, TextComponent.Builder> text, LeakInfoDto info) {
        String timestamp = DateTimeFormatter.ofPattern("dd.MM HH:mm")
            .withZone(ZoneId.of("Europe/Berlin"))
            .format(Instant.ofEpochMilli(info.leakedAt));

        text.appendNewline();
        text.append(Component.text(info.name(), NamedTextColor.DARK_AQUA));
        text.append(Component.text(": ", NamedTextColor.GRAY));
        text.append(Component.text(
            String.format("(%s) %d, %d, %d - Uhrzeit: %s",
                info.location().world(),
                (int) info.pos().x(),
                (int) info.pos().y(),
                (int) info.pos().z(),
                timestamp
            ),
            NamedTextColor.AQUA
        ));
    }
}
