/// <summary>
        /// Validates the request to create a new trail
        /// </summary>
        public ValidationServiceResponse <TopoTrailInfo> ValidateCreate(ITopoTrailUpdateRequest request)
        {
            // load existing trail
            var response = new ValidationServiceResponse <TopoTrailInfo>(null);

            // do basic validation
            if (String.IsNullOrWhiteSpace(request.Name))
            {
                response.AddError("Name", "Name is required!");
            }

            // TODO: REFACTOR: use GeoPolitical
            var timezone = GeoTimezoneInfo.Find(request.Timezone);

            if (timezone == null)
            {
                response.AddError("Timezone", "Timezone is missing or invalid!");
            }

            var country = GeoCountryInfo.Find(request.Country);

            if (country == null)
            {
                response.AddError("Country", "Country is missing or invalid!");
            }

            var region = GeoRegionInfo.Find(request.Region);

            if (region == null && !String.IsNullOrWhiteSpace(request.Region))
            {
                response.AddError("Region", "Region is invalid!");
            }

            return(response);
        }
        public TopoTrailInfo(ITopoTrailUpdateRequest data, List <GpxTrackData> tracks)
        {
            Name        = data.Name;
            Description = data.Description;

            Keywords = data.Keywords;
            UrlText  = data.UrlText;
            UrlLink  = data.UrlLink;

            Timezone = GeoTimezoneInfo.Find(data.Timezone);
            if (Timezone == null)
            {
                Timezone = GeoTimezoneInfo.UTC;
            }

            Country  = GeoCountryInfo.Find(data.Country);
            Region   = GeoRegionInfo.Find(data.Region);
            Location = data.Location;

            if (tracks != null)
            {
                foreach (var track in tracks)
                {
                    var t = new TopoTrackInfo(this, track);
                    _tracks.Add(t);
                }
            }
        }
        public TopoTrailInfo(GpxFileData data)
        {
            Name        = data.Name;
            Description = data.Description;

            Keywords = data.Keywords;
            UrlText  = data.UrlText;
            UrlLink  = data.UrlLink;

            Timezone = GeoTimezoneInfo.Find(data.Extensions.Timezone);
            if (Timezone == null)
            {
                Timezone = GeoTimezoneInfo.UTC;
            }

            Country  = GeoCountryInfo.Find(data.Extensions.Country);
            Region   = GeoRegionInfo.Find(data.Extensions.Region);
            Location = data.Extensions.Location;

            foreach (var track in data.Tracks)
            {
                var t = new TopoTrackInfo(this, track);
                _tracks.Add(t);
            }
        }
        private IHttpActionResult GetTimezone(string id)
        {
            var t = GeoTimezoneInfo.Find(id);

            if (t == null)
            {
                t = Graffiti.Geo.FindTimezone(id);
            }
            if (t == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var d = MapTimezone(t);

            return(Ok(d));
        }
        public GeoPolitical(IGeoPoliticalData data)
        {
            _data = data;

            Timezone = GeoTimezoneInfo.Find(_data.Timezone);
            Country  = GeoCountryInfo.Find(_data.Country);
            Region   = GeoRegionInfo.Find(_data.Region);

            // attempt to backfill timezone
            if (Timezone == null && Region != null)
            {
                Timezone = Graffiti.Geo.GuessTimezone(Region);
            }
            if (Timezone == null && Country != null)
            {
                Timezone = Graffiti.Geo.GuessTimezone(Country);
            }
        }
        /// <summary>
        /// Updates the trail metadata and synchronizes file and cache
        /// </summary>
        public ValidationServiceResponse <TopoTrailInfo> UpdateTrail(ITopoTrailUpdateRequest request)
        {
            // validate update request
            var response = ValidateUpdate(request);

            if (response.HasErrors)
            {
                return(response);
            }

            // process update request
            var trail = response.Data;

            // save current filename for later
            var existing = GetFilename(trail);

            // check file system
            if (!Directory.Exists(_rootUri))
            {
                throw new Exception($"Directory not initalized: {_rootUri}");
            }
            var folder = Path.Combine(_rootUri, trail.Country.Name);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            // update trail properties
            trail.Timezone = GeoTimezoneInfo.Find(request.Timezone);
            trail.Country  = GeoCountryInfo.Find(request.Country);
            trail.Region   = GeoRegionInfo.Find(request.Region);

            trail.Name        = TextMutate.TrimSafe(request.Name);
            trail.Description = TextMutate.TrimSafe(request.Description);
            trail.Location    = TextMutate.TrimSafe(request.Location);
            trail.Keywords    = TextMutate.FixKeywords(request.Keywords);

            // TODO: BUG: fix url writing for v1.1 files!
            //trail.UrlLink = TextMutate.FixUrl(request.UrlLink);
            //trail.UrlText = TextMutate.TrimSafe(request.UrlText);

            // generate new and rename/remove existing files
            var filename = GetFilename(trail);

            // check if overwrite file
            var renamed = String.Compare(existing, filename, true) != 0;

            if (!renamed)
            {
                // temp rename current file
                File.Move(existing, existing + "~temp");
                existing = existing + "~temp";
            }

            // write new file
            var contents = BuildGpx(trail);

            File.WriteAllText(filename, contents);

            // delete old file
            File.Delete(existing);

            // refresh key and cache data
            if (renamed)
            {
                trail.Key = Path.GetFileNameWithoutExtension(filename).ToUpperInvariant();
                _trails.Remove(request.Key.ToUpperInvariant());
                _trails.Add(trail);
            }

            // return successful response
            return(response);
        }