using System;
using System.Reactive;
using Avalonia.Controls;
using Avalonia.Threading;
using detect.gui.Api;
using detect.gui.Models;
using detect.gui.VWS;
using ReactiveUI;
using Serilog;
using Splat;
using WebViewControl;
using xwd.utils;

namespace detect.gui.VWMS;

public class MainWindowModel : ViewModelBase<MainWindowModel>
{
    /// <summary>
    /// 当前登录用户
    /// </summary>
    private UserModel? _currentUser;

    public UserModel? CurrentUser
    {
        get => _currentUser;
        set => this.RaiseAndSetIfChanged(ref _currentUser, value);
    }

    private bool _isHomeView;

    public bool IsHomeView
    {
        get => _isHomeView;
        set => this.RaiseAndSetIfChanged(ref _isHomeView, value);
    }

    private bool _isDetectTaskView;

    public bool IsDetectTaskView
    {
        get => _isDetectTaskView;
        set => this.RaiseAndSetIfChanged(ref _isDetectTaskView, value);
    }

    private bool _isDeviceView;

    public bool IsDeviceView
    {
        get => _isDeviceView;
        set => this.RaiseAndSetIfChanged(ref _isDeviceView, value);
    }

    private bool _isLogView;

    public bool IsLogView
    {
        get => _isLogView;
        set => this.RaiseAndSetIfChanged(ref _isLogView, value);
    }

    private bool _isUserView;

    public bool IsUserView
    {
        get => _isUserView;
        set => this.RaiseAndSetIfChanged(ref _isUserView, value);
    }

    private readonly string[] _localRoutes =
    [
        "#/dashboard",
        "#/data/task",
        "#/data/device",
        "#/system/log",
        "#/system/user",
        ""
    ];

    private string? _address;

    public string? Address
    {
        get => _address;
        set => this.RaiseAndSetIfChanged(ref _address, value);
    }

    public ReactiveCommand<string, Unit> GoCommand { get; }
    public ReactiveCommand<Unit, Unit> LogoutCommand { get; }

    public MainWindowModel()
    {
        ApiService.Instance().StartService();

        GoCommand = ReactiveCommand.Create<string>( p => SetAddress(int.Parse(p)));
        LogoutCommand = ReactiveCommand.Create(Logout);
    }

    public void Dispose()
    {
        ApiService.Instance().StopService();
    }

    private void Logout()
    {
        CurrentUser = null;
        SetAddress(5);
        Dispatcher.UIThread.InvokeAsync(() =>
        {
            var webView = Locator.Current.GetService<MainWindow>()!.Get<WebView>("WebView");
            webView.IsVisible = false;
        });
    }

    public void SetAddress(int index)
    {
        if (index < 4)
            Locator.Current.GetService<MainWindowModel>()!.IsLoading = true;
        var isOnLine = AppSettingsManager.Manager().GetBool("Embedded.IsOnline");
        var path = AppSettingsManager.Manager().GetString("Embedded.Path", "./dist/index.html");
        var prefix = isOnLine ? path : AppDomain.CurrentDomain.BaseDirectory + path;
        Address = $"{prefix}{_localRoutes[index]}";
        Log.Information("current address: {address}", Address);
    }
}