using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System.Threading;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using DateTimeSettings.ViewModel;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace DateTimeSettings
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        MainViewModel mvm = new MainViewModel();
        TimeSpan ts = new TimeSpan(0, 0, 1);

        public MainPage()
        {
            this.InitializeComponent();
            //c1ClockGauge1.Time = DateTime.Now.TimeOfDay;
            //C1.Xaml.Calendar.C1Calendar ccc = new C1.Xaml.Calendar.C1Calendar();
            this.DataContext = mvm;

            if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled)
            {
                Task.Run(new Action(RunClock));
            }
            
        }

        private delegate void UpdateUIDelegate();
        private ThreadPoolTimer _timer;

        private void RunClock()
        {
            _timer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler((target) =>
            {
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, UpdateClock);

            }),
                TimeSpan.FromSeconds(1));

        }

        private void UpdateClock()
        {
            mvm.MyTime = mvm.MyTime.Add(ts);
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void c1Calendar1_SelectedDateChanged(object sender, C1.Xaml.Calendar.DateChangedEventArgs e)
        {
            
        }
    }
}
