namespace Ehwrj.App.Models;

public sealed class UiText
{
    private static readonly UiText English = new("en-US")
    {
        Subtitle = "War Thunder local map companion",
        Language = "Language",
        Start = "Start",
        Stop = "Stop",
        Connection = "Connection",
        Diagnostics = "Diagnostics",
        RequiredEndpoint = "required",
        OptionalEndpoint = "optional",
        EndpointOk = "ok",
        EndpointWarning = "warning",
        EndpointError = "error",
        EndpointNotChecked = "not checked",
        Map = "Map",
        ShowLabels = "Show labels",
        ShowAircraftMach = "Show aircraft Mach",
        FollowPlayer = "Follow player",
        RotateWithPlayer = "Rotate with player",
        ShowRangeRings = "Show range rings",
        ShowBattleLog = "Show battle log",
        DeliveryTracker = "Delivery tracker",
        DeliveryTrackerAngle = "Delivery tracker angle",
        ShowOverlay = "Show overlay",
        ShowMinimap = "Show minimap",
        ShowMachLabels = "Show Mach labels",
        ShowSpotRadar = "Show spot radar",
        OverlaySize = "Overlay size",
        TopOffset = "Top offset",
        RightOffset = "Right offset",
        Zoom = "Zoom",
        Minimap = "Minimap",
        AircraftScale = "Aircraft scale",
        MinimumMach = "Minimum Mach",
        SpotRadar = "Spot radar",
        ShowDistance = "Show distance",
        ShowMach = "Show Mach",
        ShowClosureSpeed = "Show closure speed",
        RadarRangeKm = "Radar range, km",
        RadarSpread = "Radar spread",
        MarkerOpacity = "Marker opacity",
        FontOpacity = "Font opacity",
        VerticalScale = "Vertical scale",
        VerticalOffset = "Vertical offset",
        ArrowScale = "Arrow scale",
        ArrowOutline = "Arrow outline",
        DistanceFontSize = "Distance font size",
        MachFontSize = "Mach font size",
        ClosureFontSize = "Closure font size",
        ArrowColor = "Arrow color",
        DistanceColor = "Distance color",
        MachColor = "Mach color",
        ClosureColor = "Closure color",
        Save = "Save",
        Reset = "Reset",
        LiveMap = "Live Map",
        LocalApiScope = "Reads only the local game API at 127.0.0.1:8111",
        BattleLog = "Battle Log",
        WaitingForWarThunder = "Waiting for War Thunder",
        ConnectedToLocalApi = "Connected to 127.0.0.1:8111",
        WaitingForLocalMapApi = "Waiting for local map API",
        WarThunderProcessNotDetected = "War Thunder process not detected",
        StartWarThunderToEnableLiveData = "Start War Thunder to enable live data",
        WaitingForMapImage = "Waiting for map.img",
        AcesDetected = "aces.exe detected",
        AcesNotDetected = "aces.exe not detected",
        NeverUpdated = "Never updated",
        PlayerNotDetected = "Player not detected",
        Player = "Player",
        Allied = "allied",
        Enemy = "enemy",
        Objects = "objects",
        Pos = "pos",
        Climb = "climb",
        Heading = "hdg",
        Updated = "Updated",
        MessageEnemy = "Enemy",
        MessageAlly = "Ally",
        MessageEvent = "Event",
        SessionStarted = "Session started",
        AircraftVisible = "aircraft visible",
        AlliedAircraftChanged = "Allied aircraft count changed",
        EnemyAircraftChanged = "Enemy aircraft count changed"
    };

