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

export component NetplayJoin inherits Page {
    title: @tr("Netplay: Join Session");
    callback refresh_sessions();
    callback select_rom(string);
    callback join_session(string, string, string, string);
    callback close;

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

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

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

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

        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: @tr("Session Name") },
            { title: @tr("Game Name") },
            { title: @tr("Password") },
            { title: @tr("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 = [];
                    refresh_sessions();
                }
            }
        }

        HorizontalBox {
            alignment: end;
            Rectangle { }

            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.sessions[NetplayData.current_session][0].text,
                    NetplayData.player_name,
                    NetplayData.game_hash,
                    NetplayData.password);
                }
            }
            Button {
                text: @tr("Cancel");
                clicked => {
                    close();
                }
            }
        }
    }
}
