using RobotClientWpf.Utilities;
using System.DirectoryServices.ActiveDirectory;
using System.Windows.Controls;
using System.Windows.Input;

namespace RobotClientWpf.Views
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : UserControl
    {
        private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        private ChallengeFactory? challenge;
        private const int AMOUNT_SPEED_ADDED_PER_CLICK = 500;
        private const int TURN_ANGLE_PER_CLICK = 30;

        public MainView()
        {
            InitializeComponent();
        }

        public void SetChallengeFactory(ChallengeFactory challenge)
        {
            this.challenge = challenge;
            // subscribe to events
            this.challenge.RobotStationary.SplitFlap.SplitFlapDisplayChanged += this.SplitFlap_SplitFlapDisplayChanged;
        }

        public void SplitFlap_SplitFlapDisplayChanged(object? sender, RobotLib.SplitFlap.SplitFlapDisplayEventArgs e)
        {
            UIAccessHelpers.SetTextboxText(this.tbSplitflapText, e.DisplayMessage);
        }

        private void btnModeAuto_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.SetMobilityMode(true);
        }

        private void btnModeManual_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.SetMobilityMode(false);
        }

        private void btnRoboFwd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.AddSpeed(AMOUNT_SPEED_ADDED_PER_CLICK);
        }
        private void btnRoboBwd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.AddSpeed(-AMOUNT_SPEED_ADDED_PER_CLICK);
        }

        private void btnRoboRight_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.Turn(TURN_ANGLE_PER_CLICK);
        }

        private void btnRoboLeft_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.challenge?.RobotMobile.Movement.Turn(-TURN_ANGLE_PER_CLICK);
        }

        private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                this.challenge?.RobotMobile.Movement.AddSpeed(AMOUNT_SPEED_ADDED_PER_CLICK); 
                e.Handled = true;
            }
            else if (e.Key == Key.Down)
            {
                this.challenge?.RobotMobile.Movement.AddSpeed(-AMOUNT_SPEED_ADDED_PER_CLICK);
                e.Handled = true;
            }
            else if (e.Key == Key.Left)
            {
                this.challenge?.RobotMobile.Movement.Turn(-TURN_ANGLE_PER_CLICK);
                e.Handled = true;
            }
            else if (e.Key == Key.Right)
            {
                this.challenge?.RobotMobile.Movement.Turn(TURN_ANGLE_PER_CLICK);
                e.Handled = true;
            }
            else if(e.Key == Key.End)
            {
                this.challenge?.RobotMobile.Movement.Stop();
                e.Handled = true;
            }
        }
    }
}