    private static readonly UiText Korean = new("ko-KR")
    {
        Subtitle = "War Thunder 로컬 맵 도구",
        Language = "언어",
        Start = "시작",
        Stop = "정지",
        Connection = "연결",
        Diagnostics = "진단",
        RequiredEndpoint = "필수",
        OptionalEndpoint = "선택",
        EndpointOk = "정상",
        EndpointWarning = "경고",
        EndpointError = "오류",
        EndpointNotChecked = "미확인",
        Map = "지도",
        ShowLabels = "라벨 표시",
        ShowAircraftMach = "항공기 마하 표시",
        FollowPlayer = "플레이어 따라가기",
        RotateWithPlayer = "플레이어 방향 회전",
        ShowRangeRings = "거리 링 표시",
        ShowBattleLog = "전투 로그 표시",
        DeliveryTracker = "투하 추적기",
        DeliveryTrackerAngle = "투하 추적 각도",
        ShowOverlay = "오버레이 표시",
        ShowMinimap = "미니맵 표시",
        ShowMachLabels = "마하 라벨 표시",
        ShowSpotRadar = "스팟 레이더 표시",
        OverlaySize = "오버레이 크기",
        TopOffset = "위쪽 오프셋",
        RightOffset = "오른쪽 오프셋",
        Zoom = "확대",
        Minimap = "미니맵",
        AircraftScale = "항공기 크기",
        MinimumMach = "최소 마하",
        SpotRadar = "스팟 레이더",
        ShowDistance = "거리 표시",
        ShowMach = "마하 표시",
        ShowClosureSpeed = "접근 속도 표시",
        RadarRangeKm = "레이더 범위, km",
        RadarSpread = "레이더 간격",
        MarkerOpacity = "마커 불투명도",
        FontOpacity = "글자 불투명도",
        VerticalScale = "세로 배율",
        VerticalOffset = "세로 오프셋",
        ArrowScale = "화살표 크기",
        ArrowOutline = "화살표 외곽선",
        DistanceFontSize = "거리 글자 크기",
        MachFontSize = "마하 글자 크기",
        ClosureFontSize = "접근 속도 글자 크기",
        ArrowColor = "화살표 색상",
        DistanceColor = "거리 색상",
        MachColor = "마하 색상",
        ClosureColor = "접근 속도 색상",
        Save = "저장",
        Reset = "초기화",
        LiveMap = "라이브 맵",
        LocalApiScope = "로컬 게임 API 127.0.0.1:8111만 읽음",
        BattleLog = "전투 로그",
        WaitingForWarThunder = "War Thunder 대기 중",
        ConnectedToLocalApi = "127.0.0.1:8111 연결됨",
        WaitingForLocalMapApi = "로컬 맵 API 대기 중",
        WarThunderProcessNotDetected = "War Thunder 프로세스 감지 안 됨",
        StartWarThunderToEnableLiveData = "실시간 데이터를 보려면 War Thunder를 시작하세요",
        WaitingForMapImage = "map.img 대기 중",
        AcesDetected = "aces.exe 감지됨",
        AcesNotDetected = "aces.exe 감지 안 됨",
        NeverUpdated = "아직 갱신 안 됨",
        PlayerNotDetected = "플레이어 감지 안 됨",
        Player = "플레이어",
        Allied = "아군",
        Enemy = "적",
        Objects = "개 오브젝트",
        Pos = "좌표",
        Climb = "상승각",
        Heading = "방위",
        Updated = "갱신",
        MessageEnemy = "적",
        MessageAlly = "아군",
        MessageEvent = "이벤트",
        SessionStarted = "세션 시작",
        AircraftVisible = "항공기 표시 중",
        AlliedAircraftChanged = "아군 항공기 수 변경",
        EnemyAircraftChanged = "적 항공기 수 변경"
    };

    private UiText(string code)
    {
        Code = code;
    }

