using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using FLY.Simulation.Flyad7;

namespace FLYAD7_Simulation_Wpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        FlyADClientUI flyadclient_ui;

        
        public int OneSecCnt { get; set; }

        FLYAD7 flyad7;
        FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy flyad7objproxy;
        bool bClosed = false;
        public MainWindow()
        {
            InitializeComponent();
            FObjBase.PollModule.Current.IsAccurate = true;


            flyad7 = new FLYAD7();
            flyad7.Init();

            flyad7objproxy = new FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy(0, flyad7);
            flyad7objproxy.Listen();

            flyadclient_ui = new FlyADClientUI(flyad7);

            this.DataContext = flyadclient_ui;
            this.groupbox_ip.DataContext = flyad7;

            groupBox4.DataContext = this;

            comboBox1.DataContext = flyad7;

            Thread t = new Thread(OnPoll);
            t.IsBackground = true;
            t.Start();
            //FObjBase.PollModule.Current.Start();
        }

        DateTime dt_cnt = DateTime.MinValue;
        int onesecondcnt = 0;
        TimeSpan ts_1scnt = TimeSpan.Zero;
        void Update1sCnt() 
        {
            DateTime d = DateTime.Now;
            if (dt_cnt == DateTime.MinValue)
                dt_cnt = d;
            else
            {
                onesecondcnt++;
                TimeSpan ts = (d - dt_cnt);
                if (ts.TotalMilliseconds >= 1.28)
                {
                    //OneSecCnt = (int)(d - dt_cnt).TotalMilliseconds;// onesecondcnt;
                    OneSecCnt = onesecondcnt;
                    onesecondcnt = 0;
                    dt_cnt = d;
                }
                else 
                {
                    ts_1scnt = d - dt_cnt;
                }
                
            }
        }
        void OnPoll()
        {

            while (true) 
            {
                if (bClosed)
                    return;
                timer_Tick(null, null);
                //Update1sCnt();
                Thread.Sleep(0);
            }
        }


        void timer_Tick(object sender, EventArgs e)
        {
            
            FObjBase.PollModule.Current.OnPoll();

        }

        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void Window_Closed(object sender, EventArgs e)
        {
           bClosed = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (flyad7.mSimGageAD is FLY.Simulation.Coating.GageAD)
            {
                WindowCoating wb = new WindowCoating();
                wb.Init((flyad7.mSimGageAD as FLY.Simulation.Coating.GageAD).mCoating);
                wb.ShowDialog();
            }
            else if (flyad7.mSimGageAD is FLY.Simulation.HeaderAndTailer.GageAD)
            {
                WindowHeaderAndTailer wb = new WindowHeaderAndTailer();
                wb.Init((flyad7.mSimGageAD as FLY.Simulation.HeaderAndTailer.GageAD));
                wb.ShowDialog();
            }
            else if(flyad7.mSimGageAD is FLY.Simulation.Blowing.GageAD)
            {
                WindowBlowing wb = new WindowBlowing();
                wb.mBlowing = (flyad7.mSimGageAD as FLY.Simulation.Blowing.GageAD).mBlowing;
                wb.ShowDialog();
            }
            else if (flyad7.mSimGageAD is FLY.Simulation.Casting.GageAD)
            {
                MessageBox.Show("不支持");
            }
            else 
            {
                MessageBox.Show("不支持");
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            flyad7.Save();
            MessageBox.Show("保存成功，请重启");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Window_SyncList w = new Window_SyncList();
            w.Init(flyad7);
            w.Show();
        }
    }
    public class IO2BinConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            UInt16 io = (UInt16)value;
            string str = System.Convert.ToString(io, 2);

            return str.PadLeft(12, '0');
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return 0xffff;

            else 
            {
                string str = value as string;
                UInt16 io;
                if (UInt16.TryParse(str, out io))
                    return io;
                else
                    return 0xffff;
            }

        }

        #endregion
    }


    public class EnumToIsCheckedConverter<TEnum> : IValueConverter
    where TEnum : struct
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TEnum type = (TEnum)value;
            if (Enum.GetName(typeof(TEnum), type) == (parameter as string))
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value == true)
            {
                return Enum.Parse(typeof(TEnum), (parameter as string));
            }
            else
            {
                return null;
            }
        }

        #endregion
    }



    public class SimModeConverter : EnumToIsCheckedConverter<FLY.Simulation.Flyad7.FLYAD7.SIM_MODE>
    {

    }
}
