using Manager.DTOs; using ManagerService.Data; using ManagerService.Data.SubSection; using ManagerService.DTOs; using MongoDB.Bson; using Newtonsoft.Json; using System; using System.Linq; using System.Text.Json; namespace ManagerService.Services { public static class SectionFactory { public static Section Create(JsonElement jsonElement, SectionDTO dto) { AgendaDTO agendaDTO = new AgendaDTO(); ArticleDTO articleDTO = new ArticleDTO(); MapDTO mapDTO = new MapDTO(); MenuDTO menuDTO = new MenuDTO(); PdfDTO pdfDTO = new PdfDTO(); PuzzleDTO puzzleDTO = new PuzzleDTO(); QuizDTO quizDTO = new QuizDTO(); SliderDTO sliderDTO = new SliderDTO(); VideoDTO videoDTO = new VideoDTO(); WeatherDTO weatherDTO = new WeatherDTO(); WebDTO webDTO = new WebDTO(); switch (dto.type) { case SectionType.Agenda: agendaDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Article: articleDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Map: mapDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Menu: menuDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.PDF: pdfDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Puzzle: puzzleDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Quiz: quizDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Slider: sliderDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Video: videoDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Weather: weatherDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; case SectionType.Web: webDTO = JsonConvert.DeserializeObject(jsonElement.ToString()); break; } return dto.type switch { SectionType.Agenda => new SectionAgenda { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, AgendaResourceIds = agendaDTO.resourceIds.Select(r => new Translation().FromDTO(r)).ToList(), AgendaMapProvider = agendaDTO.agendaMapProvider }, SectionType.Article => new SectionArticle { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, ArticleContent = articleDTO.content.Select(r => new Translation().FromDTO(r)).ToList(), ArticleIsContentTop = articleDTO.isContentTop, ArticleAudioIds = articleDTO.audioIds.Select(a => new Translation().FromDTO(a)).ToList(), ArticleIsReadAudioAuto = articleDTO.isReadAudioAuto, ArticleContents = articleDTO.contents.Select(c => new Content().FromDTO(c)).ToList() }, SectionType.Map => new SectionMap { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, MapZoom = mapDTO.zoom, MapMapType = mapDTO.mapType, MapTypeMapbox = mapDTO.mapTypeMapbox, MapMapProvider = mapDTO.mapProvider, MapResourceId = mapDTO.iconResourceId, MapCenterLatitude = mapDTO.centerLatitude, MapCenterLongitude = mapDTO.centerLongitude, MapCategories = null, //((MapDTO)dto).categories, // TODO specific MapPoints = null // ((MapDTO)dto).points, // TODO specific }, SectionType.Menu => new SectionMenu { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, //Sections = ((MenuDTO)dto).sections, // TODO specific }, SectionType.PDF => new SectionPdf { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, PDFOrderedTranslationAndResources = pdfDTO.pdfs.Select(p => new OrderedTranslationAndResource().FromDTO(p)).ToList() }, SectionType.Puzzle => new SectionPuzzle { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, PuzzleMessageDebut = puzzleDTO.messageDebut.Select(md => new TranslationAndResource().FromDTO(md)).ToList(), PuzzleMessageFin = puzzleDTO.messageFin.Select(mf => new TranslationAndResource().FromDTO(mf)).ToList(), PuzzleImageId = puzzleDTO.puzzleImageId, PuzzleRows = puzzleDTO.rows, PuzzleCols = puzzleDTO.cols }, SectionType.Quiz => new SectionQuiz { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, QuizBadLevel = quizDTO.bad_level.Select(bl => new TranslationAndResource().FromDTO(bl)).ToList(), QuizMediumLevel = quizDTO.medium_level.Select(ml => new TranslationAndResource().FromDTO(ml)).ToList(), QuizGoodLevel = quizDTO.good_level.Select(gol => new TranslationAndResource().FromDTO(gol)).ToList(), QuizGreatLevel = quizDTO.great_level.Select(gl => new TranslationAndResource().FromDTO(gl)).ToList(), //Questions = ((QuizDTO)dto).questions, // TODO specific }, SectionType.Slider => new SectionSlider { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, SliderContents = sliderDTO.contents.Select(c => new Content().FromDTO(c)).ToList(), // TODO TEST }, SectionType.Video => new SectionVideo { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, VideoSource = videoDTO.source, }, SectionType.Weather => new SectionWeather { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, WeatherCity = weatherDTO.city, WeatherUpdatedDate = weatherDTO.updatedDate, WeatherResult = weatherDTO.result }, SectionType.Web => new SectionWeb { Id = dto.id, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title.Select(t => new Translation().FromDTO(t)).ToList(), Description = dto.description.Select(d => new Translation().FromDTO(d)).ToList(), Order = dto.order.Value, ImageId = dto.imageId, ImageSource = dto.imageSource, IsSubSection = dto.isSubSection, ParentId = dto.parentId, IsBeacon = dto.isBeacon, BeaconId = dto.beaconId, Latitude = dto.latitude, Longitude = dto.longitude, MeterZoneGPS = dto.meterZoneGPS, Type = dto.type, WebSource = webDTO.source, }, _ => throw new NotImplementedException("Section type not handled") }; } public static SectionDTO ToDTO(Section section) { return section switch { SectionAgenda agenda => new AgendaDTO { id = agenda.Id, configurationId = agenda.ConfigurationId, instanceId = agenda.InstanceId, label = agenda.Label, title = agenda.Title.Select(t => t.ToDTO()).ToList(), description = agenda.Description.Select(t => t.ToDTO()).ToList(), order = agenda.Order, imageId = agenda.ImageId, imageSource = agenda.ImageSource, isSubSection = agenda.IsSubSection, parentId = agenda.ParentId, isBeacon = agenda.IsBeacon, beaconId = agenda.BeaconId, latitude = agenda.Latitude, longitude = agenda.Longitude, meterZoneGPS = agenda.MeterZoneGPS, type = agenda.Type, resourceIds = agenda.AgendaResourceIds.Select(r => r.ToDTO()).ToList(), agendaMapProvider = agenda.AgendaMapProvider }, SectionArticle article => new ArticleDTO { id = article.Id, configurationId = article.ConfigurationId, instanceId = agenda.InstanceId, label = article.Label, title = article.Title.Select(t => t.ToDTO()).ToList(), description = article.Description.Select(d => d.ToDTO()).ToList(), order = article.Order, imageId = article.ImageId, imageSource = article.ImageSource, isSubSection = article.IsSubSection, parentId = article.ParentId, isBeacon = article.IsBeacon, beaconId = article.BeaconId, latitude = article.Latitude, longitude = article.Longitude, meterZoneGPS = article.MeterZoneGPS, type = article.Type, content = article.ArticleContent.Select(r => r.ToDTO()).ToList(), isContentTop = article.ArticleIsContentTop, audioIds = article.ArticleAudioIds.Select(a => a.ToDTO()).ToList(), isReadAudioAuto = article.ArticleIsReadAudioAuto, contents = article.ArticleContents.Select(c => c.ToDTO()).ToList() }, SectionMap map => new MapDTO { id = map.Id, configurationId = map.ConfigurationId, label = map.Label, title = map.Title.Select(t => t.ToDTO()).ToList(), description = map.Description.Select(d => d.ToDTO()).ToList(), order = map.Order, imageId = map.ImageId, imageSource = map.ImageSource, isSubSection = map.IsSubSection, parentId = map.ParentId, isBeacon = map.IsBeacon, beaconId = map.BeaconId, latitude = map.Latitude, longitude = map.Longitude, meterZoneGPS = map.MeterZoneGPS, type = map.Type, zoom = map.MapZoom, mapType = map.MapMapType, mapTypeMapbox = map.MapTypeMapbox, mapProvider = map.MapMapProvider, iconResourceId = map.MapResourceId, centerLatitude = map.MapCenterLatitude, centerLongitude = map.MapCenterLongitude, categories = null, // map.MapCategories, // TODO specific points = null // map.MapPoints // TODO specific }, SectionMenu menu => new MenuDTO { id = menu.Id, configurationId = menu.ConfigurationId, label = menu.Label, title = menu.Title.Select(t => t.ToDTO()).ToList(), description = menu.Description.Select(d => d.ToDTO()).ToList(), order = menu.Order, imageId = menu.ImageId, imageSource = menu.ImageSource, isSubSection = menu.IsSubSection, parentId = menu.ParentId, isBeacon = menu.IsBeacon, beaconId = menu.BeaconId, latitude = menu.Latitude, longitude = menu.Longitude, meterZoneGPS = menu.MeterZoneGPS, type = menu.Type, sections = null // menu.Sections, // TODO specific }, SectionPdf pdf => new PdfDTO { id = pdf.Id, configurationId = pdf.ConfigurationId, label = pdf.Label, title = pdf.Title.Select(t => t.ToDTO()).ToList(), description = pdf.Description.Select(d => d.ToDTO()).ToList(), order = pdf.Order, imageId = pdf.ImageId, imageSource = pdf.ImageSource, isSubSection = pdf.IsSubSection, parentId = pdf.ParentId, isBeacon = pdf.IsBeacon, beaconId = pdf.BeaconId, latitude = pdf.Latitude, longitude = pdf.Longitude, meterZoneGPS = pdf.MeterZoneGPS, type = pdf.Type, pdfs = pdf.PDFOrderedTranslationAndResources.Select(p => p.ToDTO()).ToList() }, SectionPuzzle puzzle => new PuzzleDTO { id = puzzle.Id, configurationId = puzzle.ConfigurationId, label = puzzle.Label, title = puzzle.Title.Select(t => t.ToDTO()).ToList(), description = puzzle.Description.Select(d => d.ToDTO()).ToList(), order = puzzle.Order, imageId = puzzle.ImageId, imageSource = puzzle.ImageSource, isSubSection = puzzle.IsSubSection, parentId = puzzle.ParentId, isBeacon = puzzle.IsBeacon, beaconId = puzzle.BeaconId, latitude = puzzle.Latitude, longitude = puzzle.Longitude, meterZoneGPS = puzzle.MeterZoneGPS, type = puzzle.Type, messageDebut = puzzle.PuzzleMessageDebut.Select(md => md.ToDTO()).ToList(), messageFin = puzzle.PuzzleMessageFin.Select(mf => mf.ToDTO()).ToList(), puzzleImageId = puzzle.PuzzleImageId, rows = puzzle.PuzzleRows, cols = puzzle.PuzzleCols }, SectionQuiz quiz => new QuizDTO { id = quiz.Id, configurationId = quiz.ConfigurationId, label = quiz.Label, title = quiz.Title.Select(t => t.ToDTO()).ToList(), description = quiz.Description.Select(d => d.ToDTO()).ToList(), order = quiz.Order, imageId = quiz.ImageId, imageSource = quiz.ImageSource, isSubSection = quiz.IsSubSection, parentId = quiz.ParentId, isBeacon = quiz.IsBeacon, beaconId = quiz.BeaconId, latitude = quiz.Latitude, longitude = quiz.Longitude, meterZoneGPS = quiz.MeterZoneGPS, type = quiz.Type, bad_level = quiz.QuizBadLevel.Select(bl => bl.ToDTO()).ToList(), medium_level = quiz.QuizMediumLevel.Select(ml => ml.ToDTO()).ToList(), good_level = quiz.QuizGoodLevel.Select(gol => gol.ToDTO()).ToList(), great_level = quiz.QuizGreatLevel.Select(gl => gl.ToDTO()).ToList(), questions = null // quiz.Questions, // TODO specific }, SectionSlider slider => new SliderDTO { id = slider.Id, configurationId = slider.ConfigurationId, label = slider.Label, title = slider.Title.Select(t => t.ToDTO()).ToList(), description = slider.Description.Select(d => d.ToDTO()).ToList(), order = slider.Order, imageId = slider.ImageId, imageSource = slider.ImageSource, isSubSection = slider.IsSubSection, parentId = slider.ParentId, isBeacon = slider.IsBeacon, beaconId = slider.BeaconId, latitude = slider.Latitude, longitude = slider.Longitude, meterZoneGPS = slider.MeterZoneGPS, type = slider.Type, contents = slider.SliderContents.Select(c => c.ToDTO()).ToList() }, SectionVideo video => new VideoDTO { id = video.Id, configurationId = video.ConfigurationId, label = video.Label, title = video.Title.Select(t => t.ToDTO()).ToList(), description = video.Description.Select(d => d.ToDTO()).ToList(), order = video.Order, imageId = video.ImageId, imageSource = video.ImageSource, isSubSection = video.IsSubSection, parentId = video.ParentId, isBeacon = video.IsBeacon, beaconId = video.BeaconId, latitude = video.Latitude, longitude = video.Longitude, meterZoneGPS = video.MeterZoneGPS, type = video.Type, source = video.VideoSource }, SectionWeather weather => new WeatherDTO { id = weather.Id, configurationId = weather.ConfigurationId, label = weather.Label, title = weather.Title.Select(t => t.ToDTO()).ToList(), description = weather.Description.Select(d => d.ToDTO()).ToList(), order = weather.Order, imageId = weather.ImageId, imageSource = weather.ImageSource, isSubSection = weather.IsSubSection, parentId = weather.ParentId, isBeacon = weather.IsBeacon, beaconId = weather.BeaconId, latitude = weather.Latitude, longitude = weather.Longitude, meterZoneGPS = weather.MeterZoneGPS, type = weather.Type, city = weather.WeatherCity, updatedDate = weather.WeatherUpdatedDate, result = weather.WeatherResult }, SectionWeb web => new WebDTO { id = web.Id, configurationId = web.ConfigurationId, label = web.Label, title = web.Title.Select(t => t.ToDTO()).ToList(), description = web.Description.Select(d => d.ToDTO()).ToList(), order = web.Order, imageId = web.ImageId, imageSource = web.ImageSource, isSubSection = web.IsSubSection, parentId = web.ParentId, isBeacon = web.IsBeacon, beaconId = web.BeaconId, latitude = web.Latitude, longitude = web.Longitude, meterZoneGPS = web.MeterZoneGPS, type = web.Type, source = web.WebSource }, _ => throw new NotImplementedException("Section type not handled") }; } } }