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
//! Event types for polling responses.

use crate::{std::fmt, Error};

mod cashbox_removed;
mod cashbox_replaced;
mod disable;
mod disabled;
mod dispense;
mod enable;
mod fraud_attempt;
mod method;
mod note_cleared_from_front;
mod note_cleared_into_cashbox;
mod note_credit;
mod read;
mod reject;
mod rejected;
mod rejecting;
mod reset;
mod stack;
mod stacked;
mod stacker_full;
mod stacking;
mod status;
mod unsafe_jam;

pub use cashbox_removed::*;
pub use cashbox_replaced::*;
pub use disable::*;
pub use disabled::*;
pub use dispense::*;
pub use enable::*;
pub use fraud_attempt::*;
pub use method::*;
pub use note_cleared_from_front::*;
pub use note_cleared_into_cashbox::*;
pub use note_credit::*;
pub use read::*;
pub use reject::*;
pub use rejected::*;
pub use rejecting::*;
pub use reset::*;
pub use stack::*;
pub use stacked::*;
pub use stacker_full::*;
pub use stacking::*;
pub use status::*;
pub use unsafe_jam::*;

/// JSON-RPC payloads for request parameters and response results.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum EventPayload {
    Error(Error),
    // Command event payloads
    DisableEvent(DisableEvent),
    DispenseEvent(DispenseEvent),
    EnableEvent(EnableEvent),
    RejectEvent(RejectEvent),
    StackEvent(StackEvent),
    StatusEvent(StatusEvent),
    // Response event payloads
    CashboxRemovedEvent(CashboxRemovedEvent),
    CashboxReplacedEvent(CashboxReplacedEvent),
    DisabledEvent(DisabledEvent),
    FraudAttemptEvent(FraudAttemptEvent),
    NoteClearedFromFrontEvent(NoteClearedFromFrontEvent),
    NoteClearedIntoCashboxEvent(NoteClearedIntoCashboxEvent),
    NoteCreditEvent(NoteCreditEvent),
    ReadEvent(ReadEvent),
    RejectedEvent(RejectedEvent),
    RejectingEvent(RejectingEvent),
    // Reset is a command & response event payload
    ResetEvent(ResetEvent),
    StackedEvent(StackedEvent),
    StackerFullEvent(StackerFullEvent),
    StackingEvent(StackingEvent),
    UnsafeJamEvent(UnsafeJamEvent),
}

impl EventPayload {
    pub fn method(&self) -> Method {
        match self {
            Self::Error(_) => Method::Fail,
            Self::DisableEvent(_) => DisableEvent::method(),
            Self::DispenseEvent(_) => DispenseEvent::method(),
            Self::EnableEvent(_) => EnableEvent::method(),
            Self::RejectEvent(_) => RejectEvent::method(),
            Self::StackEvent(_) => StackEvent::method(),
            Self::StatusEvent(_) => StatusEvent::method(),
            Self::CashboxRemovedEvent(_) => CashboxRemovedEvent::method(),
            Self::CashboxReplacedEvent(_) => CashboxReplacedEvent::method(),
            Self::DisabledEvent(_) => DisabledEvent::method(),
            Self::FraudAttemptEvent(_) => FraudAttemptEvent::method(),
            Self::NoteClearedFromFrontEvent(_) => NoteClearedFromFrontEvent::method(),
            Self::NoteClearedIntoCashboxEvent(_) => NoteClearedIntoCashboxEvent::method(),
            Self::NoteCreditEvent(_) => NoteCreditEvent::method(),
            Self::ReadEvent(_) => ReadEvent::method(),
            Self::RejectedEvent(_) => RejectedEvent::method(),
            Self::RejectingEvent(_) => RejectingEvent::method(),
            Self::ResetEvent(_) => ResetEvent::method(),
            Self::StackedEvent(_) => StackedEvent::method(),
            Self::StackerFullEvent(_) => StackerFullEvent::method(),
            Self::StackingEvent(_) => StackingEvent::method(),
            Self::UnsafeJamEvent(_) => UnsafeJamEvent::method(),
        }
    }

