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
use super::{rewrite::Rewrite, timer::Timer};
use crate::service::triggers::{
    lidarr::{Lidarr, LidarrRequest},
    manual::Manual,
    notify::Notify,
    radarr::{Radarr, RadarrRequest},
    readarr::ReadarrRequest,
    sonarr::{Sonarr, SonarrRequest},
};
use serde::Deserialize;

pub trait TriggerRequest {
    fn from_json(json: serde_json::Value) -> anyhow::Result<Self>
    where
        Self: Sized;

    // where the bool represents whether to check found status
    fn paths(&self) -> Vec<(String, bool)>;
}

#[derive(Deserialize, Clone)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Trigger {
    Manual(Manual),
    Radarr(Radarr),
    Sonarr(Sonarr),
    Lidarr(Lidarr),
    Readarr(Sonarr),
    Notify(Notify),
}

impl Trigger {
    pub const fn get_rewrite(&self) -> Option<&Rewrite> {
        match &self {
            Self::Sonarr(trigger) => trigger.rewrite.as_ref(),
            Self::Radarr(trigger) => trigger.rewrite.as_ref(),
            Self::Lidarr(trigger) => trigger.rewrite.as_ref(),
            Self::Readarr(trigger) => trigger.rewrite.as_ref(),
            Self::Manual(_) | Self::Notify(_) => None,
        }
    }

    pub const fn get_timer(&self) -> &Timer {
        match &self {
            Self::Sonarr(trigger) => &trigger.timer,
            Self::Radarr(trigger) => &trigger.timer,
            Self::Lidarr(trigger) => &trigger.timer,
            Self::Readarr(trigger) => &trigger.timer,
            Self::Manual(trigger) => &trigger.timer,
            Self::Notify(trigger) => &trigger.timer,
        }
    }

    pub fn paths(&self, body: serde_json::Value) -> anyhow::Result<Vec<(String, bool)>> {
        match &self {
            Self::Sonarr(_) => Ok(SonarrRequest::from_json(body)?.paths()),
            Self::Radarr(_) => Ok(RadarrRequest::from_json(body)?.paths()),
            Self::Lidarr(_) => Ok(LidarrRequest::from_json(body)?.paths()),
            Self::Readarr(_) => Ok(ReadarrRequest::from_json(body)?.paths()),
            Self::Manual(_) | Self::Notify(_) => {
                Err(anyhow::anyhow!("Manual trigger does not have paths"))
            }
        }
    }

    pub const fn excludes(&self) -> &Vec<String> {
        match &self {
            Self::Manual(trigger) => &trigger.excludes,
            Self::Radarr(trigger) => &trigger.excludes,
            Self::Sonarr(trigger) => &trigger.excludes,
            Self::Lidarr(trigger) => &trigger.excludes,
            Self::Readarr(trigger) => &trigger.excludes,
            Self::Notify(trigger) => &trigger.excludes,
        }
    }
}