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
use std::collections::HashMap;
use std::str::FromStr;
use std::io::Write;


use enum_map::Enum;
use ritelinked::LinkedHashMap;
use strum::{EnumProperty, EnumString, Display, EnumIter};
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;

use crate::components::colours::ColourMap;

#[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 SampleBase {
    element_name: String,
    colour_map: ColourMap,
    state: String, // Seems to be "Empty" most of the time..
    sample_stack: HashMap<SampleBank, SampleStack>,
}

impl SampleBase {
    pub fn new(element_name: String) -> Self {
        let colour_map = element_name.clone();
        Self {
            element_name,
            colour_map: ColourMap::new(colour_map),
            state: "Empty".to_string(),
            sample_stack: Default::default(),
        }
    }

    pub fn parse_sample_root(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
        for attr in attributes {
            if attr.name.local_name.ends_with("state") && self.element_name != "sampleClear" {
                if attr.value != "Empty" && attr.value != "Stopped" {
                    println!("[Sampler] Unknown State: {}", &attr.value);
                }
                self.state = attr.value.clone();
                continue;
            }

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

        Ok(())
    }

    pub fn parse_sample_stack(
        &mut self,
        id: char,
        attributes: &[OwnedAttribute],
    ) -> Result<(), ParseError> {
        // The easiest way to handle this is to parse everything into key-value pairs, then try
        // to locate all the settings for each track inside it..
        let mut map: HashMap<String, String> = HashMap::default();

        for attr in attributes {
            map.insert(attr.name.local_name.clone(), attr.value.clone());
        }

        let mut sample_stack = SampleStack::new();

        // Pull out any 'extra' attributes which may be useful..
        if let Some(value) = map.get("playbackMode") {
            sample_stack.playback_mode =
                Option::Some(PlaybackMode::from_usize(value.parse::<usize>()?));
        }

        if let Some(value) = map.get("playOrder") {
            sample_stack.play_order = Option::Some(PlayOrder::from_usize(value.parse::<usize>()?));
        }

        // Ok, somewhere in here we should have a key that tells us how many tracks are configured..
        let key = format!("sampleStack{}stackSize", id);

        if !map.contains_key(key.as_str()) {
            // Stack doesn't contain any tracks, we're done here.
            self.sample_stack.insert(SampleBank::from_str(id.to_string().as_str())?, sample_stack);
            return Ok(());
        }

        if let Some(track_count) = map.get(key.as_str()) {
            let track_count: u8 = track_count.parse()?;
            for i in 0..track_count {
                if let (Some(track), Some(start), Some(end), Some(gain)) = (
                    map.get(&format!("track_{}", i)),
                    map.get(&format!("track_{}StartPosition", i)),
                    map.get(&format!("track_{}EndPosition", i)),
                    map.get(&format!("track_{}NormalizedGain", i)),
                ) {
                    let track = Track::new(
                        track.to_string(),
                        start.parse()?,
                        end.parse()?,
                        gain.parse()?,
                    );
                    sample_stack.tracks.push(track);
                }
            }
        }

        self.sample_stack.insert(SampleBank::from_str(id.to_string().as_str())?, sample_stack);

        Ok(())
    }

    pub fn write_sample<W: Write>(
        &self,
        writer: &mut EventWriter<&mut W>,
    ) -> Result<(), xml::writer::Error> {
        let mut element: StartElementBuilder =
            XmlWriterEvent::start_element(self.element_name.as_str());

        let mut attributes: HashMap<String, String> = HashMap::default();
        attributes.insert(
            format!("{}state", self.element_name),
            self.state.to_string(),
        );
        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)?;

        // Now onto the damn stacks..
        for (key, value) in &self.sample_stack {
            let sub_element_name = format!("sampleStack{}", key.to_string());

            let mut sub_element = XmlWriterEvent::start_element(sub_element_name.as_str());

            // Welcome to the only place where order seems to matter, the track_X attributes must all appear together
            // in an ordered, unbroken list, otherwise the GoXLR App will crash :D
            let mut sub_attributes: LinkedHashMap<String, String> = Default::default();

            for i in 0..value.tracks.len() {
                sub_attributes.insert(
                    format!("track_{}", i),
                    value.tracks.get(i).unwrap().track.to_string(),
                );
            }

            if !value.tracks.is_empty() {
                sub_attributes.insert(
                    format!("sampleStack{}stackSize", key),
                    format!("{}", value.tracks.len()),
                );
            }

            for i in 0..value.tracks.len() {
                sub_attributes.insert(
                    format!("track_{}NormalizedGain", i),
                    format!("{}", value.tracks.get(i).unwrap().normalized_gain),
                );
                sub_attributes.insert(
                    format!("track_{}StartPosition", i),
                    format!("{}", value.tracks.get(i).unwrap().start_position),
                );
                sub_attributes.insert(
                    format!("track_{}EndPosition", i),
                    format!("{}", value.tracks.get(i).unwrap().end_position),
                );
            }

            if let Some(output) = &value.playback_mode {
                sub_attributes.insert(
                    "playbackMode".to_string(),
                    output.get_str("index").unwrap().to_string(),
                );
            }

            if let Some(order) = &value.play_order {
                sub_attributes.insert(
                    "playOrder".to_string(),
                    order.get_str("index").unwrap().to_string(),
                );
            }

            // Write the attributes into the tag, and close it.
            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())?;
        }

        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_stack(&self, bank: SampleBank) -> &SampleStack {
        self.sample_stack.get(&bank).unwrap()
    }
}

#[derive(Debug)]
pub struct SampleStack {
    tracks: Vec<Track>,
    playback_mode: Option<PlaybackMode>,
    play_order: Option<PlayOrder>,
}

impl SampleStack {
    pub fn new() -> Self {
        Self {
            tracks: vec![],
            playback_mode: None,
            play_order: None,
        }
    }

    pub fn get_sample_count(&self) -> usize {
        return self.tracks.len();
    }

    pub fn get_first_sample_file(&self) -> String {
        self.tracks[0].track.to_string()
    }
}

#[derive(Debug)]
struct Track {
    track: String,
    start_position: u8,
    end_position: u8,
    normalized_gain: f64,
}

impl Track {
    pub fn new(track: String, start_position: u8, end_position: u8, normalized_gain: f64) -> Self {
        Self {
            track,
            start_position,
            end_position,
            normalized_gain,
        }
    }
}

#[derive(Debug, Enum, EnumProperty)]
enum PlaybackMode {
    #[strum(props(index = "0"))]
    PlayNext,
    #[strum(props(index = "1"))]
    PlayStop,
    #[strum(props(index = "2"))]
    PlayFade,
    #[strum(props(index = "3"))]
    StopOnRelease,
    #[strum(props(index = "4"))]
    FadeOnRelease,
    #[strum(props(index = "5"))]
    Loop,
}

#[derive(Debug, Enum, EnumProperty)]
enum PlayOrder {
    #[strum(props(index = "0"))]
    Sequential,
    #[strum(props(index = "1"))]
    Random,
}

#[derive(Debug, Copy, Clone, Display, Enum, EnumString, EnumProperty, EnumIter, PartialEq, Eq, Hash)]
pub enum SampleBank {
    #[strum(props(contextTitle = "sampleStackA"))]
    A,
    #[strum(props(contextTitle = "sampleStackB"))]
    B,
    #[strum(props(contextTitle = "sampleStackC"))]
    C
}