    #[cfg(feature = "jsonrpc")]
    pub fn to_json(&self) -> serde_json::Value {
        use serde_json::json;

        match self {
            Self::Error(evt) => json!(evt),
            Self::DisableEvent(evt) => json!(evt),
            Self::DispenseEvent(evt) => json!(evt),
            Self::EnableEvent(evt) => json!(evt),
            Self::RejectEvent(evt) => json!(evt),
            Self::StackEvent(evt) => json!(evt),
            Self::StatusEvent(evt) => json!(evt),
            Self::CashboxRemovedEvent(evt) => json!(evt),
            Self::CashboxReplacedEvent(evt) => json!(evt),
            Self::DisabledEvent(evt) => json!(evt),
            Self::FraudAttemptEvent(evt) => json!(evt),
            Self::NoteClearedFromFrontEvent(evt) => json!(evt),
            Self::NoteClearedIntoCashboxEvent(evt) => json!(evt),
            Self::NoteCreditEvent(evt) => json!(evt),
            Self::ReadEvent(evt) => json!(evt),
            Self::RejectedEvent(evt) => json!(evt),
            Self::RejectingEvent(evt) => json!(evt),
            Self::ResetEvent(evt) => json!(evt),
            Self::StackedEvent(evt) => json!(evt),
            Self::StackerFullEvent(evt) => json!(evt),
            Self::StackingEvent(evt) => json!(evt),
            Self::UnsafeJamEvent(evt) => json!(evt),
        }
    }
}

impl fmt::Display for EventPayload {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Error(err) => write!(f, r#"{{"error": "{err}"}}"#),
            Self::DisableEvent(evt) => write!(f, "{evt}"),
            Self::DispenseEvent(evt) => write!(f, "{evt}"),
            Self::EnableEvent(evt) => write!(f, "{evt}"),
            Self::RejectEvent(evt) => write!(f, "{evt}"),
            Self::StackEvent(evt) => write!(f, "{evt}"),
            Self::StatusEvent(evt) => write!(f, "{evt}"),
            Self::CashboxRemovedEvent(evt) => write!(f, "{evt}"),
            Self::CashboxReplacedEvent(evt) => write!(f, "{evt}"),
            Self::DisabledEvent(evt) => write!(f, "{evt}"),
            Self::FraudAttemptEvent(evt) => write!(f, "{evt}"),
            Self::NoteClearedFromFrontEvent(evt) => write!(f, "{evt}"),
            Self::NoteClearedIntoCashboxEvent(evt) => write!(f, "{evt}"),
            Self::NoteCreditEvent(evt) => write!(f, "{evt}"),
            Self::ReadEvent(evt) => write!(f, "{evt}"),
            Self::RejectedEvent(evt) => write!(f, "{evt}"),
            Self::RejectingEvent(evt) => write!(f, "{evt}"),
            Self::ResetEvent(evt) => write!(f, "{evt}"),
            Self::StackedEvent(evt) => write!(f, "{evt}"),
            Self::StackerFullEvent(evt) => write!(f, "{evt}"),
            Self::StackingEvent(evt) => write!(f, "{evt}"),
            Self::UnsafeJamEvent(evt) => write!(f, "{evt}"),
        }
    }
}

