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

export component NetplayCreate inherits Window {
    title: @tr("Netplay: Create 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 game_name <=> NetplayData.game_name;
    in-out property game_hash <=> NetplayData.game_hash;
    in-out property game_cheats <=> NetplayData.game_cheats;
    in-out property pending_session <=> NetplayData.pending_session;
    in-out property rom_path <=> NetplayData.rom_path;
    in-out property peer_addr <=> NetplayData.peer_addr;
    in-out property rom_dir <=> NetplayData.rom_dir;
    callback get_custom_url();
    callback select_rom(string);
    callback create_session(string, string, string, string, string, string, string);

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

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

            VerticalBox {
                Text {
                    text: @tr("Password (Optional):");
                }

                LineEdit {
                    placeholder_text: @tr("Enter session password");
                    edited(text) => {
                        NetplayData.password = text;
                    }
                }
            }
        }

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

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

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

            HorizontalBox {
                server := ComboBox {
                    model: NetplayData.server_names;
                    selected => {
                        if (self.current-index >= 0) {
                            if (self.current-value == "Custom") {
                                get_custom_url();
                            }
                        }
                    }
                }
            }
        }

        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);
            }
        }

        Rectangle { }

        if NetplayData.game_cheats != "":Text {
            text: @tr("This game has cheats enabled. Cheats will be synchronized with other players.");
        }
        Rectangle { }

        Button {
            property <string> server_url;
            text: @tr("Create Session");
            enabled: NetplayData.session_name != "" && NetplayData.player_name != "" && NetplayData.game_name != "" && !NetplayData.pending_session;
            clicked => {
                NetplayData.pending_session = true;
                if (server.current-value == "Custom") {
                    server_url = "ws://" + NetplayData.custom_server_url;
                } else {
                    server_url = NetplayData.server_urls[server.current-index];
                }
                create_session(
                    server_url,
                    NetplayData.session_name,
                    NetplayData.player_name,
                    NetplayData.game_name,
                    NetplayData.game_hash,
                    NetplayData.game_cheats,
                    NetplayData.password);
            }
        }
    }
}

export component DispatcherDialog inherits Dialog {
    title: @tr("Creating Server");
    Text {
        text: "Creating server, please wait...This may take about 30 seconds.";
    }
}
