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

namespace FLY.Thick.FilmCasting.UI.Server
{
    /// <summary>
    /// PgMain.xaml 的交互逻辑
    /// </summary>
    public partial class PgMain : Page
    {
        PgMainVm viewModel;
        public PgMain()
        {
            InitializeComponent();
        }

        [InjectionMethod]
        public void Init(IUnityContainer container)
        {
            viewModel = new PgMainVm();
            container.BuildUp(viewModel);
            this.DataContext = viewModel;

            container.BuildUp(ucAllErrNos);
        }
    }
    public class PgMainVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public SysParam SysParam { get; set; }
        public FlyAD7 FlyAd { get; set; }
        public ThkWebApiServer WebApiServer => thkWebApiServer;

        public RelayCommand AddrSetupCmd { get; private set; }

        public RelayCommand OpenLinkCmd { get; private set; }
        public RelayCommand RebootCmd { get; private set; }

        ThkWebApiServer thkWebApiServer;
        IUnityContainer container;
        public PgMainVm()
        {
            AddrSetupCmd = new RelayCommand(AddrSetup);
            OpenLinkCmd = new RelayCommand(OpenLink);
            RebootCmd = new RelayCommand(Reboot);
        }

        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            TDGage mGage,
            ThkWebApiServer thkWebApiServer)
        {
            this.container = container;

            SysParam = mGage.sysParam;
            FlyAd = mGage.flyAd;
            this.thkWebApiServer = thkWebApiServer;
        }

        private void AddrSetup()
        {
            //打开设置页面

            WdSetup w = new WdSetup();
            container.BuildUp(w);
            w.Owner = App.Current.MainWindow;
            w.ShowDialog();
        }
        private void OpenLink()
        {
            System.Diagnostics.Process.Start($"http://{thkWebApiServer.ServerAddr}/swagger/ui/index");
        }

        private void Reboot()
        {
            string msg = (string)Application.Current.FindResource("str.WdSetup.IsSureToReboot");


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

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

    }
}