inner_enum!(EventPayload, Error);
inner_enum!(EventPayload, DisableEvent);
inner_enum!(EventPayload, DispenseEvent);
inner_enum!(EventPayload, EnableEvent);
inner_enum!(EventPayload, RejectEvent);
inner_enum!(EventPayload, StackEvent);
inner_enum!(EventPayload, StatusEvent);
inner_enum!(EventPayload, CashboxRemovedEvent);
inner_enum!(EventPayload, CashboxReplacedEvent);
inner_enum!(EventPayload, DisabledEvent);
inner_enum!(EventPayload, FraudAttemptEvent);
inner_enum!(EventPayload, NoteClearedFromFrontEvent);
inner_enum!(EventPayload, NoteClearedIntoCashboxEvent);
inner_enum!(EventPayload, NoteCreditEvent);
inner_enum!(EventPayload, ReadEvent);
inner_enum!(EventPayload, RejectedEvent);
inner_enum!(EventPayload, RejectingEvent);
inner_enum!(EventPayload, ResetEvent);
inner_enum!(EventPayload, StackedEvent);
inner_enum!(EventPayload, StackerFullEvent);
inner_enum!(EventPayload, StackingEvent);
inner_enum!(EventPayload, UnsafeJamEvent);

macro_rules! from_event_for_payload {
    ($event:ident) => {
        impl From<$event> for EventPayload {
            fn from(val: $event) -> Self {
                Self::$event(val)
            }
        }

        impl From<&$event> for EventPayload {
            fn from(val: &$event) -> Self {
                Self::$event(val.clone())
            }
        }

        impl TryFrom<EventPayload> for $event {
            type Error = $crate::Error;

            fn try_from(val: EventPayload) -> $crate::Result<Self> {
                match val {
                    EventPayload::$event(evt) => Ok(evt),
                    event => Err(Error::InvalidEvent((
                        event.method().into(),
                        $event::method().into(),
                    ))),
                }
            }
        }

        impl TryFrom<&EventPayload> for $event {
            type Error = $crate::Error;

            fn try_from(val: &EventPayload) -> $crate::Result<Self> {
                $event::try_from(val.clone())
            }
        }
    };
}

// Command events
from_event_for_payload!(DisableEvent);
from_event_for_payload!(EnableEvent);
from_event_for_payload!(RejectEvent);
from_event_for_payload!(StackEvent);
from_event_for_payload!(StatusEvent);
from_event_for_payload!(DispenseEvent);
// Response events
from_event_for_payload!(CashboxRemovedEvent);
from_event_for_payload!(CashboxReplacedEvent);
from_event_for_payload!(DisabledEvent);
from_event_for_payload!(FraudAttemptEvent);
from_event_for_payload!(NoteClearedFromFrontEvent);
from_event_for_payload!(NoteClearedIntoCashboxEvent);
from_event_for_payload!(NoteCreditEvent);
from_event_for_payload!(ReadEvent);
from_event_for_payload!(RejectedEvent);
from_event_for_payload!(RejectingEvent);
from_event_for_payload!(ResetEvent); // Reset is also a command
from_event_for_payload!(StackedEvent);
from_event_for_payload!(StackerFullEvent);
from_event_for_payload!(StackingEvent);
from_event_for_payload!(UnsafeJamEvent);

/// Represents a generic event from a polling response.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Event {
    method: Method,
    payload: EventPayload,
}

impl Event {
    /// Creates a new [Event] from the provided `method` and `data`.
    pub fn new(method: Method, payload: EventPayload) -> Self {
        Self { method, payload }
    }

    /// Gets the method.
    pub const fn method(&self) -> Method {
        self.method
    }

    /// Gets the method as a `&str`.
    pub const fn method_str(&self) -> &str {
        self.method.to_str()
    }

    /// Sets the method.
    pub fn set_method(&mut self, method: Method) {
        self.method = method;
    }

    /// Gets a reference to the [EventPayload].
    pub fn payload(&self) -> &EventPayload {
        &self.payload
    }

    /// Sets the [EventPayload].
    pub fn set_payload(&mut self, payload: EventPayload) {
        self.payload = payload
    }
}

