public static async Task <bool> LogIn(LoggedUser user)
        {
            AuthToken authToken = Authorize(user);

            if (authToken.AuthDT != 0)
            {
                LoggedUser loggedUser = SQLiteDb.GetUser();
                if (loggedUser.Email != user.Email)
                {
                    var httpClient = new HttpClient();
                    var url        = "https://pwszalarmwebapi.azurewebsites.net/api/accounts/user?username="******"Bearer", authToken.AccessToken);
                    var content = await httpClient.GetStringAsync(url);

                    loggedUser = JsonConvert.DeserializeObject <LoggedUser>(content);
                    if (loggedUser.Email != user.Email)
                    {
                        return(false);
                    }
                }
                loggedUser.AuthorizationTime = DateTime.Now;
                loggedUser.Authorization     = authToken.AccessToken;
                loggedUser.RememberMe        = user.RememberMe;
                if (user.RememberMe)
                {
                    loggedUser.Password = user.Password;
                }
                SQLiteDb.UpdateAccountData(loggedUser);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static List <Messages> GetMessages(int alarmId)
        {
            IEnumerable <Messages> messages   = new List <Messages>();
            LoggedUser             loggedUser = SQLiteDb.GetUser();

            if (loggedUser.Email != "failed")
            {
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loggedUser.Authorization);
                var url = "https://pwszalarmwebapi.azurewebsites.net/api/messages?id=" + alarmId;
                //GET/api/messages
                var content = httpClient.GetStringAsync(url).GetAwaiter().GetResult();
                messages = JsonConvert.DeserializeObject <List <Messages> >(content);
                if (messages.Any())
                {
                    return(messages.ToList());
                }
            }
            //else
            var message = new Messages()
            {
                Id          = 1,
                AlarmId     = 1,
                Message     = "failed",
                MessageTime = DateTime.Now,
                UserName    = "******"
            };
            var messagesList = messages.ToList();

            messagesList.Add(message);
            return(messagesList);
        }
        public static async Task SetSendNotificationsAsync(bool sendNotifications)
        {
            LoggedUser loggedUser = SQLiteDb.GetUser();
            var        httpClient = new HttpClient();
            var        url        = "https://pwszalarmwebapi.azurewebsites.net/api/accounts/user/notifications?userId=" + loggedUser.Id + "&sendNotifications=" + sendNotifications;

            //POST /api/accounts/user/notifications
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loggedUser.Authorization);
            _ = await httpClient.GetAsync(url);
        }
        public static async void PostNotifyToken(string token)
        {
            LoggedUser loggedUser = SQLiteDb.GetUser();

            if (!string.IsNullOrEmpty(token))
            {
                var notifyToken = new NotifyToken
                {
                    UserId = loggedUser.Id,
                    Token  = token
                };
                var httpClient = new HttpClient();
                var url        = "https://pwszalarmwebapi.azurewebsites.net/api/accounts/user/fcmtoken";
                //POST /api/accounts/user/token
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loggedUser.Authorization);
                var tokenJson   = JsonConvert.SerializeObject(notifyToken);
                var httpContent = new StringContent(tokenJson, Encoding.UTF8, "application/json");
                await httpClient.PostAsync(url, httpContent);
            }
        }
        public static void GetAlarmsFromApi(SQLiteConnection db)
        {
            IEnumerable <Alarm> alarms;
            LoggedUser          loggedUser = SQLiteDb.GetUser();

            if (loggedUser.Email != "failed")
            {
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loggedUser.Authorization);
                var url = "https://pwszalarmwebapi.azurewebsites.net/api/alarms";
                //GET/api/alarms
                var content = httpClient.GetStringAsync(url).GetAwaiter().GetResult();
                alarms = JsonConvert.DeserializeObject <List <Alarm> >(content);
                if (alarms.Any())
                {
                    var cmd = db.CreateCommand("DELETE FROM Alarm");
                    cmd.ExecuteNonQuery();
                    db.InsertAll(alarms);
                }
            }
        }
        public static async void PostAlarm(Activity activity, ShortAlarm shortAlarm)
        {
            LoggedUser loggedUser = SQLiteDb.GetUser();

            if (loggedUser.Email != "failed")
            {
                var httpClient = new HttpClient();
                var url        = "https://pwszalarmwebapi.azurewebsites.net/api/alarms";
                //POST /api/alarms
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loggedUser.Authorization);
                var alarmJson   = JsonConvert.SerializeObject(shortAlarm);
                var httpContent = new StringContent(alarmJson, Encoding.UTF8, "application/json");
                var response    = await httpClient.PostAsync(url, httpContent);

                if (response.IsSuccessStatusCode)
                {
                    Toast.MakeText(activity, "Zgłoszenie wysłane", ToastLength.Short).Show();
                    await SQLiteDb.LoadAlarmsToDb(activity);
                }
            }
        }
        public static async void CheckPermissions(Activity activity)
        {
            var storage = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

            var location = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.LocationAlways);

            if (storage != PermissionStatus.Granted || location != PermissionStatus.Granted)
            {
                if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
                {
                    SQLiteDb.ShowAlert(activity, "Uprawnienia", "Potrzebujemy uprawnień do pamięci.");
                }
                if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.LocationAlways))
                {
                    SQLiteDb.ShowAlert(activity, "Uprawnienia", "Potrzebujemy uprawnień do lokalizacji.");
                }
                Permission[] permissions =
                {
                    Permission.Storage,
                    Permission.LocationAlways
                };
                var results = await CrossPermissions.Current.RequestPermissionsAsync(permissions);
            }
        }