1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::collections::HashMap;
use std::io::Write;
use std::os::raw::c_float;

use enum_map::{Enum, EnumMap};
use strum::{EnumIter, EnumProperty, IntoEnumIterator};
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;

use crate::components::colours::ColourMap;
use crate::components::megaphone::Preset;
use crate::components::megaphone::Preset::{Preset1, Preset2, Preset3, Preset4, Preset5, Preset6};

#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
    #[error("Expected int: {0}")]
    ExpectedInt(#[from] std::num::ParseIntError),

    #[error("Expected float: {0}")]
    ExpectedFloat(#[from] std::num::ParseFloatError),

    #[error("Expected enum: {0}")]
    ExpectedEnum(#[from] strum::ParseError),

    #[error("Invalid colours: {0}")]
    InvalidColours(#[from] crate::components::colours::ParseError),
}

/**
 * This is relatively static, main tag contains standard colour mapping, subtags contain various
 * presets, we'll use an EnumMap to define the 'presets' as they'll be useful for the other various
 * 'types' of presets (encoders and effects).
 */
#[derive(Debug)]
pub struct EchoEncoderBase {
    colour_map: ColourMap,
    preset_map: EnumMap<Preset, EchoEncoder>,
    active_set: u8, // Not sure what this does?
}

impl EchoEncoderBase {
    pub fn new(element_name: String) -> Self {
        let colour_map = element_name;
        Self {
            colour_map: ColourMap::new(colour_map),
            preset_map: EnumMap::default(),
            active_set: 0,
        }
    }

    pub fn parse_echo_root(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
        for attr in attributes {
            if attr.name.local_name == "active_set" {
                self.active_set = attr.value.parse()?;
                continue;
            }

            if !self.colour_map.read_colours(attr)? {
                println!("[EchoEncoder] Unparsed Attribute: {}", attr.name);
            }
        }

        Ok(())
    }

    pub fn parse_echo_preset(
        &mut self,
        id: u8,
        attributes: &[OwnedAttribute],
    ) -> Result<(), ParseError> {
        let mut preset = EchoEncoder::new();
        for attr in attributes {
            if attr.name.local_name == "DELAY_STYLE" {
                for style in EchoStyle::iter() {
                    if style.get_str("uiIndex").unwrap() == attr.value {
                        preset.style = style;
                        break;
                    }
                }
                continue;
            }

            if attr.name.local_name == "DELAY_KNOB_POSITION" {
                preset.knob_position = attr.value.parse::<c_float>()? as i8;
                continue;
            }

            if attr.name.local_name == "DELAY_SOURCE" {
                preset.source = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_DIV_L" {
                preset.div_l = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_DIV_R" {
                preset.div_r = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_FB_L" {
                preset.feedback_left = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_FB_R" {
                preset.feedback_right = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_XFB_L_R" {
                preset.xfb_l_to_r = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_XFB_R_L" {
                preset.xfb_r_to_l = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_FB_CONTROL" {
                preset.feedback_control = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_FILTER_STYLE" {
                preset.filter_style = attr.value.parse::<c_float>()? as u8;
                continue;
            }
            if attr.name.local_name == "DELAY_TIME_L" {
                preset.time_left = attr.value.parse::<c_float>()? as u16;
                continue;
            }
            if attr.name.local_name == "DELAY_TIME_R" {
                preset.time_right = attr.value.parse::<c_float>()? as u16;
                continue;
            }
            if attr.name.local_name == "DELAY_TEMPO" {
                preset.tempo = attr.value.parse::<c_float>()? as u16;
                continue;
            }

            println!(
                "[EchoEncoder] Unparsed Child Attribute: {}",
                &attr.name.local_name
            );
        }

        // Ok, we should be able to store this now..
        if id == 1 {
            self.preset_map[Preset1] = preset;
        } else if id == 2 {
            self.preset_map[Preset2] = preset;
        } else if id == 3 {
            self.preset_map[Preset3] = preset;
        } else if id == 4 {
            self.preset_map[Preset4] = preset;
        } else if id == 5 {
            self.preset_map[Preset5] = preset;
        } else if id == 6 {
            self.preset_map[Preset6] = preset;
        }

        Ok(())
    }

    pub fn write_echo<W: Write>(
        &self,
        writer: &mut EventWriter<&mut W>,
    ) -> Result<(), xml::writer::Error> {
        let mut element: StartElementBuilder = XmlWriterEvent::start_element("echoEncoder");

        let mut attributes: HashMap<String, String> = HashMap::default();
        attributes.insert("active_set".to_string(), format!("{}", self.active_set));
        self.colour_map.write_colours(&mut attributes);

        // Write out the attributes etc for this element, but don't close it yet..
        for (key, value) in &attributes {
            element = element.attr(key.as_str(), value.as_str());
        }

        writer.write(element)?;

        // Because all of these are seemingly 'guaranteed' to exist, we can straight dump..
        for (key, value) in &self.preset_map {
            let mut sub_attributes: HashMap<String, String> = HashMap::default();

            let tag_name = format!("echoEncoder{}", key.get_str("tagSuffix").unwrap());
            let mut sub_element: StartElementBuilder =
                XmlWriterEvent::start_element(tag_name.as_str());

            sub_attributes.insert(
                "DELAY_KNOB_POSITION".to_string(),
                format!("{}", value.knob_position),
            );
            sub_attributes.insert(
                "DELAY_STYLE".to_string(),
                value.style.get_str("uiIndex").unwrap().to_string(),
            );
            sub_attributes.insert("DELAY_SOURCE".to_string(), format!("{}", value.source));
            sub_attributes.insert("DELAY_DIV_L".to_string(), format!("{}", value.div_l));
            sub_attributes.insert("DELAY_DIV_R".to_string(), format!("{}", value.div_r));
            sub_attributes.insert("DELAY_FB_L".to_string(), format!("{}", value.feedback_left));
            sub_attributes.insert(
                "DELAY_FB_R".to_string(),
                format!("{}", value.feedback_right),
            );
            sub_attributes.insert("DELAY_XFB_L_R".to_string(), format!("{}", value.xfb_l_to_r));
            sub_attributes.insert("DELAY_XFB_R_L".to_string(), format!("{}", value.xfb_r_to_l));
            sub_attributes.insert(
                "DELAY_FB_CONTROL".to_string(),
                format!("{}", value.feedback_control),
            );
            sub_attributes.insert(
                "DELAY_FILTER_STYLE".to_string(),
                format!("{}", value.filter_style),
            );
            sub_attributes.insert("DELAY_TIME_L".to_string(), format!("{}", value.time_left));
            sub_attributes.insert("DELAY_TIME_R".to_string(), format!("{}", value.time_right));
            sub_attributes.insert("DELAY_TEMPO".to_string(), format!("{}", value.tempo));

            for (key, value) in &sub_attributes {
                sub_element = sub_element.attr(key.as_str(), value.as_str());
            }

            writer.write(sub_element)?;
            writer.write(XmlWriterEvent::end_element())?;
        }

        // Finally, close the 'main' tag.
        writer.write(XmlWriterEvent::end_element())?;

        Ok(())
    }

    pub fn colour_map(&self) -> &ColourMap {
        &self.colour_map
    }

    pub fn colour_map_mut(&mut self) -> &mut ColourMap {
        &mut self.colour_map
    }

    pub fn get_preset(&self, preset: Preset) -> &EchoEncoder {
        &self.preset_map[preset]
    }

    pub fn get_preset_mut(&mut self, preset: Preset) -> &mut EchoEncoder {
        &mut self.preset_map[preset]
    }
}

#[derive(Debug, Default)]
pub struct EchoEncoder {
    knob_position: i8,
    style: EchoStyle,
    source: u8,
    div_l: u8,
    div_r: u8,
    feedback_left: u8,
    feedback_right: u8,
    feedback_control: u8,
    xfb_l_to_r: u8,
    xfb_r_to_l: u8,
    filter_style: u8,
    time_left: u16,
    time_right: u16,
    tempo: u16,
}

impl EchoEncoder {
    pub fn new() -> Self {
        Self {
            knob_position: 0,
            style: EchoStyle::Quarter,
            source: 0,
            div_l: 0,
            div_r: 0,
            feedback_left: 0,
            feedback_right: 0,
            feedback_control: 0,
            xfb_l_to_r: 0,
            xfb_r_to_l: 0,
            filter_style: 0,
            time_left: 0,
            time_right: 0,
            tempo: 0,
        }
    }

    pub fn amount(&self) -> i8 {
        ((36 * self.knob_position as i32) / 24 - 36) as i8
    }

    pub fn knob_position(&self) -> i8 {
        self.knob_position
    }

    pub fn set_knob_position(&mut self, knob_position: i8) {
        self.knob_position = knob_position;
    }

    pub fn style(&self) -> &EchoStyle {
        &self.style
    }
    pub fn source(&self) -> u8 {
        self.source
    }
    pub fn div_l(&self) -> u8 {
        self.div_l
    }
    pub fn div_r(&self) -> u8 {
        self.div_r
    }
    pub fn feedback_left(&self) -> u8 {
        self.feedback_left
    }
    pub fn feedback_right(&self) -> u8 {
        self.feedback_right
    }
    pub fn feedback_control(&self) -> u8 {
        self.feedback_control
    }
    pub fn xfb_l_to_r(&self) -> u8 {
        self.xfb_l_to_r
    }
    pub fn xfb_r_to_l(&self) -> u8 {
        self.xfb_r_to_l
    }
    pub fn filter_style(&self) -> u8 {
        self.filter_style
    }
    pub fn time_left(&self) -> u16 {
        self.time_left
    }
    pub fn time_right(&self) -> u16 {
        self.time_right
    }
    pub fn tempo(&self) -> u16 {
        self.tempo
    }
}

#[derive(Debug, EnumIter, Enum, EnumProperty)]
pub enum EchoStyle {
    #[strum(props(uiIndex = "0"))]
    #[strum(to_string = "QUARTER")]
    Quarter,

    #[strum(props(uiIndex = "1"))]
    #[strum(to_string = "EIGHTH")]
    Eighth,

    #[strum(props(uiIndex = "2"))]
    #[strum(to_string = "TRIPLET")]
    Triplet,

    #[strum(props(uiIndex = "3"))]
    #[strum(to_string = "PING_PONG")]
    PingPong,

    #[strum(props(uiIndex = "4"))]
    #[strum(to_string = "CLASSIC_SLAP")]
    ClassicSlap,

    #[strum(props(uiIndex = "5"))]
    #[strum(to_string = "MULTI_TAP")]
    MultiTap,
}

impl Default for EchoStyle {
    fn default() -> Self {
        EchoStyle::Quarter
    }
}