private async void CheckUserAccountStatus()
        {
            try
            {
                var result = await ProxyTracker.GetInstance().Client.IsUserRegisteredAsync(ProxyTracker.GetInstance().GetDeviceId().ToString());

                if (result == true)
                {
                    var activated = await ProxyTracker.GetInstance().Client.IsUserActivatedAsync(ProxyTracker.GetInstance().GetDeviceId().ToString());

                    if (activated == true)
                    {
                        this.Frame.Navigate(typeof(TrackMap));
                    }
                    else
                    {
                        this.Frame.Navigate(typeof(Registration));
                    }
                }
                else
                {
                    this.Frame.Navigate(typeof(Registration));
                }
            }
            catch (Exception ex) {
                SetMessage(MessageType.Error, "❎ Sorry, we couldnt process your request at this time. Please check your internet connection or try again later");
                btnRetry.Visibility = Visibility.Visible;
            }
        }
        private async void btnSendCompleteRegistration_Click(object sender, RoutedEventArgs e)
        {
            btnCompleteRegistration.IsEnabled = false;
            try
            {
                if (txtActivationCode.Text.Trim() != "")
                {
                    txtMessage.Text       = "Finalizing Registration process...";
                    txtMessage.Foreground = new SolidColorBrush(Colors.Black);
                    var result = await ProxyTracker.GetInstance().Client.UpdateIsActivatedAsync(deviceId, txtActivationCode.Text.Trim());

                    if (result == true)
                    {
                        SetMessage(MessageType.Information, "✔ Thank you for completing your registration");
                        this.Frame.Navigate(typeof(TrackMap));
                    }
                    else
                    {
                        SetMessage(MessageType.Error, "❎ Please enter valid activation code");
                    }
                }
                else
                {
                    SetMessage(MessageType.Error, "❎ Please enter valid activation code");
                }
            }
            catch (Exception)
            {
                SetMessage(MessageType.Error, "❎ Sorry, we couldnt process your request at this time. Please check your internet connection or try again later");
            }
            btnCompleteRegistration.IsEnabled = true;
        }
        async void timer_TickFetch(object sender, object e)
        {
            try
            {
                long trackId = 0;

                if (txtTrackId.Text != "" && btnTrack.Content.ToString().Equals("Cancel"))
                {
                    if (Int64.TryParse(txtTrackId.Text, out trackId))
                    {
                        var res = await ProxyTracker.GetInstance().Client.GetTrackingInfoAsync(Convert.ToInt64(txtTrackId.Text));

                        if (res != null)
                        {
                            SetUserTrackCurrentLocation(res.Latitude, res.Longitude);
                        }
                        else
                        {
                            SetMessage(MessageType.Error, "❎ Sorry, there is no location associated with the user's TrackViewer ID");
                            btnTrack_Click(sender, null);
                        }
                    }
                    else
                    {
                        SetMessage(MessageType.Error, "❎ TrackViewer ID entered is invalid");
                        btnTrack_Click(sender, null);
                    }
                }
            }
            catch
            {
                SetMessage(MessageType.Error, "❎ Sorry, there is no location associated with the user's TrackViewer ID");
                btnTrack_Click(sender, null);
            }
        }
        async void CheckIfUserActivated()
        {
            try
            {
                var registrationStatus = await ProxyTracker.GetInstance().Client.IsUserRegisteredAsync(deviceId);

                if (registrationStatus == true)
                {
                    var activationStatus = await ProxyTracker.GetInstance().Client.IsUserActivatedAsync(deviceId);

                    if (activationStatus == false)
                    {
                        txtName.IsEnabled = false;
                        // txtEmailAddress.IsEnabled = false;
                        btnSendActivation.Content = "Resend Code";
                        // txtActivationCode.IsEnabled = true;
                        btnCompleteRegistration.IsEnabled = true;

                        Task <TrackViewerUser> user = ProxyTracker.GetInstance().Client.GetUserInfoAsync(deviceId);
                        txtName.Text         = user.Result.Name;
                        txtEmailAddress.Text = user.Result.Email;
                    }
                }
            }
            catch (Exception ex)
            {
                SetMessage(MessageType.Warning, "Some error occured in connectivity, trying again...");
                CheckIfUserActivated();
            }
        }
        private async void btnDeactivate_Click(object sender, RoutedEventArgs e)
        {
            if (!ProxyTracker.GetInstance().IsTestAccount)
            {
                btnDeactivate.IsEnabled = false;
                await ProxyTracker.GetInstance().Client.DeactivateUserAccountAsync(ProxyTracker.GetInstance().GetDeviceId());

                btnDeactivate.IsEnabled = true;
                Frame rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(Splash));
                RemoveDeactivateAccountMenu();
            }
            else
            {
                txtMessage.Text = "Its a test account, you cannot perform this operation";
            }
        }
        async void timer_Tick(object sender, object e)
        {
            try
            {
                if (toggleSwitch.IsOn)
                {
                    Geoposition pos = await _geolocator.GetGeopositionAsync().AsTask(token);

                    Location location = new Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
                    ProxyTracker.GetInstance().MyTrackLocation = new Services.TrackService.TrackLocation {
                        Latitude = location.Latitude, Longitude = location.Longitude
                    };
                    await ProxyTracker.GetInstance().Client.PublishTrackingInfoAsync(ProxyTracker.GetInstance().MyTrackId, ProxyTracker.GetInstance().MyTrackLocation);
                }

                if (!btnTrack.Content.ToString().Equals("Cancel"))
                {
                    await SetCurrentLocation();

                    //        txtMessage.Text = "";
                }
            }
            catch { }
        }
 public static ProxyTracker GetInstance()
 {
     lock (_lock) {
         return(_instance = (_instance == null) ? new ProxyTracker() : _instance);
     }
 }
 public Registration()
 {
     this.InitializeComponent();
     deviceId = ProxyTracker.GetInstance().GetDeviceId().ToString();
     CheckIfUserActivated();
 }
        private async void btnSendActivation_Click_1(object sender, RoutedEventArgs e)
        {
            //For testing purpose
            if (txtName.Text.Equals("test"))
            {
                var result = ProxyTracker.GetInstance().Client.GetUserInfoAsync("test").Result;
                if (result != null && result.Email == "test")
                {
                    ProxyTracker.GetInstance().IsTestAccount = true;
                    this.Frame.Navigate(typeof(TrackMap));
                }
            }


            if (txtName.Text.Trim() != "" && txtEmailAddress.Text.Trim() != "")
            {
                if (!Regex.IsMatch(txtEmailAddress.Text.Trim(), "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))
                {
                    SetMessage(MessageType.Error, "❎ Email Address is not valid, please enter valid email address");
                    btnSendActivation.IsEnabled = true;
                    return;
                }
            }
            else
            {
                SetMessage(MessageType.Error, "❎ Name and Email Address cannot be empty, please enter correct information");
                btnSendActivation.IsEnabled = true;
                return;
            }
            btnSendActivation.IsEnabled = false;
            if (btnSendActivation.Content.ToString().Equals("Register"))
            {
                txtMessage.Text       = "Registering...";
                txtMessage.Foreground = new SolidColorBrush(Colors.Black);
                Task <long> result = ProxyTracker.GetInstance().Client.RegisterUserAsync(ProxyTracker.GetInstance().GetDeviceId(), "", txtName.Text.Trim(), txtEmailAddress.Text.Trim());
                if (result.Result > 0)
                {
                    SetMessage(MessageType.Information, "✔ Thankyou for registering, your activation code has been sent to your email address");
                    txtName.IsEnabled                 = false;
                    btnSendActivation.Content         = "Resend Code";
                    btnCompleteRegistration.IsEnabled = true;
                }
                else
                {
                    SetMessage(MessageType.Error, "❎ Sorry, we couldnt process your request at this time. Please check your internet connection or try again later");
                }
            }
            else
            {
                try
                {
                    txtMessage.Text       = "Resending activation code...";
                    txtMessage.Foreground = new SolidColorBrush(Colors.Black);
                    await ProxyTracker.GetInstance().Client.ResendCodeAsync(deviceId, txtEmailAddress.Text.Trim());

                    SetMessage(MessageType.Information, "✔ Activation code has been sent to your email address");
                }
                catch (Exception ex) {
                    SetMessage(MessageType.Error, "❎ Sorry, we couldnt process your request at this time. Please check your internet connection or try again later");
                }
            }
            btnSendActivation.IsEnabled = true;
        }
        async void MapCurrentLocation()
        {
            try
            {
                // Remove any previous location icon.
                if (trvMap.Children.Count > 0)
                {
                    trvMap.Children.RemoveAt(0);
                }

                try
                {
                    await SetCurrentLocation();

                    try
                    {
                        long trackNo = await ProxyTracker.GetInstance().Client.StartTrackingAsync(ProxyTracker.GetInstance().GetDeviceId(), ProxyTracker.GetInstance().MyTrackLocation);

                        Task <TrackViewerUser> trackUser = ProxyTracker.GetInstance().Client.GetUserInfoAsync(ProxyTracker.GetInstance().GetDeviceId());
                        ProxyTracker.GetInstance().Name  = trackUser.Result.Name;
                        txtWelcome.Text   = "Welcome " + ProxyTracker.GetInstance().Name + ", you're connected!";
                        txtMyTrackId.Text = "Your TrackViewer ID is " + trackNo.ToString();
                        EnableControls();



                        ProxyTracker.GetInstance().MyTrackId = Convert.ToInt64(trackNo);

                        PostTrackingInfo();
                        FetchTrackingInfo();

                        //var trackLocation = ProxyTracker.GetInstance().MyTrackLocation;
                        //SetPushPin("↓", new Location { Latitude = trackLocation.Latitude, Longitude = trackLocation.Longitude });
                    }
                    catch (Exception ex)  { MapCurrentLocation(); }

                    // Display the location information in the textboxes.
                    //   LatitudeTextbox.Text = pos.Coordinate.Latitude.ToString();
                    //  LongitudeTextbox.Text = pos.Coordinate.Longitude.ToString();
                    //  AccuracyTextbox.Text = pos.Coordinate.Accuracy.ToString();
                }
                catch (System.UnauthorizedAccessException)
                {
                    //MessageTextbox.Text = "Location disabled.";

                    //LatitudeTextbox.Text = "No data";
                    //LongitudeTextbox.Text = "No data";
                    //AccuracyTextbox.Text = "No data";
                }
                catch (TaskCanceledException)
                {
                    //     MessageTextbox.Text = "Operation canceled.";
                }
                finally
                {
                    _cts = null;
                }

                // Reset the buttons.
                //MapLocationButton.IsEnabled = true;
                //CancelGetLocationButton.IsEnabled = false;
            }
            catch { }
        }
        async Task SetCurrentLocation()
        {
            try
            {
                // Get the cancellation token.
                _cts  = new CancellationTokenSource();
                token = _cts.Token;
                // Get the location.

                var asyncResult = _geolocator.GetGeopositionAsync();
                var task        = asyncResult.AsTask();

                var readyTask = await Task.WhenAny(task, Task.Delay(10000));

                if (readyTask != task)
                {
                    SetMessage(MessageType.Error, "❎ Unable to find your location, trying again...");
                    SetCurrentLocation();
                }

                Geoposition pos = await task;
                //  MessageTextbox.Text = "";

                Location location = new Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
                ProxyTracker.GetInstance().MyTrackLocation = new Services.TrackService.TrackLocation {
                    Latitude = pos.Coordinate.Latitude, Longitude = pos.Coordinate.Longitude
                };
                // Now set the zoom level of the map based on the accuracy of our location data.
                // Default to IP level accuracy. We only show the region at this level - No icon is displayed.
                double zoomLevel = 13.0f;


                Callout callout = new Callout();

                callout.Text = "My Location";
                callout.Lon  = "Lon (λ): " + location.Longitude.ToString().Substring(0, 7);
                callout.Lat  = "Lat (φ): " + location.Latitude.ToString().Substring(0, 7);
                _locationIcon100m.DataContext = callout;

                foreach (var children in trvMap.Children)
                {
                    if (children.GetType().Name == "LocationIcon100m")
                    {
                        trvMap.Children.Remove(children);
                        break;
                    }
                }

                // Add the 100m icon and zoom a little closer.
                trvMap.Children.Add(_locationIcon100m);

                MapLayer.SetPosition(_locationIcon100m, location);
                zoomLevel = 17.0f;

                // Set the map to the given location and zoom level.
                trvMap.SetView(location, zoomLevel);
                //  txtMessage.Text = "";
            }
            catch (Exception ex)
            {
                SetMessage(MessageType.Error, "❎ Location services is disabled on this computer");
            }
        }