using CommunityToolkit.Mvvm.Input;
using FLY.Thick.FilmCasting.Server;
using FLY.Thick.FilmCasting.WebAPI;
using FlyADBase;
using System.ComponentModel;
using System.Windows;
using Unity;

namespace FLY.Thick.FilmCasting.UI.Server
{
    /// <summary>
    /// WdSetup.xaml 的交互逻辑
    /// </summary>
    public partial class WdSetup : Window
    {
        WdSetupVm viewModel;
        public WdSetup()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(IUnityContainer container)
        {
            viewModel = new WdSetupVm();
            container.BuildUp(viewModel);
            this.DataContext = viewModel;
        }
    }
    public class WdSetupVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string Addr { get; set; }
        public string FlyAdAddr { get; set; }
        public string WebApiAddr { get; set; }
        public string WsAddr { get; set; }

        public RelayCommand OkCmd { get; private set; }

        SysParam sysParam;
        FlyAD7 flyAd;
        ThkWebApiServer thkWebApiServer;
        

        public WdSetupVm()
        {
            OkCmd = new RelayCommand(Ok);
        }


        [InjectionMethod]
        public void Init(SysParam sysParam, FlyAD7 flyAd, ThkWebApiServer thkWebApiServer)
        {
            this.sysParam = sysParam;
            this.flyAd = flyAd;
            this.thkWebApiServer = thkWebApiServer;
            
            Addr = sysParam.Addr;
            FlyAdAddr = flyAd.Addr;
            WebApiAddr = thkWebApiServer.ServerAddr;
            WsAddr = sysParam.WsAddr;
        }
        private void Ok()
        {
            string msg = (string)Application.Current.FindResource("str.WdSetup.IsSureToReboot");

            if (MessageBox.Show(msg, null, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
            {
                return;
            }

            sysParam.Addr = Addr;
            sysParam.WsAddr = WsAddr;
            sysParam.Save();

            flyAd.Addr = FlyAdAddr;
            flyAd.Save();

            thkWebApiServer.ServerAddr = WebApiAddr;
            thkWebApiServer.Save();


            System.Environment.CurrentDirectory = "../";
            AppHelper.AppJustOne.Restart();
        }
    }
}
