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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::time::Duration;

use crate::{GenCamError, GenCamPixelBpp, Result};
use serde::{Deserialize, Serialize};

use crate::controls::GenCamCtrl;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// A property
pub struct Property {
    name: GenCamCtrl,
    auto: bool,
    prop: PropertyLims,
}

impl Property {
    /// Create a new property
    pub fn new<T>(name: T, prop: PropertyLims, auto: bool) -> Self
    where
        T: Into<GenCamCtrl>,
    {
        Property {
            name: name.into(),
            auto,
            prop,
        }
    }

    /// Get the name of the property
    pub fn get_name(&self) -> GenCamCtrl {
        self.name
    }

    /// Get the type of the property
    pub fn get_type(&self) -> PropertyType {
        (&self.prop).into()
    }

    /// Check if the property supports auto mode
    pub fn supports_auto(&self) -> bool {
        self.auto
    }

    /// Get the minimum value of the property
    pub fn get_min(&self) -> Result<PropertyValue> {
        match &self.prop {
            PropertyLims::Bool(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::Int(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::Float(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::Unsigned(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::PixelFmt(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::Duration(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::EnumStr(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::EnumInt(prop) => Ok(prop.get_min()?.into()),
            PropertyLims::EnumUnsigned(prop) => Ok(prop.get_min()?.into()),
        }
    }

    /// Get the maximum value of the property
    pub fn get_max(&self) -> Result<PropertyValue> {
        match &self.prop {
            PropertyLims::Bool(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::Int(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::Float(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::Unsigned(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::PixelFmt(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::Duration(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::EnumStr(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::EnumInt(prop) => Ok(prop.get_max()?.into()),
            PropertyLims::EnumUnsigned(prop) => Ok(prop.get_max()?.into()),
        }
    }

    /// Get the step value of the property
    pub fn get_step(&self) -> Result<PropertyValue> {
        match &self.prop {
            PropertyLims::Bool(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::Int(prop) => Ok(prop.get_step()?.into()),
            PropertyLims::Float(prop) => Ok(prop.get_step()?.into()),
            PropertyLims::Unsigned(prop) => Ok(prop.get_step()?.into()),
            PropertyLims::PixelFmt(_) => Err(GenCamError::PropertyIsEnum),
            PropertyLims::Duration(prop) => Ok(prop.get_step()?.into()),
            PropertyLims::EnumStr(_) => Err(GenCamError::PropertyNotNumber),
            PropertyLims::EnumInt(_) => Err(GenCamError::PropertyIsEnum),
            PropertyLims::EnumUnsigned(_) => Err(GenCamError::PropertyIsEnum),
        }
    }

    /// Get the default value of the property
    pub fn get_default(&self) -> Result<PropertyValue> {
        match &self.prop {
            PropertyLims::Bool(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::Int(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::Float(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::Unsigned(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::PixelFmt(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::Duration(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::EnumStr(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::EnumInt(prop) => Ok(prop.get_default()?.into()),
            PropertyLims::EnumUnsigned(prop) => Ok(prop.get_default()?.into()),
        }
    }

    /// Get the variants of the property
    pub fn get_variants(&self) -> Result<Vec<PropertyValue>> {
        match &self.prop {
            PropertyLims::Bool(_) => Err(GenCamError::PropertyNotEnum),
            PropertyLims::Int(_) => Err(GenCamError::PropertyNotEnum),
            PropertyLims::Float(_) => Err(GenCamError::PropertyNotEnum),
            PropertyLims::Unsigned(_) => Err(GenCamError::PropertyNotEnum),
            PropertyLims::Duration(_) => Err(GenCamError::PropertyNotEnum),
            PropertyLims::PixelFmt(prop) => {
                Ok(prop.get_variants()?.into_iter().map(|x| x.into()).collect())
            }
            PropertyLims::EnumStr(prop) => {
                Ok(prop.get_variants()?.into_iter().map(|x| x.into()).collect())
            }
            PropertyLims::EnumInt(prop) => {
                Ok(prop.get_variants()?.into_iter().map(|x| x.into()).collect())
            }
            PropertyLims::EnumUnsigned(prop) => {
                Ok(prop.get_variants()?.into_iter().map(|x| x.into()).collect())
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum PropertyLims {
    Bool(PropertyConcrete<bool>),
    Int(PropertyConcrete<i64>),
    Float(PropertyConcrete<f64>),
    Unsigned(PropertyConcrete<u64>),
    Duration(PropertyConcrete<Duration>),
    PixelFmt(PropertyEnum<GenCamPixelBpp>),
    EnumStr(PropertyEnum<String>),
    EnumInt(PropertyEnum<i64>),
    EnumUnsigned(PropertyEnum<u64>),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
/// A property value
pub enum PropertyValue {
    /// A boolean value
    Bool(bool),
    /// An integer value
    Int(i64),
    /// A floating point value
    Float(f64),
    /// An unsigned integer value
    Unsigned(u64),
    /// A pixel format value
    PixelFmt(GenCamPixelBpp),
    /// A duration value
    Duration(Duration),
    /// An enum string value
    EnumStr(String),
}

impl From<i64> for PropertyValue {
    fn from(val: i64) -> Self {
        PropertyValue::Int(val)
    }
}

impl From<u64> for PropertyValue {
    fn from(val: u64) -> Self {
        PropertyValue::Unsigned(val)
    }
}

impl From<f64> for PropertyValue {
    fn from(val: f64) -> Self {
        PropertyValue::Float(val)
    }
}

impl From<Duration> for PropertyValue {
    fn from(val: Duration) -> Self {
        PropertyValue::Duration(val)
    }
}

impl From<String> for PropertyValue {
    fn from(val: String) -> Self {
        PropertyValue::EnumStr(val)
    }
}

impl From<&str> for PropertyValue {
    fn from(val: &str) -> Self {
        PropertyValue::EnumStr(val.to_owned())
    }
}

impl From<bool> for PropertyValue {
    fn from(val: bool) -> Self {
        PropertyValue::Bool(val)
    }
}

impl From<GenCamPixelBpp> for PropertyValue {
    fn from(val: GenCamPixelBpp) -> Self {
        PropertyValue::PixelFmt(val)
    }
}

impl From<&PropertyValue> for PropertyType {
    fn from(prop: &PropertyValue) -> Self {
        use PropertyValue::*;
        match prop {
            Bool(_) => PropertyType::Bool,
            Int(_) => PropertyType::Int,
            Float(_) => PropertyType::Float,
            Unsigned(_) => PropertyType::Unsigned,
            PixelFmt(_) => PropertyType::PixelFmt,
            Duration(_) => PropertyType::Duration,
            EnumStr(_) => PropertyType::EnumStr,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
/// The type of a property
pub enum PropertyType {
    /// A boolean property
    Bool,
    /// An integer property ([`i64`])
    Int,
    /// A floating point property ([`f64`])
    Float,
    /// An unsigned integer property ([`u64`])
    Unsigned,
    /// A pixel format property ([`GenCamPixelBpp`])
    PixelFmt,
    /// A duration property ([`Duration`])
    Duration,
    /// An enum string property ([`String`])
    EnumStr,
    /// An enum integer property ([`i64`])
    EnumInt,
    /// An enum unsigned integer property ([`u64`])
    EnumUnsigned,
}

impl From<&PropertyLims> for PropertyType {
    fn from(prop: &PropertyLims) -> Self {
        use PropertyLims::*;
        match prop {
            Bool(_) => PropertyType::Bool,
            Int(_) => PropertyType::Int,
            Float(_) => PropertyType::Float,
            Unsigned(_) => PropertyType::Unsigned,
            Duration(_) => PropertyType::Duration,
            PixelFmt(_) => PropertyType::PixelFmt,
            EnumStr(_) => PropertyType::EnumStr,
            EnumInt(_) => PropertyType::EnumInt,
            EnumUnsigned(_) => PropertyType::EnumUnsigned,
        }
    }
}

pub trait PropertyFunctions<T: Sized> {
    fn get_min(&self) -> Result<T>;
    fn get_max(&self) -> Result<T>;
    fn get_step(&self) -> Result<T>;
    fn get_default(&self) -> Result<T>;
    fn get_variants(&self) -> Result<Vec<T>>;
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct PropertyConcrete<T: Sized> {
    min: T,
    max: T,
    step: T,
    rdonly: bool,
    default: T,
}

impl<T: Sized> PropertyConcrete<T> {
    pub fn new(min: T, max: T, step: T, rdonly: bool, default: T) -> Self {
        PropertyConcrete {
            min,
            max,
            step,
            rdonly,
            default,
        }
    }
}

macro_rules! prop_num_for_prop_conc {
    ($t:ty, $b: path) => {
        impl PropertyFunctions<$t> for PropertyConcrete<$t> {
            fn get_min(&self) -> Result<$t> {
                Ok(self.min)
            }
            fn get_max(&self) -> Result<$t> {
                Ok(self.max)
            }
            fn get_step(&self) -> Result<$t> {
                Ok(self.step)
            }
            fn get_default(&self) -> Result<$t> {
                Ok(self.default)
            }
            fn get_variants(&self) -> Result<Vec<$t>> {
                Err(GenCamError::PropertyNotEnum)
            }
        }
    };
}

prop_num_for_prop_conc!(i64, PropertyType::Int);
prop_num_for_prop_conc!(f64, PropertyType::Float);
prop_num_for_prop_conc!(u64, PropertyType::Unsigned);
prop_num_for_prop_conc!(Duration, PropertyType::Duration);

impl PropertyFunctions<bool> for PropertyConcrete<bool> {
    fn get_min(&self) -> Result<bool> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_max(&self) -> Result<bool> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_step(&self) -> Result<bool> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_default(&self) -> Result<bool> {
        Ok(self.default)
    }
    fn get_variants(&self) -> Result<Vec<bool>> {
        Err(GenCamError::PropertyNotEnum)
    }
}

impl PropertyFunctions<String> for PropertyConcrete<String> {
    fn get_min(&self) -> Result<String> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_max(&self) -> Result<String> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_step(&self) -> Result<String> {
        Err(GenCamError::PropertyNotNumber)
    }
    fn get_default(&self) -> Result<String> {
        Ok(self.default.clone())
    }
    fn get_variants(&self) -> Result<Vec<String>> {
        Err(GenCamError::PropertyNotEnum)
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PropertyEnum<T: Sized + PartialEq> {
    value: usize, // index of the value in the variants
    variants: Vec<T>,
    rdonly: bool,
    default: usize,
}

impl<T: Sized + PartialEq> PropertyEnum<T> {
    pub fn new(value: T, variants: Vec<T>, rdonly: bool, default: T) -> Self {
        let default = variants.iter().position(|x| x == &default).unwrap();
        let value = variants.iter().position(|x| x == &value).unwrap();
        PropertyEnum {
            value,
            variants,
            rdonly,
            default,
        }
    }
}

macro_rules! impl_propfn_for_propenum {
    ($t:ty) => {
        impl PropertyFunctions<$t> for PropertyEnum<$t> {
            fn get_min(&self) -> Result<$t> {
                Err(GenCamError::PropertyNotNumber)
            }
            fn get_max(&self) -> Result<$t> {
                Err(GenCamError::PropertyNotNumber)
            }
            fn get_step(&self) -> Result<$t> {
                Err(GenCamError::PropertyNotNumber)
            }
            fn get_default(&self) -> Result<$t> {
                Ok(self.variants[self.default].clone())
            }
            fn get_variants(&self) -> Result<Vec<$t>> {
                Ok(self.variants.clone())
            }
        }
    };
}

impl_propfn_for_propenum!(String);
impl_propfn_for_propenum!(i64);
impl_propfn_for_propenum!(u64);
impl_propfn_for_propenum!(GenCamPixelBpp);

// trait EnumType {
//     fn get_enum() -> PropertyType;
// }

// impl EnumType for String {
//     fn get_enum() -> PropertyType {
//         PropertyType::EnumStr
//     }
// }

// impl EnumType for i64 {
//     fn get_enum() -> PropertyType {
//         PropertyType::EnumInt
//     }
// }

// impl EnumType for u64 {
//     fn get_enum() -> PropertyType {
//         PropertyType::EnumUnsigned
//     }
// }