﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
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 Quobject.SocketIoClientDotNet.Client;
using Newtonsoft.Json;
using WalkingMicrosoft.Core;
using Windows.Devices.Geolocation;
using WalkingMicrosoft.Core.Entities;
using WalkingMicrosoft.Core.Enums;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

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

namespace WalkingMicrosoft
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainExperiments : Page
    {
        

        public MainExperiments()
        {
            this.InitializeComponent();

            App.OnSocketEvent += App_OnSocketEvent;
        }

        void App_OnSocketEvent(object sender, object e)
        {

            switch ((String)(e.GetType().GetRuntimeProperty("Command").GetValue(e, null)))
            {
                case "EVENT_CONNECT":
                    UpdateStatus((string)(e.GetType().GetRuntimeProperty("Data").GetValue(e, null)));
                    break;
                case "EVENT_DISCONNECT":
                    UpdateStatus((string)(e.GetType().GetRuntimeProperty("Data").GetValue(e, null)));
                    break;
                case "EVENT_RECONNECTING":
                    UpdateStatus((string)(e.GetType().GetRuntimeProperty("Data").GetValue(e, null)));
                    break;
                case "EVENT_RECONNECT_FAILED":
                    UpdateStatus((string)(e.GetType().GetRuntimeProperty("Data").GetValue(e, null)));
                    break;
                case "Welcome":
                    WelcomeStatus((object)(e.GetType().GetRuntimeProperty("Data").GetValue(e, null)));
                    break;
                default:
                    break;
            }
        }
        
        private void UpdateStatus(string status)
        {
            this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                SocketStatusTextBlock.Text = status;
            });            
        }

        private void WelcomeStatus(object data)
        {
            Welcome welcomeData = JsonConvert.DeserializeObject<Welcome>(data.ToString());

            this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                SocketStatusTextBlock.Text = "Status: " + welcomeData.SocketId;
            });  
            
        }
        

        private void ShowPositionsButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            #if WINDOWS_PHONE_APP
            this.Frame.Navigate(typeof(WalkingMicrosoft.Experiments.ShowingPositions));
            #endif
            //socket.Emit("GetPositions");
        }

        private async void SendPositionButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            Geolocator geo = new Geolocator();
            Geoposition myPosition = await geo.GetGeopositionAsync();

            int eventType = EventTypeCombo.SelectedIndex;

            Position position = new Position
            {
                Id = Guid.NewGuid(),
                Latitude = myPosition.Coordinate.Point.Position.Latitude,
                Longitude = myPosition.Coordinate.Point.Position.Longitude,
                EventType = (EventType)eventType
            };
            var objSerialized = JsonConvert.SerializeObject(position);
            App.socket.Emit("PushPosition", objSerialized);
        }

        private void ShowBackendLogsButton_Click(object sender, RoutedEventArgs e)
        {
            App.socket.Emit("ShowBackendPositionLogs");
        }
    }
}
