using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FlyADBase;
using Misc;

namespace Flyad7_WPF
{
    /// <summary>
    /// 来回走
    /// </summary>
    public class AutoForwBackw : INotifyPropertyChanged
    {

        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        FlyAD7 flyad;

        private bool fbenable = false;
        public bool FB_Enable
        {
            get { return fbenable; }
            set
            {
                if (fbenable != value)
                {
                    fbenable = value;
                    PropertyChanged.Notify(this, "FB_Enable");
                }
            }
        }
        private int fb_pos1 = 1000;
        private int fb_pos2 = 3000;
        public int FB_Pos1
        {
            get { return fb_pos1; }
            set
            {
                if (fb_pos1 != value)
                {
                    fb_pos1 = value;
                    PropertyChanged.Notify(this, "FB_Pos1");
                }
            }
        }
        public int FB_Pos2
        {
            get { return fb_pos2; }
            set
            {
                if (fb_pos2 != value)
                {
                    fb_pos2 = value;
                    PropertyChanged.Notify(this, "FB_Pos2");
                }
            }
        }

        public AutoForwBackw(FlyAD7 flyad)
        {
            this.flyad = flyad;
            FB_init();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (!flyad.IsConnected)
            {
                FB_Enable = false;
            }

            if (flyad.DriveStatus != DRIVE_MAN_STATUS.RUNNING)
            {
                if (Math.Abs(FB_Pos1 - flyad.Position) > Math.Abs(FB_Pos2 - flyad.Position))
                {
                    flyad.Runto(FB_Pos1);
                }
                else
                {
                    flyad.Runto(FB_Pos2);
                }
            }

        }
        void FB_init()
        {
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            FB_Enable = false;
            this.PropertyChanged += new PropertyChangedEventHandler(MainWindow_PropertyChanged);
        }

        void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FB_Enable")
            {
                if (FB_Enable)
                {
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}