    public string Code { get; }
    public string Subtitle { get; init; } = "";
    public string Language { get; init; } = "";
    public string Start { get; init; } = "";
    public string Stop { get; init; } = "";
    public string Connection { get; init; } = "";
    public string Diagnostics { get; init; } = "";
    public string RequiredEndpoint { get; init; } = "";
    public string OptionalEndpoint { get; init; } = "";
    public string EndpointOk { get; init; } = "";
    public string EndpointWarning { get; init; } = "";
    public string EndpointError { get; init; } = "";
    public string EndpointNotChecked { get; init; } = "";
    public string Map { get; init; } = "";
    public string ShowLabels { get; init; } = "";
    public string ShowAircraftMach { get; init; } = "";
    public string FollowPlayer { get; init; } = "";
    public string RotateWithPlayer { get; init; } = "";
    public string ShowRangeRings { get; init; } = "";
    public string ShowBattleLog { get; init; } = "";
    public string DeliveryTracker { get; init; } = "";
    public string DeliveryTrackerAngle { get; init; } = "";
    public string ShowOverlay { get; init; } = "";
    public string ShowMinimap { get; init; } = "";
    public string ShowMachLabels { get; init; } = "";
    public string ShowSpotRadar { get; init; } = "";
    public string OverlaySize { get; init; } = "";
    public string TopOffset { get; init; } = "";
    public string RightOffset { get; init; } = "";
    public string Zoom { get; init; } = "";
    public string Minimap { get; init; } = "";
    public string AircraftScale { get; init; } = "";
    public string MinimumMach { get; init; } = "";
    public string SpotRadar { get; init; } = "";
    public string ShowDistance { get; init; } = "";
    public string ShowMach { get; init; } = "";
    public string ShowClosureSpeed { get; init; } = "";
    public string RadarRangeKm { get; init; } = "";
    public string RadarSpread { get; init; } = "";
    public string MarkerOpacity { get; init; } = "";
    public string FontOpacity { get; init; } = "";
    public string VerticalScale { get; init; } = "";
    public string VerticalOffset { get; init; } = "";
    public string ArrowScale { get; init; } = "";
    public string ArrowOutline { get; init; } = "";
    public string DistanceFontSize { get; init; } = "";
    public string MachFontSize { get; init; } = "";
    public string ClosureFontSize { get; init; } = "";
    public string ArrowColor { get; init; } = "";
    public string DistanceColor { get; init; } = "";
    public string MachColor { get; init; } = "";
    public string ClosureColor { get; init; } = "";
    public string Save { get; init; } = "";
    public string Reset { get; init; } = "";
    public string LiveMap { get; init; } = "";
    public string LocalApiScope { get; init; } = "";
    public string BattleLog { get; init; } = "";
    public string WaitingForWarThunder { get; init; } = "";
    public string ConnectedToLocalApi { get; init; } = "";
    public string WaitingForLocalMapApi { get; init; } = "";
    public string WarThunderProcessNotDetected { get; init; } = "";
    public string StartWarThunderToEnableLiveData { get; init; } = "";
    public string WaitingForMapImage { get; init; } = "";
    public string AcesDetected { get; init; } = "";
    public string AcesNotDetected { get; init; } = "";
    public string NeverUpdated { get; init; } = "";
    public string PlayerNotDetected { get; init; } = "";
    public string Player { get; init; } = "";
    public string Allied { get; init; } = "";
    public string Enemy { get; init; } = "";
    public string Objects { get; init; } = "";
    public string Pos { get; init; } = "";
    public string Climb { get; init; } = "";
    public string Heading { get; init; } = "";
    public string Updated { get; init; } = "";
    public string MessageEnemy { get; init; } = "";
    public string MessageAlly { get; init; } = "";
    public string MessageEvent { get; init; } = "";
    public string SessionStarted { get; init; } = "";
    public string AircraftVisible { get; init; } = "";
    public string AlliedAircraftChanged { get; init; } = "";
    public string EnemyAircraftChanged { get; init; } = "";

    public static IReadOnlyList<LanguageOption> LanguageOptions { get; } =
    [
        new("en-US", "English"),
        new("ko-KR", "한국어")
    ];

    public static UiText For(string? code)
    {
        return string.Equals(code, Korean.Code, StringComparison.OrdinalIgnoreCase)
            ? Korean
            : English;
    }

    public string FormatAllySummary(int count) => Code == Korean.Code ? $"{Allied} {count}" : $"{count} {Allied}";
    public string FormatEnemySummary(int count) => Code == Korean.Code ? $"{Enemy} {count}" : $"{count} {Enemy}";
    public string FormatObjectSummary(int count) => Code == Korean.Code ? $"{count}{Objects}" : $"{count} {Objects}";
    public string FormatUpdated(DateTimeOffset updatedAt) => $"{Updated} {updatedAt:HH:mm:ss}";
    public string FormatSessionStarted(int ally, int enemy) => $"{SessionStarted}: {ally} {Allied}, {enemy} {Enemy} {AircraftVisible}.";
    public string FormatAlliedAircraftChanged(int oldCount, int newCount) => $"{AlliedAircraftChanged}: {oldCount} -> {newCount}.";
    public string FormatEnemyAircraftChanged(int oldCount, int newCount) => $"{EnemyAircraftChanged}: {oldCount} -> {newCount}.";

    public string FormatEndpointHealth(EndpointHealth health)
    {
        var scope = health.Required ? RequiredEndpoint : OptionalEndpoint;
        var state = health.State switch
        {
            EndpointHealthState.Ok => EndpointOk,
            EndpointHealthState.Warning => EndpointWarning,
            EndpointHealthState.Error => EndpointError,
            EndpointHealthState.NotChecked => EndpointNotChecked,
            _ => health.State.ToString()
        };

        return $"{health.Path} [{scope}] {state} - {health.Detail}";
    }

    public string FormatStatus(string status)
    {
        return status switch
        {
            "Waiting for War Thunder" => WaitingForWarThunder,
            "Connected to 127.0.0.1:8111" => ConnectedToLocalApi,
            "War Thunder process not detected" => WarThunderProcessNotDetected,
            _ when status.StartsWith("Waiting for local map API: ", StringComparison.Ordinal) =>
                $"{WaitingForLocalMapApi}: {status["Waiting for local map API: ".Length..]}",
            _ => status
        };
    }
}
