using Plane.CopterManagement;
using Plane.Messaging;

namespace Plane.CopterControllers
{
    public class FlyToController : CopterControllerBase
    {
        public FlyToController(IPLMessenger messenger, ICopterManager copterManager) : base(messenger, copterManager)
        {
        }

        public override bool IsEnabled
        {
            get
            {
                return base.IsEnabled;
            }

            set
            {
                base.IsEnabled = value;
                if (value)
                {
                    _messenger.Register<RequestFlyToMessage>(this, OnRequestFlyTo);
                }
                else
                {
                    _messenger.Unregister(this);
                }
            }
        }

        public override CopterControllerTypes Type
        {
            get
            {
                return CopterControllerTypes.FlyToController;
            }
        }

        private async void OnRequestFlyTo(RequestFlyToMessage m)
        {
            var accepted = await _copterManager.CheckStatusAndFlyToAsync(m.Latitude, m.Longitude);
            if (accepted)
            {
                _messenger.Send(new FlyToRequestAcceptedMessage(m));
            }
        }
    }
}
