public async Task <Response> GetAsync(string url, object multiple)
        {
            var response = new Response();

            if (NetworkService.IsServerAvailable)
            {
                var fullUrl = SysPrefs.ApiBase + url;
                try
                {
                    response = await fullUrl.WithHeaders(multiple).GetJsonAsync <Response>();
                }
                catch (Exception)
                {
                    MessengerService.Toast(this, "Problem reaching remote server!", false);
                    response = new Response
                    {
                        Success = false
                    };
                }
            }
            else
            {
                response.Success = false;
                response.Detail  = "No network connection";
                MessengerService.Toast(this, "No network connection available!", true);
            }

            return(response);
        }
        public async Task <bool> UpdateLinks()
        {
            Stopwatch stop = new Stopwatch();

            stop.Start();
            try
            {
                var resp = await GetAsync("all_links/");

                if (resp.Success)
                {
                    Links = JsonConvert.DeserializeObject <List <Link> >(resp.Data.ToString());
                }
                else
                {
                    MessengerService.Toast(this, "Failed to update links", true);
                    return(false);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                MessengerService.Toast(this, "Failed to update links", true);
                return(false);
            }
            stop.Stop();
            Debug.WriteLine("pbuddy links: " + stop.Elapsed);
            return(true);
        }
        //public async Task<Response> EstimateTime(int startID, int endID, int riverID)
        //{
        //    Response resp = new Response();
        //    try
        //    {
        //        resp = await GetAsync("estimate_time/", new {p1 = startID, p2 = endID, river = riverID});
        //        if (resp.Success)
        //        {
        //            resp.Data = JsonConvert.DeserializeObject<TripEstimate>(resp.Data.ToString());
        //        }
        //    }
        //    catch (JsonException)
        //    {
        //        MessengerService.Toast(this, "Failed estimate_time call!", true);
        //    }
        //    return resp;
        //}

        public TripEstimate EstimateTrip(int startId, int endId, int riverId)
        {
            var trip      = new TripEstimate();
            var beginList = (from point in DatabaseService.GetInstance().Points where point.RiverId == riverId select new { BeginId = point.Id, BeginLat = point.Lat, BeginLng = point.Lng }).ToList();
            var endList   = (from point in DatabaseService.GetInstance().Points where point.RiverId == riverId select new { EndId = point.Id, EndLat = point.Lat, EndLng = point.Lng }).ToList();
            var result    = (from link in DatabaseService.GetInstance().Links join p1 in beginList on link.Begin equals p1.BeginId join p2 in endList on link.End equals p2.EndId select new LinkPoint(link.Id, link.Begin, link.End, link.Speed, link.RiverId, (float)p1.BeginLat, (float)p1.BeginLng, (float)p2.EndLat, (float)p2.EndLng)).ToList();

            var temp = (from first in result where first.Begin == startId select first).First();

            if (temp == null)
            {
                MessengerService.Toast(this, "Could not find first point", true);
                return(null);
            }
            int newId;
            var list = new List <LinkPoint>();

            while (temp != null && temp.End != endId)
            {
                list.Add(temp);
                newId = temp.End;
                result.Remove(temp);
                temp = (from f in result where f.Begin == newId select f).FirstOrDefault();
            }
            if (temp == null)
            {
                MessengerService.Toast(this, "Did not reach end point", true);
                return(null);
            }
            if (temp.End == endId)
            {
                list.Add(temp);
            }
            if (list.ElementAt(0).Begin == startId && list.Last().End == endId)
            {
                trip         = PBUtilities.LinksToEstimate(result);
                trip.RiverId = riverId;
                trip.StartId = startId;
                trip.EndId   = endId;
            }
            return(trip);
        }
        public async Task <bool> Setup(bool sync = false)
        {
            if (storageService == null)
            {
                storageService = Mvx.Resolve <IStorageService>();
            }
            if (sync)
            {
                return(await UpdateAll());
            }
            if (storageService.HasData(names))
            {
                Points = JsonConvert.DeserializeObject <List <Point> >(storageService.ReadSerializedFromFile("points"));
                Rivers = JsonConvert.DeserializeObject <List <River> >(storageService.ReadSerializedFromFile("rivers"));
                Links  = JsonConvert.DeserializeObject <List <Link> >(storageService.ReadSerializedFromFile("links"));
            }
            var isUpdated = (Points != null && Rivers != null && Links != null && Points.Count > 0 && Rivers.Count > 0 && Links.Count > 0);

            MessengerService.Toast(this, isUpdated ? "Data obtained from local copy" : "Data not updated", false);
            return(isUpdated ? true : await UpdateAll());
        }
        public Path GetPath(Point start, Point end)
        {
            //todo: clean this up
            var path = new Path();

            path.Points = new List <Point>();
            if (start.RiverId != end.RiverId)
            {
                MessengerService.Toast(this, "invalid points for path", true);
            }
            path.RiverId = start.RiverId;
            var tempList = (from p in Points where p.RiverId == start.RiverId join lnk in Links on p.Id equals lnk.Begin select new PointWithNext(p, lnk.End)).ToList();

            if (tempList.Count > 0)
            {
                var temp = (from p in tempList where p.Point.Id == start.Id select p).Single();
                if (temp == null)
                {
                    MessengerService.Toast(this, "Failed to get path", true);
                }
                else
                {
                    path.Points.Add(temp.Point);
                }
                while (temp != null && temp.Point.Id != end.Id)
                {
                    temp = (from p in tempList where p.Point.Id == temp.Next select p).First();
                    if (temp != null)
                    {
                        path.Points.Add(temp.Point);
                    }
                    else
                    {
                        MessengerService.Toast(this, "Failed to get path", true);
                        break;
                    }
                }
            }
            return(path);
        }