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

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

        DispatcherTimer timer;
        FlyAD7 flyad;


        public bool FB_Enable { get; set; }
        public int FB_Pos1 { get; set; } = 1000;

        public int FB_Pos2 { get; set; } = 3000;

        public AutoForwBackw(FlyAD7 flyad)
        {
            this.flyad = flyad;
            FB_init();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            if (!flyad.IsConnected)
            {
                FB_Enable = false;
            }
            if (!flyad.IsFinish)
                return;
            if (flyad.IsTimeToPushTimeGridAdv)//推送完成
                return;


            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 = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_Tick);
            FB_Enable = false;
            this.PropertyChanged += AutoForwBackw_PropertyChanged;
        }

        private void AutoForwBackw_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(FB_Enable))
            {
                if (FB_Enable)
                {
                    timer.Start();
                }
                else
                {
                    timer.Stop();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
