public ReturnRideData AddRide(string passengerID, string driverID, string from_destination, string to_destination, string from_lat, string from_lng, string to_lat, string to_lng)
        {
            ReturnRideData returnRideData = new ReturnRideData();

            ride_detail ride = new ride_detail();

            int pID = Convert.ToInt32(passengerID); // passenger
            int dID = Convert.ToInt32(driverID);    //driver

            ride.passengerID      = pID;
            ride.driverID         = dID;
            ride.from_destination = System.Web.HttpUtility.UrlDecode(from_destination);
            ride.to_destination   = System.Web.HttpUtility.UrlDecode(to_destination);
            ride.from_lat         = Convert.ToDouble(from_lat);
            ride.from_lng         = Convert.ToDouble(from_lng);
            ride.to_lat           = Convert.ToDouble(to_lat);
            ride.to_lng           = Convert.ToDouble(to_lng);
            ride.created_date     = DateTime.Now;
            ride.status           = 0;
            ride.amount           = 0;
            ride.rating           = 0;
            db.ride_detail.Add(ride);
            db.SaveChanges();

            var usr = db.users.Where(v => v.id == ride.driverID).FirstOrDefault();

            usr.is_available = 0;
            db.SaveChanges();

            string rid  = ride.id.ToString();          //ride id
            string rpid = ride.passengerID.ToString(); // passengerID
            string rdid = ride.driverID.ToString();    //driverID

            returnRideData = new ReturnRideData
            {
                id               = rid,
                passengerID      = rpid,
                driverID         = rdid,
                from_destination = ride.from_destination,
                to_destination   = ride.to_destination,
                from_lat         = ride.from_lat.ToString(),
                from_lng         = ride.from_lng.ToString(),
                to_lat           = ride.to_lat.ToString(),
                to_lng           = ride.to_lng.ToString(),
                status           = ride.status.ToString(),
                amount           = ride.amount.ToString(),
                review           = ride.review,
                rating           = ride.rating.ToString(),
                driver_name      = "Driver Name",
                passenger_name   = "Passenger Name"
            };
            return(returnRideData);
        }
        public ReturnRideData ChangeRideStatus(string rideId, string userId, string status)
        {
            ReturnRideData returnRideData = new ReturnRideData();

            int rID     = Convert.ToInt32(rideId);                                                                  //rideid
            int dID     = Convert.ToInt32(userId);                                                                  //driver
            int rstatus = Convert.ToInt32(status);                                                                  //driver

            var ride = (from r in db.ride_detail where r.id == rID && r.driverID == dID select r).FirstOrDefault(); //AND c.ownerId = userid

            ride.status = rstatus;
            db.SaveChanges();

            if (ride.status == 1)
            {
                var usr = db.users.Where(v => v.id == ride.driverID).FirstOrDefault();
                usr.is_available = 0;
                db.SaveChanges();
            }
            if (ride.status == 3)
            {
                var usr = db.users.Where(v => v.id == ride.driverID).FirstOrDefault();
                usr.is_available = 1;
                db.SaveChanges();
            }

            returnRideData = new ReturnRideData {
                id               = ride.id.ToString(),
                passengerID      = ride.passengerID.ToString(),
                driverID         = ride.driverID.ToString(),
                from_destination = ride.from_destination,
                to_destination   = ride.to_destination,
                from_lat         = ride.from_lat.ToString(),
                from_lng         = ride.from_lng.ToString(),
                to_lat           = ride.to_lat.ToString(),
                to_lng           = ride.to_lng.ToString(),
                status           = ride.status.ToString(),
                amount           = ride.amount.ToString(),
                review           = ride.review,
                rating           = ride.rating.ToString(),
                driver_name      = "Driver Name",
                passenger_name   = "Passenger Name"
            };
            return(returnRideData);
        }
        public ReturnRideData FinishRide(ReturnRideData postRideData)
        {
            ReturnRideData returnRideData = new ReturnRideData();
            int            rID            = Convert.ToInt32(postRideData.id); //rideid


            var ride = (from r in db.ride_detail where r.id == rID select r).FirstOrDefault(); //AND c.ownerId = userid

            ride.status = 2;
            ride.amount = Convert.ToDouble(postRideData.amount);
            ride.review = System.Web.HttpUtility.UrlDecode(postRideData.review);
            ride.rating = Convert.ToDouble(postRideData.rating);
            db.SaveChanges();

            var usr           = db.users.Where(v => v.id == ride.driverID).FirstOrDefault();
            var ratingAverage = db.ride_detail.Where(a => a.driverID == usr.id).Average(a => a.rating);

            usr.is_available = 1;
            usr.avg_rating   = Math.Round(Convert.ToDouble(ratingAverage), 1, MidpointRounding.AwayFromZero);
            db.SaveChanges();

            returnRideData = new ReturnRideData
            {
                id               = ride.id.ToString(),
                passengerID      = ride.passengerID.ToString(),
                driverID         = ride.driverID.ToString(),
                from_destination = ride.from_destination,
                to_destination   = ride.to_destination,
                from_lat         = ride.from_lat.ToString(),
                from_lng         = ride.from_lng.ToString(),
                to_lat           = ride.to_lat.ToString(),
                to_lng           = ride.to_lng.ToString(),
                status           = ride.status.ToString(),
                amount           = ride.amount.ToString(),
                review           = ride.review,
                rating           = ride.rating.ToString(),
                driver_name      = "Driver Name",
                passenger_name   = "Passenger Name"
            };
            return(returnRideData);
        }
        public ReturnRideData CheckNewRide(string userId, string userType)
        {
            ReturnRideData returnRideData = null;

            int userID = Convert.ToInt32(userId); //driver

            if (userType == "Passenger")
            {
                // var ride = db.ride_detail.Where(r => r.passengerID == userID && r.status == 1).FirstOrDefault();

                var ride = db.ride_detail.Where(r => r.passengerID == userID && r.status == 1).Join(db.users, r => r.driverID, u => u.id, (r, u) => new { r = r, username = u.name })
                           .FirstOrDefault();


                if (ride != null)
                {
                    returnRideData = new ReturnRideData
                    {
                        id               = ride.r.id.ToString(),
                        passengerID      = ride.r.passengerID.ToString(),
                        driverID         = ride.r.driverID.ToString(),
                        from_destination = ride.r.from_destination,
                        to_destination   = ride.r.to_destination,
                        from_lat         = ride.r.from_lat.ToString(),
                        from_lng         = ride.r.from_lng.ToString(),
                        to_lat           = ride.r.to_lat.ToString(),
                        to_lng           = ride.r.to_lng.ToString(),
                        status           = ride.r.status.ToString(),
                        amount           = ride.r.amount.ToString(),
                        review           = ride.r.review,
                        rating           = ride.r.rating.ToString(),
                        driver_name      = ride.username,
                        passenger_name   = ""

                                           /* id = ride.id.ToString(),
                                            * passengerID = ride.passengerID.ToString(),
                                            * driverID = ride.driverID.ToString(),
                                            * from_destination = ride.from_destination,
                                            * to_destination = ride.to_destination,
                                            * from_lat = ride.from_lat.ToString(),
                                            * from_lng = ride.from_lng.ToString(),
                                            * to_lat = ride.to_lat.ToString(),
                                            * to_lng = ride.to_lng.ToString(),
                                            * status = ride.status.ToString(),
                                            * amount = ride.amount.ToString(),
                                            * review = ride.review,
                                            * rating = ride.rating.ToString(),
                                            * driver_name = "Driver Name",
                                            * passenger_name = "Passenger Name"
                                            */
                    };
                }
            }
            else if (userType == "Driver")
            {
                //  var ride = db.ride_detail.Where(r => r.driverID == userID && r.status == 0).FirstOrDefault();

                var ride = db.ride_detail.Where(r => r.driverID == userID && r.status == 0).Join(db.users, r => r.passengerID, u => u.id, (r, u) => new { r = r, username = u.name })
                           .FirstOrDefault();

                if (ride != null)
                {
                    returnRideData = new ReturnRideData
                    {
                        id               = ride.r.id.ToString(),
                        passengerID      = ride.r.passengerID.ToString(),
                        driverID         = ride.r.driverID.ToString(),
                        from_destination = ride.r.from_destination,
                        to_destination   = ride.r.to_destination,
                        from_lat         = ride.r.from_lat.ToString(),
                        from_lng         = ride.r.from_lng.ToString(),
                        to_lat           = ride.r.to_lat.ToString(),
                        to_lng           = ride.r.to_lng.ToString(),
                        status           = ride.r.status.ToString(),
                        amount           = ride.r.amount.ToString(),
                        review           = ride.r.review,
                        rating           = ride.r.rating.ToString(),
                        driver_name      = "",
                        passenger_name   = ride.username

                                           /*id = ride.id.ToString(),
                                            * passengerID = ride.passengerID.ToString(),
                                            * driverID = ride.driverID.ToString(),
                                            * from_destination = ride.from_destination,
                                            * to_destination = ride.to_destination,
                                            * from_lat = ride.from_lat.ToString(),
                                            * from_lng = ride.from_lng.ToString(),
                                            * to_lat = ride.to_lat.ToString(),
                                            * to_lng = ride.to_lng.ToString(),
                                            * status = ride.status.ToString(),
                                            * amount = ride.amount.ToString(),
                                            * review = ride.review,
                                            * rating = ride.rating.ToString(),
                                            * driver_name = "Driver Name",
                                            * passenger_name = "Passenger Name"*/
                    };
                }
            }


            return(returnRideData);
        }