import { VerticalBox, LineEdit, StandardTableView, HorizontalBox, ComboBox, Button } from "std-widgets.slint";
import { NetplayData } from "netplay_page.slint";

export component NetplayJoin inherits Window {
    title: @tr("Netplay: Join Session");
    in-out property server_names <=> NetplayData.server_names;
    in-out property server_urls <=> NetplayData.server_urls;
    in-out property custom_server_url <=> NetplayData.custom_server_url;
    in-out property sessions <=> NetplayData.sessions;
    in-out property current_session <=> NetplayData.current_session;
    in-out property room_urls <=> NetplayData.room_urls;
    in-out property room_ports <=> NetplayData.room_ports;
    in-out property game_name <=> NetplayData.game_name;
    in-out property game_hash <=> NetplayData.game_hash;
    in-out property game_cheats <=> NetplayData.game_cheats;
    in-out property peer_addr <=> NetplayData.peer_addr;
    in-out property pending_session <=> NetplayData.pending_session;
    in-out property pending_refresh <=> NetplayData.pending_refresh;
    in-out property rom_path <=> NetplayData.rom_path;
    in-out property rom_dir <=> NetplayData.rom_dir;
    callback refresh_session();
    callback select_rom(string);
    callback join_session(string, string, string, string, int);
    preferred-width: 700px;

    VerticalBox {
        HorizontalBox {
            VerticalBox {
                Text {
                    text: @tr("Player Name:");
                }

                LineEdit {
                    placeholder_text: @tr("Enter player name");
                    edited(text) => {
                        NetplayData.player_name = text;
                    }
                }
            }

            VerticalBox {
                Text {
                    text: @tr("Custom Server URL (optional):");
                }

                LineEdit {
                    placeholder-text: @tr("example.com:45000");
                    text: NetplayData.custom_server_url;
                    edited(text) => {
                        NetplayData.custom_server_url = text;
                    }
                }
            }
        }

        if NetplayData.pending_refresh: Text {
            text: @tr("Refreshing session list, please wait...");
            horizontal-alignment: center;
            vertical-alignment: center;
        }
        if !NetplayData.pending_refresh && NetplayData.sessions.length > 0: Text {
            text: @tr("Click on a session below to select it");
            horizontal-alignment: center;
            vertical-alignment: center;
        }
        if !NetplayData.pending_refresh && NetplayData.sessions.length == 0: Text {
            text: @tr("No sessions available. Please refresh to check again.");
            horizontal-alignment: center;
            vertical-alignment: center;
        }
        StandardTableView {
            columns: [
                { title: "Server" },
                { title: "Session Name" },
                { title: "Game Name" },
                { title: "Password" },
                { title: "Cheats" }
            ];
            rows: NetplayData.sessions;
            current-row: NetplayData.current_session;
            current-row-changed(current-row) => {
                NetplayData.current_session = current-row;
            }
        }

        HorizontalBox {
            Rectangle { }

            Button {
                enabled: !NetplayData.pending_refresh;
                text: @tr("Refresh Session List");
                clicked => {
                    NetplayData.pending_refresh = true;
                    NetplayData.current_session = -1;
                    NetplayData.sessions = [];
                    NetplayData.room_urls = [];
                    NetplayData.room_ports = [];
                    refresh_session();
                }
            }
        }

        HorizontalBox {
            LineEdit {
                placeholder-text: @tr("Password (if required)");
                edited(text) => {
                    NetplayData.password = text;
                }
            }

            if NetplayData.game_name == "": Button {
                text: @tr("Select ROM");
                clicked => {
                    select_rom(NetplayData.rom_dir);
                }
            }
            if NetplayData.game_name != "": Button {
                text: NetplayData.game_name;
                clicked => {
                    select_rom(NetplayData.rom_dir);
                }
            }

            Button {
                text: @tr("Join Session");
                enabled: NetplayData.player_name != "" && NetplayData.current_session >= 0 && NetplayData.game_name != "" && !NetplayData.pending_session;
                clicked => {
                    NetplayData.pending_session = true;
                    join_session(
                    NetplayData.player_name,
                    NetplayData.game_hash,
                    NetplayData.password,
                    NetplayData.room_urls[NetplayData.current_session],
                    NetplayData.room_ports[NetplayData.current_session]);
                }
            }
        }
    }
}
