using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Input.Touch;
using System.Threading;
using System.Windows.Threading;

namespace WPTempToDelete
{
    public partial class MainPage : PhoneApplicationPage
    {
        DispatcherTimer dt;
        int cnt = 0;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            TouchPanel.EnabledGestures = GestureType.Hold | GestureType.Tap;

        }

        public void StartHoldTimer()
        {
            dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(0, 0, 0, 0, 500);
            dt.Tick += dt_Tick;
            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
            if (cnt==3)
            {
                cnt = 0;
                GetGesture();
                TouchPanel.EnabledGestures = GestureType.None;
                StopHoldTimer();
            }
            cnt++;
        }

        private void StopHoldTimer()
        {
            dt.Stop();
            cnt = 0;
        }



        private void GetGesture()
        {
                if(TouchPanel.IsGestureAvailable)
                {
                    GestureSample sample = TouchPanel.ReadGesture();
                    switch (sample.GestureType)
                    {
                        case GestureType.DoubleTap:
                            break;
                        case GestureType.DragComplete:
                            break;
                        case GestureType.Flick:
                            break;
                        case GestureType.FreeDrag:
                            Dispatcher.BeginInvoke(() => { tbGestures.Text = "Drag Geste"; });
                            break;
                        case GestureType.Hold:
                            break;
                        case GestureType.HorizontalDrag:
                            break;
                        case GestureType.None:
                            break;
                        case GestureType.Pinch:
                            var vector = sample.Position;
                            break;
                        case GestureType.PinchComplete:
                            break;
                        case GestureType.Tap:
                            Dispatcher.BeginInvoke(() => { tbGestures.Text = "Tap Geste"; });
                            break;
                        case GestureType.VerticalDrag:
                            break;
                        default:
                            Dispatcher.BeginInvoke(() => { tbGestures.Text = "Keine Geste"; });
                            break;
                    }
                } 
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }

        private void PhoneApplicationPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            TouchPanel.EnabledGestures = GestureType.Hold | GestureType.Tap;
            StartHoldTimer();
        }

        private void PhoneApplicationPage_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            TouchPanel.EnabledGestures = GestureType.None;
            StopHoldTimer();
        }

    }
}
