/*public ReturnUserData Register(string name, string email, string password, string phone, string nic, string userType, string image)
         * {
         *  user usr = new user();
         *  usr.name = System.Web.HttpUtility.UrlDecode(name);
         *  usr.email = email;
         *  usr.password = System.Web.HttpUtility.UrlDecode(password);
         *  usr.phone = phone;
         *  usr.nic = nic;
         *  usr.userType = userType;
         *  usr.is_login = 1;
         *  usr.is_vehicle_added = 0;
         *  usr.is_available = 0;
         *  usr.image = image;
         *  db.users.Add(usr);
         *  db.SaveChanges();
         *  string uid = usr.id.ToString();
         *  ReturnUserData returnUserData = new ReturnUserData {
         *      isError = "0", errorMessage = null, id = uid, name = usr.name, email = usr.email,
         *      password = usr.password, phone = usr.phone, userType = usr.userType, street = usr.street, city = usr.city, country = usr.country,
         *      lat = "333.5555", lng = "79.5554", is_login = usr.is_login.ToString(), is_vehicle_added = usr.is_vehicle_added.ToString(),
         *      reg_id = "asdfa1234asdfdf", nic = "5555"
         *  };
         *  return returnUserData;
         *  // return usr;
         * }*/

        /*public ReturnUserData UpdateProfile(string usrid, string name, string email, string password, string phone, string nic)
         * {
         *
         *  ReturnUserData returnUserData = new ReturnUserData();
         *
         *  int userid = Convert.ToInt32(usrid);
         *  var usr = (from u in db.users where u.id == userid select u).FirstOrDefault();
         *  if (usr != null)
         *  {
         *      //usr.id = userid;
         *      usr.name = System.Web.HttpUtility.UrlDecode(name);
         *      usr.email = email;
         *      usr.password = System.Web.HttpUtility.UrlDecode(password);
         *      usr.phone = phone;
         *      usr.nic = nic;
         *      db.SaveChanges();
         *      returnUserData = new ReturnUserData {
         *          isError = "0", errorMessage = null, id = userid.ToString(), name = usr.name, email = usr.email, password = usr.password,
         *          phone = usr.phone, userType = usr.userType, street = usr.street, city = usr.city, country = usr.country,
         *          lat = usr.lat.ToString(), lng = usr.lng.ToString(), is_login = usr.is_login.ToString(),
         *          is_vehicle_added = usr.is_vehicle_added.ToString(), reg_id = "asdfa1234asdfdf", nic = usr.nic
         *      };
         *  }
         *
         *  return returnUserData;
         *  // return usr;
         * }*/

        public ReturnUserData SaveAddress(string id, string street, string city, string country, string lat, string lng)
        {
            ReturnUserData returnUserData = new ReturnUserData();

            int userid = Convert.ToInt32(id);
            var usr    = (from c in db.users where c.id == userid select c).FirstOrDefault();

            if (usr != null)
            {
                usr.id      = userid;
                usr.street  = System.Web.HttpUtility.UrlDecode(street);
                usr.city    = System.Web.HttpUtility.UrlDecode(city);
                usr.country = System.Web.HttpUtility.UrlDecode(country);
                usr.lat     = Convert.ToDouble(lat);
                usr.lng     = Convert.ToDouble(lng);
                db.SaveChanges();
                returnUserData = new ReturnUserData {
                    isError  = "0", errorMessage = null, id = usr.id.ToString(), name = usr.name,
                    email    = usr.email, password = usr.password, phone = usr.phone, userType = usr.userType,
                    street   = usr.street, city = usr.city, country = usr.country, lat = usr.lat.ToString(), lng = usr.lng.ToString(),
                    is_login = usr.is_login.ToString(), is_vehicle_added = usr.is_vehicle_added.ToString(),
                    reg_id   = "asdfa1234asdfdf", nic = usr.nic
                };
            }
            return(returnUserData);
        }
        public ReturnUserData SignUp(ReturnUserData user)
        {
            user usr = new user();

            usr.name             = user.name;
            usr.email            = user.email;
            usr.password         = user.password;
            usr.phone            = user.phone;
            usr.nic              = user.nic;
            usr.userType         = user.userType;
            usr.is_login         = 1;
            usr.is_vehicle_added = 0;
            usr.is_available     = 0;
            usr.image            = System.Web.HttpUtility.UrlDecode(user.image);

            db.users.Add(usr);
            db.SaveChanges();
            string         uid            = usr.id.ToString();
            ReturnUserData returnUserData = new ReturnUserData
            {
                isError          = "0",
                errorMessage     = null,
                id               = uid,
                name             = usr.name,
                email            = usr.email,
                password         = usr.password,
                phone            = usr.phone,
                userType         = usr.userType,
                street           = usr.street,
                city             = usr.city,
                country          = usr.country,
                lat              = "333.5555",
                lng              = "79.5554",
                is_login         = usr.is_login.ToString(),
                is_vehicle_added = usr.is_vehicle_added.ToString(),
                reg_id           = "asdfa1234asdfdf",
                nic              = usr.nic
            };

            return(returnUserData);
        }
        public ReturnUserData UpdateProfile(ReturnUserData user)
        {
            ReturnUserData returnUserData = new ReturnUserData();

            int userid = Convert.ToInt32(user.id);
            var usr    = (from u in db.users where u.id == userid select u).FirstOrDefault();

            if (usr != null)
            {
                //usr.id = userid;
                usr.name     = System.Web.HttpUtility.UrlDecode(user.name);
                usr.email    = user.email;
                usr.password = System.Web.HttpUtility.UrlDecode(user.password);
                usr.phone    = user.phone;
                usr.nic      = user.nic;
                usr.image    = user.image;
                db.SaveChanges();
                returnUserData = new ReturnUserData
                {
                    isError          = "0",
                    errorMessage     = null,
                    id               = userid.ToString(),
                    name             = usr.name,
                    email            = usr.email,
                    password         = usr.password,
                    phone            = usr.phone,
                    userType         = usr.userType,
                    street           = usr.street,
                    city             = usr.city,
                    country          = usr.country,
                    lat              = usr.lat.ToString(),
                    lng              = usr.lng.ToString(),
                    is_login         = usr.is_login.ToString(),
                    is_vehicle_added = usr.is_vehicle_added.ToString(),
                    reg_id           = "asdfa1234asdfdf",
                    nic              = usr.nic
                };
            }
            return(returnUserData);
        }
        public ReturnUserData Authenticate(string email, string password)
        {
            ReturnUserData returnUserData;
            var            usr = db.users.Where(u => u.email == email && u.password == password).FirstOrDefault();

            if (usr != null)
            {
                usr.is_login = 1;
                db.SaveChanges();
                returnUserData = new ReturnUserData {
                    isError = "0", errorMessage = null, id = usr.id.ToString(), name = usr.name, email = usr.email, password = usr.password, phone = usr.phone, userType = usr.userType, street = usr.street, city = usr.city, country = usr.country, lat = usr.lat.ToString(), lng = usr.lng.ToString(), is_login = usr.is_login.ToString(), is_vehicle_added = usr.is_vehicle_added.ToString(), reg_id = "asdfa1234asdfdf", nic = "5555"
                };
            }
            else
            {
                returnUserData = new ReturnUserData {
                    isError = "1", errorMessage = "Invalid credentinals", id = "", name = "", email = "", password = "", phone = "", userType = "", street = "", city = "", country = "", lat = "", lng = "", is_login = "", is_vehicle_added = "", reg_id = "", nic = ""
                };
            }

            return(returnUserData);
        }