using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using StartServerWPF.Models;
using StartServerWPF.Modules.Main.models;
using StartServerWPF.Modules.Main.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Principal;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace StartServerWPF.Modules.Main.ViewModels
{
    public class LoginViewDialogViewModel : BindableBase, IDialogAware
    {
        public LoginViewDialogViewModel(WebsocketClient websocketClient)
        {
            this._websocketClient = websocketClient;
        }
        private string _title = "登录";
        public string Title => _title;
        private string account = "admin";

        public string Account
        {
            get { return account; }
            set { SetProperty(ref account, value); }
        }
        private string password = "123";

        public string Password
        {
            get { return password; }
            set { SetProperty(ref password, value); }
        }
        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed() { }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            //  _title = "编辑" + _title;
            var _type = parameters.GetValue<int>("type");
            // _title = (_type == 0 ? "新增" : "修改") + _title;
        }

        private VpnInfo _mainModel = new VpnInfo();
        public VpnInfo MainModel
        {
            get => _mainModel;
            set { SetProperty(ref _mainModel, value); }
        }
        private DateTime _moniTime ;
        private readonly WebsocketClient _websocketClient;

        public DateTime MoniTime
        {
            get => _moniTime;
            set { SetProperty(ref _moniTime, value); }
        }
        public DelegateCommand LoginCommand => new(() => {
            OnShowLoading(new LoadingPayload { IsShow = true });
            _websocketClient.SiginServer(Account, Password);
        });
        public  void CloseDialog(DialogResult dialogResult)
        {
            RequestClose?.Invoke(dialogResult);
        }
        public ICommand CancelCommand
        {
            get => new DelegateCommand(() =>
            {
                RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
            });
        }

        private string isLoadingVisibile = "Hidden";

        public string IsLoadingVisibile
        {
            get { return isLoadingVisibile; }
            set { SetProperty(ref isLoadingVisibile, value); }
        }

        private string _loadingMessage;

        public string LoadingMessage
        {
            get { return _loadingMessage; }
            set { SetProperty(ref _loadingMessage, value); }
        }

        public void OnShowLoading(LoadingPayload msg)
        {
            this.IsLoadingVisibile = msg.IsShow ? "Visible" : "Hidden";
            this.LoadingMessage = msg.Message;
        }
    }
}
