use chrono::{DateTime, FixedOffset, Timelike};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct Container<D> {
    pub data: D,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Status {
    pub train: TransportResource,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TransportResource {
    pub category: String,
    pub line_name: String,
    pub distance: u32,
    pub duration: u32,
    pub operator: Option<OperatorResource>,
    pub origin: StopOverResource,
    pub destination: StopOverResource,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StopOverResource {
    pub name: String,
    pub ril_identifier: Option<String>,
    pub arrival_planned: Option<DateTime<FixedOffset>>,
    pub arrival_real: Option<DateTime<FixedOffset>>,
    pub departure_planned: Option<DateTime<FixedOffset>>,
    pub departure_real: Option<DateTime<FixedOffset>>,
    pub platform: Option<String>,
    pub departure_platform_planned: Option<String>,
    pub departure_platform_real: Option<String>,
    pub arrival_platform_planned: Option<String>,
    pub arrival_platform_real: Option<String>,
}

// ????
pub struct JsonableData {
    pub time_err: bool,
    pub time_planned: Option<i64>,
    pub time_real: Option<i64>,
    pub station: String,
    pub ril100: Option<String>,
    pub platform_data_available: bool,
    pub platform_planned: Option<String>,
    pub platform_real: Option<String>,
}

// What the meaning of the stop in the journey is
pub enum StopJourneyPart {
    Origin,
    Destination,
}
impl StopOverResource {
    pub fn get_time_data(&self, journey_part: StopJourneyPart) -> JsonableData {
        let (time_planned, time_real, platform_planned, platform_real) = match journey_part {
            StopJourneyPart::Origin => (
                self.departure_planned,
                self.departure_real,
                self.departure_platform_planned.clone(),
                self.departure_platform_real.clone(),
            ),
            StopJourneyPart::Destination => (
                self.arrival_planned,
                self.arrival_real,
                self.arrival_platform_planned.clone(),
                self.arrival_platform_real.clone(),
            ),
        };

        let time_err = time_planned == None;

        JsonableData {
            time_err,
            time_planned: time_planned.map(|ts| ts.timestamp()),
            time_real: time_real.map(|ts| ts.timestamp()),
            station: self.name.clone(),
            ril100: self.ril_identifier.clone(),
            platform_data_available: platform_planned.is_some() || platform_real.is_some(),
            platform_planned,
            platform_real,
        }
    }
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OperatorResource {
    pub name: String,
}
