# -*- coding: utf-8 -*-
"""Shared imports and runtime dependencies for the GUI package."""

import sys
import os
import re
import json
import subprocess
import threading
import logging
import tempfile
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

SRC_DIR = Path(__file__).resolve().parents[1]

from services.commands import (
    IS_WINDOWS,
    ensure_and_get_server_manager_config_dir,
    find_existing_default_config_path,
    find_existing_user_config_path,
    get_default_config_path,
    get_local_ip,
    get_project_config_path,
    get_server_manager_config_dir,
    get_user_config_path,
    get_ebin_paths,
    get_erl_cmd,
    get_escript_cmd,
    get_rebar3_cmd,
    run_cmd_in_new_window,
    register_launched_window,
    get_launched_windows,
    close_launched_windows,
    build_start_command,
    build_start_command_by_config,
    build_stop_command,
    build_remsh_command,
    subprocess_args_to_display,
    read_config_file,
    read_merged_config,
    write_config_file,
    replace_placeholders,
    get_template_file,
    build_replace_map,
    generate_start_config,
    run_startup_migrations,
    get_config_list,
    get_server_list,
    query_remote_servers_from_login_rpc,
    rpc_clear_server_on_center_and_login,
    get_login_db_name,
    get_node_status_cache,
    check_nodes_status,
    check_node_status,
    test_mysql_connection,
    REBAR3_CMD,
    DEFAULT_CONFIG_FILE,
    LEGACY_DEFAULT_CONFIG_FILE,
    USER_CONFIG_FILE,
    LEGACY_USER_CONFIG_FILE,
    PROJECT_CONFIG_FILE,
)

from services.server_creator import (
    get_existing_server_ids,
    create_server,
    get_id_range_from_config,
    get_default_port,
    get_default_server_name,
    build_confirmation_message,
    generate_server_dir_name,
    get_server_type_name,
    get_allowed_config_keys,
    get_required_keys,
    get_template_config_keys,
    SERVER_TYPE_MAP,
)

from services.hot_update import (
    get_current_version,
    get_version_info,
    fetch_update_manifest,
    version_less,
    get_delta_for_current,
    get_delta_kind,
    get_delta_download_url,
    get_delta_expected_sha256,
    download_file as updater_download_file,
    run_installer_and_exit,
    apply_delta_patch,
    apply_file_delta_package,
    clean_up_old_version,
)

from views.logs.log_viewer_tab import LogViewerTab

from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QTabWidget, QLabel, QLineEdit, QPushButton, QTextEdit, QComboBox,
    QGroupBox, QFormLayout, QSpinBox, QMessageBox, QFileDialog,
    QSplitter, QFrame, QScrollArea, QCheckBox, QGridLayout,
    QListWidget, QListWidgetItem, QDialog, QDialogButtonBox,
    QProgressBar, QRadioButton, QButtonGroup, QDateTimeEdit,
    QTableWidget, QTableWidgetItem, QHeaderView, QStackedWidget,
    QProgressDialog, QAbstractItemView, QSizePolicy,
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QTimer, QDateTime, QSize, QPointF, QByteArray, QEvent
from PyQt6.QtGui import (
    QFont, QColor, QPalette, QIcon, QAction, QCursor,
    QPainter, QPen, QBrush, QPixmap, QPolygonF,
)
from PyQt6.QtSvg import QSvgRenderer

from .logging import logger


def get_terminal_launch_options(config) -> Dict[str, str]:
    """Return the currently configured terminal launch options for new windows."""
    return {
        "terminal_mode": config.get("terminal", "launch_mode", default="auto"),
        "terminal_exe_path": config.get("terminal", "exe_path", default=""),
    }
