public static bool UpdatePhoneNumberToCard(string phone, DataModels.ContactCard.ContactCard card, string verificationCode, MobileServiceProvider carrier = MobileServiceProvider.None, bool isVerified = false)
 {
     try
     {
         int phoneType = Convert.ToInt32(CommunicationTypeEnum.PhoneNumber);
         if (card.Communications.Where(x => x.IsDefault == true).Where(x => x.CommunicationTypeEnum == (byte)CommunicationTypeEnum.PhoneNumber).FirstOrDefault() == null)
         {
             Communication c = new Communication();
             c.IsDefault = true;
             UpdatePhoneNumberToCard(phone, c, verificationCode, carrier, isVerified);
             card.Communications.Add(c);
         }
         else
         {
             UpdatePhoneNumberToCard(phone, card.Communications.Where(x => x.IsDefault == true).Where(x => x.CommunicationTypeEnum == (byte)CommunicationTypeEnum.PhoneNumber).FirstOrDefault(), verificationCode, carrier, isVerified);
         }
         return true;
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return false;
 }
        public static Portable.Classes.Location.Location CreateNewLocation(LocationOwnerType ownerType, string name, string address1, string address2, string city, int country, string state, string zip, string website, Guid idOfOwner)
        {
            Portable.Classes.Location.Location l = new Portable.Classes.Location.Location();
            string log = string.Empty;
            try
            {
                var dc = new ManagementContext();
                DataModels.Location.Location location = new DataModels.Location.Location();
                if (ownerType == LocationOwnerType.calendar)
                {
                    var cal = dc.Calendar.Where(x => x.CalendarId == idOfOwner).FirstOrDefault();
                    cal.Locations.Add(location);
                }
                else if (ownerType == LocationOwnerType.shop)
                {
                    var shop = dc.Merchants.Where(x => x.PrivateManagerId == idOfOwner).FirstOrDefault();
                    shop.Locations.Add(location);
                }
                location.LocationName = name;
                location.Contact = new DataModels.ContactCard.ContactCard();
                Address a = new Address();
                a.Address1 = address1;
                a.Address2 = address2;
                a.CityRaw = city;
                a.Country = dc.Countries.Where(x => x.CountryId == country).FirstOrDefault();
                a.StateRaw = state;
                a.ContactCard = location.Contact;
                var coords = OpenStreetMap.FindLatLongOfAddress(a.Address1, a.Address2, a.Zip, a.CityRaw, a.StateRaw, a.Country != null ? a.Country.Name : string.Empty);
                a.Coords = new System.Device.Location.GeoCoordinate();
                if (coords != null)
                {
                    log += "not" + coords.Latitude + " " + coords.Longitude;
                    a.Coords.Latitude = coords.Latitude;
                    a.Coords.Longitude = coords.Longitude;
                }
                else
                {
                    a.Coords.Latitude = 0.0;
                    a.Coords.Longitude = 0.0;
                }
                a.Coords.Altitude = 0.0;
                a.Coords.Course = 0.0;
                a.Coords.HorizontalAccuracy = 1.0;
                a.Coords.Speed = 0.0;
                a.Coords.VerticalAccuracy = 1.0;

                location.Contact.Addresses.Add(a);
                if (!String.IsNullOrEmpty(website))
                {
                    string comType = CommunicationTypeEnum.Website.ToString();
                    Communication web = new Communication();
                    web.Data = website;
                    web.CommunicationTypeEnum = (byte)CommunicationTypeEnum.Website;
                    location.Contact.Communications.Add(web);
                }
                dc.Locations.Add(location);
                int c = dc.SaveChanges();

                l.LocationId = location.LocationId;
                l.LocationName = name;
                Portable.Classes.ContactCard.Address address = new Portable.Classes.ContactCard.Address();
                address.Address1 = address1;
                address.Address2 = address2;
                address.CityRaw = city;
                if (a.Country != null)
                    address.Country = a.Country.Name;
                address.StateRaw = a.StateRaw;
                address.Zip = a.Zip;
                l.Contact.Addresses.Add(address);
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType(), additionalInformation: name + " " + address1 + " " + address2 + " " + city + " " + country + " " + state + " " + zip + " " + website + " " + idOfOwner);
            }
            return l;

        }
        public static bool UpdateLeagueFromMobile(LeagueBase leagueToUpdate)
        {
            try
            {
                var dc = new ManagementContext();
                var league = dc.Leagues.Include("ContactCard").Where(x => x.LeagueId == leagueToUpdate.LeagueId).FirstOrDefault();
                league.Name = leagueToUpdate.Name;
                league.WebSite = leagueToUpdate.Website;
                league.Twitter = leagueToUpdate.Twitter;
                league.Instagram = leagueToUpdate.Instagram;
                league.Facebook = leagueToUpdate.Facebook;
                league.RuleSetsPlayedEnum = (long)leagueToUpdate.RuleSetsPlayedEnum;
                league.CultureLCID = leagueToUpdate.CultureSelected;

                if (leagueToUpdate.Founded != null && leagueToUpdate.Founded != new DateTime() && leagueToUpdate.Founded > DateTime.UtcNow.AddYears(-100))
                    league.Founded = leagueToUpdate.Founded;

                var emailDb = league.ContactCard.Emails.Where(x => x.IsDefault == true).FirstOrDefault();
                if (emailDb != null)
                {
                    emailDb.EmailAddress = leagueToUpdate.Email;
                }
                else
                {
                    Email em = new Email();
                    em.ContactCard = league.ContactCard;
                    em.EmailAddress = leagueToUpdate.Email;
                    em.IsDefault = true;
                    league.ContactCard.Emails.Add(em);
                }
                //int numberType = Convert.ToInt32(CommunicationTypeEnum.PhoneNumber);
                var phone = league.ContactCard.Communications.Where(x => x.CommunicationTypeEnum == (byte)CommunicationTypeEnum.PhoneNumber).FirstOrDefault();
                if (phone != null)
                    phone.Data = leagueToUpdate.PhoneNumber;
                else
                {
                    Communication com = new Communication();
                    com.Data = leagueToUpdate.PhoneNumber;
                    //com.CommunicationType = dc.CommunicationType.Where(x => x.CommunicationTypeId == numberType).FirstOrDefault();
                    com.CommunicationTypeEnum = (byte)CommunicationTypeEnum.PhoneNumber;
                    com.ContactCard = league.ContactCard;
                    com.IsDefault = true;
                    league.ContactCard.Communications.Add(com);
                }

                var addresses = league.ContactCard.Addresses.Where(x => x.IsDefault == true).FirstOrDefault();
                if (addresses == null)
                    if (league.ContactCard.Addresses.Count > 0)
                        addresses = league.ContactCard.Addresses.FirstOrDefault();
                if (addresses == null)
                {
                    addresses = new Address();
                    addresses.ContactCard = league.ContactCard;
                }
                addresses.CityRaw = leagueToUpdate.City;
                addresses.StateRaw = leagueToUpdate.State;
                addresses.Zip = leagueToUpdate.ZipCode;

                var countryDb = dc.Countries.Where(x => x.Name.ToLower() == leagueToUpdate.Country.ToLower()).FirstOrDefault();
                if (countryDb == null)
                    countryDb = dc.Countries.Where(x => x.Code.ToLower() == leagueToUpdate.Country.ToLower()).FirstOrDefault();
                addresses.Country = countryDb;
                var coords = OpenStreetMap.FindLatLongOfAddress(addresses.Address1, addresses.Address2, addresses.Zip, addresses.CityRaw, addresses.StateRaw, addresses.Country != null ? addresses.Country.Name : string.Empty);
                if (coords != null)
                {
                    addresses.Coords = new System.Device.Location.GeoCoordinate();
                    addresses.Coords.Latitude = coords.Latitude;
                    addresses.Coords.Longitude = coords.Longitude;
                    addresses.Coords.Altitude = 0;
                    addresses.Coords.Course = 0;
                    addresses.Coords.HorizontalAccuracy = 1;
                    addresses.Coords.Speed = 0;
                    addresses.Coords.VerticalAccuracy = 1;
                }
                addresses.IsDefault = true;


                int c = dc.SaveChanges();
                return c > 0;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }