private void ParseActions(XElement root, Dictionary<Event, List<Actions.Action>> events, Event ev)
{
foreach (var notify in root.Element("actions").Elements("notify"))
{
Balloon ballonData = null;
var balloon = notify.Element("balloon");
if (balloon != null)
{
var titleText = balloon.Attribute("title_text").Value;
var bodyText = balloon.Attribute("body_text").Value;
var displayDuration = int.Parse(balloon.Attribute("display_time").Value);
ballonData = new Balloon(titleText, bodyText, displayDuration);
}
SoundInterface soundInterface = null;
var music = notify.Descendants("music");
var beeps = notify.Descendants("beeps");
var textToSpeech = notify.Descendants("text_to_speech");
if ((music.Any() && beeps.Any()) || ((music.Any() && textToSpeech.Any()) || textToSpeech.Any() && beeps.Any()))
{
throw new Exception("Only 1 type of sound allowed by notifyAction");
}
if (music.Any())
{
var musicFile = music.First().Attribute("file").Value;
var volume = float.Parse(music.First().Attribute("volume").Value,CultureInfo.InvariantCulture)/100;
var duration = int.Parse(music.First().Attribute("duration").Value);
soundInterface = new Music(musicFile, volume, duration);
}
if (beeps.Any())
{
var beepsList = new List<Beep>();
foreach (var beep in beeps.First().Elements())
{
var frequency = int.Parse(beep.Attribute("frequency").Value);
var duration = int.Parse(beep.Attribute("duration").Value);
beepsList.Add(new Beep(frequency, duration));
}
soundInterface = new Beeps(beepsList);
}
if (textToSpeech.Any())
{
var tts = textToSpeech.First();
var text = tts.Attribute("text").Value;
var voiceGender = (VoiceGender)Enum.Parse(typeof(VoiceGender), tts.Attribute("voice_gender")?.Value ?? "Female", true);
var voiceAge = (VoiceAge)Enum.Parse(typeof(VoiceAge), tts.Attribute("voice_age")?.Value ?? "Adult", true);
var culture = tts.Attribute("culture")?.Value ?? CultureInfo.CurrentUICulture.ToString();
var voicePosition = int.Parse(tts.Attribute("voice_position")?.Value ?? "0");
var volume = int.Parse(tts.Attribute("volume")?.Value ?? "30");
var rate = int.Parse(tts.Attribute("rate")?.Value ?? "0");
soundInterface = new TextToSpeech(text, voiceGender, voiceAge, voicePosition, culture, volume, rate);
}
var notifyAction = new NotifyAction(soundInterface, ballonData);
events[ev].Add(notifyAction);
}
}