using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Server;
using FLY.Thick.FilmCasting.Server;
using FLY.Thick.FilmCasting.WebAPI;
using FlyADBase;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Unity;
using InitParam = FLY.Thick.FilmCasting.Server.InitParam;
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();
        }

    }

    public class ConnectionValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? b = (bool?)value;
            if (b == null)
                return null;
            else if (b == true)
                return "连接成功";
            else
                return "连接断开";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class IO2BinConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is UInt16))
                return null;
            int number = 16;
            if (parameter is string)
            {
                if (int.TryParse((string)parameter, out number))
                {

                }

                if (number > 16)
                    number = 16;
                else if (number < 4)
                    number = 4;
            }

            UInt16 io = (UInt16)value;
            string str = "";
            for (int i = 0; i < 4; i++)
            {
                if (i * 4 >= number)
                    break;
                byte b = (byte)((io >> (i * 4)) & 0x0f);
                string str_b = System.Convert.ToString(b, 2);
                if (string.IsNullOrEmpty(str))
                {
                    str = str_b.PadLeft(4, '0');
                }
                else
                {
                    str = str_b.PadLeft(4, '0') + " " + str;
                }
            }
            return str;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToUInt16((string)value, 2);
        }

        #endregion
    }
}