impl From<Method> for Event {
    fn from(method: Method) -> Self {
        let payload = match method {
            Method::Fail => EventPayload::Error(Error::Generic(-1)),
            Method::Disable | Method::Stop | Method::Shutdown => {
                EventPayload::DisableEvent(DisableEvent::new())
            }
            Method::Dispense => EventPayload::DispenseEvent(DispenseEvent::default()),
            Method::Enable | Method::Accept => EventPayload::EnableEvent(EnableEvent::default()),
            Method::Reject => EventPayload::RejectEvent(RejectEvent::new()),
            Method::Stack => EventPayload::StackEvent(StackEvent::default()),
            Method::Status => EventPayload::StatusEvent(StatusEvent::default()),
            Method::CashboxRemoved => EventPayload::CashboxRemovedEvent(CashboxRemovedEvent::new()),
            Method::CashboxReplaced => {
                EventPayload::CashboxReplacedEvent(CashboxReplacedEvent::new())
            }
            Method::Disabled => EventPayload::DisabledEvent(DisabledEvent::new()),
            Method::FraudAttempt => EventPayload::FraudAttemptEvent(FraudAttemptEvent::default()),
            Method::NoteClearedFromFront => {
                EventPayload::NoteClearedFromFrontEvent(NoteClearedFromFrontEvent::default())
            }
            Method::NoteClearedIntoCashbox => {
                EventPayload::NoteClearedIntoCashboxEvent(NoteClearedIntoCashboxEvent::default())
            }
            Method::NoteCredit => EventPayload::NoteCreditEvent(NoteCreditEvent::default()),
            Method::Read => EventPayload::ReadEvent(ReadEvent::default()),
            Method::Rejected => EventPayload::RejectedEvent(RejectedEvent::new()),
            Method::Rejecting => EventPayload::RejectingEvent(RejectingEvent::new()),
            Method::Reset => EventPayload::ResetEvent(ResetEvent::new()),
            Method::Stacked => EventPayload::StackedEvent(StackedEvent::new()),
            Method::StackerFull => EventPayload::StackerFullEvent(StackerFullEvent::new()),
            Method::Stacking => EventPayload::StackingEvent(StackingEvent::new()),
            Method::UnsafeJam => EventPayload::UnsafeJamEvent(UnsafeJamEvent::new()),
            Method::Reserved(m) => EventPayload::Error(Error::Generic(-(m as i64))),
        };

        Self { method, payload }
    }
}

impl fmt::Display for Event {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let method = self.method();
        let payload = self.payload();

        write!(f, "method: {method}, payload: {payload}")
    }
}

macro_rules! from_event_for_event {
    ($event:ident) => {
        impl From<&$event> for Event {
            fn from(val: &$event) -> Self {
                Self::new($event::method(), val.into())
            }
        }

        impl From<$event> for Event {
            fn from(val: $event) -> Self {
                (&val).into()
            }
        }

        impl TryFrom<&Event> for $event {
            type Error = $crate::Error;

            fn try_from(val: &Event) -> $crate::Result<Self> {
                $event::try_from(val.payload())
            }
        }

        impl TryFrom<Event> for $event {
            type Error = $crate::Error;

            fn try_from(val: Event) -> $crate::Result<Self> {
                $event::try_from(val.payload())
            }
        }
    };
}

// Command events
from_event_for_event!(DisableEvent);
from_event_for_event!(EnableEvent);
from_event_for_event!(RejectEvent);
from_event_for_event!(StackEvent);
from_event_for_event!(StatusEvent);
from_event_for_event!(DispenseEvent);
// Response events
from_event_for_event!(CashboxRemovedEvent);
from_event_for_event!(CashboxReplacedEvent);
from_event_for_event!(DisabledEvent);
from_event_for_event!(FraudAttemptEvent);
from_event_for_event!(NoteClearedFromFrontEvent);
from_event_for_event!(NoteClearedIntoCashboxEvent);
from_event_for_event!(NoteCreditEvent);
from_event_for_event!(ReadEvent);
from_event_for_event!(RejectedEvent);
from_event_for_event!(RejectingEvent);
from_event_for_event!(ResetEvent); // Reset is also a command
from_event_for_event!(StackedEvent);
from_event_for_event!(StackerFullEvent);
from_event_for_event!(StackingEvent);
from_event_for_event!(UnsafeJamEvent);