from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
APP_JS = (ROOT / "ui_builder/static/app.js").read_text(encoding="utf-8")
STYLES = (ROOT / "ui_builder/static/styles.css").read_text(encoding="utf-8")


def test_player_selection_slots_can_be_reordered_inside_panel():
    for token in (
        "hockeyPlayerSelectionDrag",
        "async function hockeySwapPlayerSelectionSlots(panel, fromSlot, toSlot)",
        'application/x-ui-builder-hockey-player-selection-slot',
        'event.dataTransfer.dropEffect = "move"',
        "await hockeySwapPlayerSelectionSlots(panel, sourceSlot, targetSlot)",
    ):
        assert token in APP_JS


def test_swap_updates_both_slots_in_one_mapping_refresh():
    start = APP_JS.index("async function hockeySwapPlayerSelectionSlots")
    end = APP_JS.index("async function hockeyClearPlayerSelectionSlot", start)
    block = APP_JS[start:end]
    assert "...hockeyPlayerSelectionPatch(panel, targetSlot, sourcePlayer)" in block
    assert "...hockeyPlayerSelectionPatch(panel, sourceSlot, targetPlayer)" in block
    assert "await hockeySetMatchValues(patch, { refreshMapping: true });" in block


def test_filled_player_slots_expose_drag_handle_and_move_cursor():
    assert 'draggable="true" title="Перетащите на другой слот' in APP_JS
    assert "hockey-player-select-drag" in APP_JS
    assert '.hockey-player-select-slot[draggable="true"] { cursor:grab; }' in STYLES
    assert ".hockey-player-select-slot.is-dragging" in STYLES


def test_bottom_panel_settings_are_split_into_two_tabs():
    for token in (
        'data-prematch-editor-tab="buttons"',
        'data-prematch-editor-tab="players"',
        'Кнопки</button>',
        'Блоки игроков <span>${draftPlayerPanels.length}</span>',
        'activeEditorSection = section;',
    ):
        assert token in APP_JS
    assert ".prematch-editor-tabs" in STYLES
    assert ".prematch-editor-tab.active" in STYLES


def test_player_panel_editor_only_renders_in_players_tab():
    assert 'activeEditorSection === "buttons" ? `' in APP_JS
    assert 'data-prematch-editor-section="players"' in APP_JS
    assert 'player-selection-editor prematch-editor-section' in APP_JS
