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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, AgendaMapProvider = agendaDTO.agendaMapProvider }, SectionType.Article => new SectionArticle { Id = dto.id, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, ArticleIsContentTop = articleDTO.isContentTop, ArticleAudioIds = articleDTO.audioIds, ArticleIsReadAudioAuto = articleDTO.isReadAudioAuto, ArticleContents = articleDTO.contents }, SectionType.Map => new SectionMap { Id = dto.id, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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 }, SectionType.Puzzle => new SectionPuzzle { Id = dto.id, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, QuizMediumLevel = quizDTO.medium_level, QuizGoodLevel = quizDTO.good_level, QuizGreatLevel = quizDTO.great_level, //Questions = ((QuizDTO)dto).questions, // TODO specific }, SectionType.Slider => new SectionSlider { Id = dto.id, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, // TODO TEST }, SectionType.Video => new SectionVideo { Id = dto.id, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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, DateCreation = dto.dateCreation, ConfigurationId = dto.configurationId, InstanceId = dto.instanceId, Label = dto.label, Title = dto.title, Description = dto.description, 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 object ToDTO(Section section) { // TODO retrieve specific elements ? return section switch { SectionAgenda agenda => new AgendaDTO { id = agenda.Id, dateCreation = agenda.DateCreation, configurationId = agenda.ConfigurationId, instanceId = agenda.InstanceId, label = agenda.Label, title = agenda.Title, description = agenda.Description, 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, agendaMapProvider = agenda.AgendaMapProvider }, SectionArticle article => new ArticleDTO { id = article.Id, dateCreation = article.DateCreation, configurationId = article.ConfigurationId, instanceId = article.InstanceId, label = article.Label, title = article.Title, description = article.Description, 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, isContentTop = article.ArticleIsContentTop, audioIds = article.ArticleAudioIds, isReadAudioAuto = article.ArticleIsReadAudioAuto, contents = article.ArticleContents }, SectionMap map => new MapDTO { id = map.Id, dateCreation = map.DateCreation, configurationId = map.ConfigurationId, instanceId = map.InstanceId, label = map.Label, title = map.Title, description = map.Description, 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, dateCreation = menu.DateCreation, configurationId = menu.ConfigurationId, instanceId = menu.InstanceId, label = menu.Label, title = menu.Title, description = menu.Description, 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, dateCreation = pdf.DateCreation, configurationId = pdf.ConfigurationId, instanceId = pdf.InstanceId, label = pdf.Label, title = pdf.Title, description = pdf.Description, 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 }, SectionPuzzle puzzle => new PuzzleDTO { id = puzzle.Id, dateCreation = puzzle.DateCreation, configurationId = puzzle.ConfigurationId, instanceId = puzzle.InstanceId, label = puzzle.Label, title = puzzle.Title, description = puzzle.Description, 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, dateCreation = quiz.DateCreation, configurationId = quiz.ConfigurationId, instanceId = quiz.InstanceId, label = quiz.Label, title = quiz.Title, description = quiz.Description, 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, medium_level = quiz.QuizMediumLevel, good_level = quiz.QuizGoodLevel, great_level = quiz.QuizGreatLevel, questions = null // quiz.Questions, // TODO specific }, SectionSlider slider => new SliderDTO { id = slider.Id, dateCreation = slider.DateCreation, configurationId = slider.ConfigurationId, instanceId = slider.InstanceId, label = slider.Label, title = slider.Title, description = slider.Description, 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 }, SectionVideo video => new VideoDTO { id = video.Id, dateCreation = video.DateCreation, configurationId = video.ConfigurationId, instanceId = video.InstanceId, label = video.Label, title = video.Title, description = video.Description, 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, dateCreation = weather.DateCreation, configurationId = weather.ConfigurationId, instanceId = weather.InstanceId, label = weather.Label, title = weather.Title, description = weather.Description, 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, dateCreation = web.DateCreation, configurationId = web.ConfigurationId, instanceId = web.InstanceId, label = web.Label, title = web.Title, description = web.Description, 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") }; } } }