public PageApiViewModel GetPage(long id)
        {
            var pagesRepository = new PagesRepository();
            var model = pagesRepository.GetById(id);

            return new PageApiViewModel(model);
        }
        // GET: BaseLayers/Create
        public ActionResult ExternalAttachPageContent(long id)
        {
            var pageRepository = new PagesRepository();
            var page = pageRepository.GetById(id);

            return View(new PageViewModel(page));
        }
        // GET: BaseLayers/Create
        public async Task<ActionResult> ExternalAttach(long id)
        {
            var tildaClient = new TildaClient(Settings.Default.PublicKey, Settings.Default.SecretKey);

            var pages = await tildaClient.GetPagesList(Settings.Default.TildaProjectId);

            var pageRepository = new PagesRepository();
            var page = pageRepository.GetById(id);
            

            ViewBag.ListOfTildaPages = pages.Select(x => new SelectListItem()
            {
                Value = x.Id.ToString(),
                Text = x.Title,
                Selected = page.ExternalId != null && string.Equals(x.Id, page.ExternalId)
            });

            var model = new ExternalAttachViewModel()
            {
                Id = id,
                PageId = !string.IsNullOrEmpty(page.ExternalId) ? long.Parse(page.ExternalId) : (long?)null
            };

            return PartialView(model);
        }
        // GET: BaseLayers/Details/5
        public ActionResult Details(long id)
        {
            var pagesRepository = new PagesRepository();
            var pointsRepository = new PointsRepository();
            var regionsRepository = new RegionsRepository();

            var model = pagesRepository.GetById(id);

            var point = pointsRepository.GetByPage(id);
            var region = regionsRepository.GetByPage(id);

            if (model != null)
                return View(new PageViewModel(model, point, region));
            else
                return HttpNotFound();
        }
        public ActionResult ChangeAttach(ChangeAttachPageViewModel model)
        {
            var pageRepository = new PagesRepository();
            var pointRepository = new PointsRepository();

            var point = pointRepository.GetById(model.Id);

            if (model.PageId.HasValue && model.PageId != point.PageId)
            {
                var page = pageRepository.GetById(model.PageId.Value);

                point.PageId = page.Id;
                point.Page = page;

                pointRepository.Update(point);
            }
            else
            {
                if (model.OldPageId.HasValue)
                {
                    point.PageId = null;
                    point.Page = null;

                    pointRepository.Update(point);
                }
            }

            return RedirectToAction("Details", new { id = model.Id });
        }
        public ActionResult ChangeAttach(ChangeAttachPageViewModel model)
        {
            var pageRepository = new PagesRepository();
            var regionRepository = new RegionsRepository();

            var region = regionRepository.GetById(model.Id);

            if (model.PageId.HasValue && model.PageId != region.PageId)
            {
                var page = pageRepository.GetById(model.PageId.Value);

                region.PageId = page.Id;
                region.Page = page;

                regionRepository.Update(region);
            }
            else
            {
                if (model.OldPageId.HasValue)
                {
                    region.PageId = null;
                    region.Page = null;

                    regionRepository.Update(region);
                }
            }

            return RedirectToAction("Details", new { id = model.Id });
        }
        // GET: BaseLayers/Delete/5
        public ActionResult Delete(int id)
        {
            var pagesRepository = new PagesRepository();

            var model = pagesRepository.GetById(id);

            if (model != null)
                return View(model);
            return HttpNotFound();
        }
        public ActionResult Edit(EditPageViewModel model)
        {
            try
            {
                var tagsRepository = new TagsRepository();

                var pagesRepository = new PagesRepository();
                var originalPage = pagesRepository.GetById(model.Id);

                if (model.SelectedTags != null)
                {
                    var tags = tagsRepository.GetByStringIds(model.SelectedTags);
                    originalPage.Tags = new List<Tag>(tags);
                }
                else
                {
                    originalPage.Tags = null;
                }

                originalPage.Name = model.Name;
                originalPage.Description = model.Description;
                originalPage.Content = model.Content;
                originalPage.ExternalId = model.ExternalId;

                pagesRepository.Update(originalPage);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
        // GET: BaseLayers/Edit/5
        public ActionResult Edit(long id)
        {
            var pagesRepository = new PagesRepository();

            var model = pagesRepository.GetById(id);
            var tagIds = model.Tags != null ? model.Tags.Select(x => x.Id) : new List<long>();
            
            var tagsRepository = new TagsRepository();
            IDisposable context = null;

            ViewBag.ListOfTags = tagsRepository.GetAll(out context).ToList().Select(x => new SelectListItem()
            {
                Value = x.Id.ToString(),
                Text = x.Name,
                Selected = tagIds.Contains(x.Id)
            });

            context?.Dispose();

            if (model != null)
                return View(new EditPageViewModel(model));
            return HttpNotFound();
        }
        public ActionResult ExternalDeattach(long id)
        {
            var pageRepository = new PagesRepository();

            var page = pageRepository.GetById(id);

            page.Content = null;
            page.ExternalId = null;

            pageRepository.Update(page);

            return RedirectToAction("Details", new { id });
        }
        public async Task<ActionResult> ExternalAttach(ExternalAttachViewModel model)
        {
            var pageRepository = new PagesRepository();

            var page = pageRepository.GetById(model.Id);

            var tildaClient = new TildaClient(Settings.Default.PublicKey, Settings.Default.SecretKey);
            var tildaPage = await tildaClient.GetPageFull(model.PageId.Value);

            page.Content = tildaPage.Html;
            page.ExternalId = tildaPage.Id;

            pageRepository.Update(page);

            return RedirectToAction("Details", new { id = model.Id });
        }