use conjure_object::serde::{ser, de};
use conjure_object::serde::ser::SerializeMap as SerializeMap_;
use conjure_object::private::{UnionField_, UnionTypeField_};
use std::fmt;
#[derive(Debug, Clone, conjure_object::private::DeriveWith)]
#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NumericSeriesNode {
    Raw(super::super::Reference),
    Arithmetic(super::ArithmeticSeriesNode),
    BitOperation(super::BitOperationSeriesNode),
    CumulativeSum(super::CumulativeSumSeriesNode),
    Derivative(super::DerivativeSeriesNode),
    RollingOperation(super::RollingOperationSeriesNode),
    UnaryArithmetic(super::UnaryArithmeticSeriesNode),
    TimeDifference(super::TimeDifferenceSeriesNode),
    TimeRangeFilter(super::NumericTimeRangeFilterSeriesNode),
    TimeShift(super::NumericTimeShiftSeriesNode),
    Union(super::NumericUnionSeriesNode),
    ValueDifference(super::ValueDifferenceSeriesNode),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for NumericSeriesNode {
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        let mut map = s.serialize_map(Some(2))?;
        match self {
            NumericSeriesNode::Raw(value) => {
                map.serialize_entry(&"type", &"raw")?;
                map.serialize_entry(&"raw", value)?;
            }
            NumericSeriesNode::Arithmetic(value) => {
                map.serialize_entry(&"type", &"arithmetic")?;
                map.serialize_entry(&"arithmetic", value)?;
            }
            NumericSeriesNode::BitOperation(value) => {
                map.serialize_entry(&"type", &"bitOperation")?;
                map.serialize_entry(&"bitOperation", value)?;
            }
            NumericSeriesNode::CumulativeSum(value) => {
                map.serialize_entry(&"type", &"cumulativeSum")?;
                map.serialize_entry(&"cumulativeSum", value)?;
            }
            NumericSeriesNode::Derivative(value) => {
                map.serialize_entry(&"type", &"derivative")?;
                map.serialize_entry(&"derivative", value)?;
            }
            NumericSeriesNode::RollingOperation(value) => {
                map.serialize_entry(&"type", &"rollingOperation")?;
                map.serialize_entry(&"rollingOperation", value)?;
            }
            NumericSeriesNode::UnaryArithmetic(value) => {
                map.serialize_entry(&"type", &"unaryArithmetic")?;
                map.serialize_entry(&"unaryArithmetic", value)?;
            }
            NumericSeriesNode::TimeDifference(value) => {
                map.serialize_entry(&"type", &"timeDifference")?;
                map.serialize_entry(&"timeDifference", value)?;
            }
            NumericSeriesNode::TimeRangeFilter(value) => {
                map.serialize_entry(&"type", &"timeRangeFilter")?;
                map.serialize_entry(&"timeRangeFilter", value)?;
            }
            NumericSeriesNode::TimeShift(value) => {
                map.serialize_entry(&"type", &"timeShift")?;
                map.serialize_entry(&"timeShift", value)?;
            }
            NumericSeriesNode::Union(value) => {
                map.serialize_entry(&"type", &"union")?;
                map.serialize_entry(&"union", value)?;
            }
            NumericSeriesNode::ValueDifference(value) => {
                map.serialize_entry(&"type", &"valueDifference")?;
                map.serialize_entry(&"valueDifference", value)?;
            }
            NumericSeriesNode::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for NumericSeriesNode {
    fn deserialize<D>(d: D) -> Result<NumericSeriesNode, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = NumericSeriesNode;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union NumericSeriesNode")
    }
    fn visit_map<A>(self, mut map: A) -> Result<NumericSeriesNode, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let v = match map.next_key::<UnionField_<Variant_>>()? {
            Some(UnionField_::Type) => {
                let variant = map.next_value()?;
                let key = map.next_key()?;
                match (variant, key) {
                    (Variant_::Raw, Some(Variant_::Raw)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Raw(value)
                    }
                    (Variant_::Arithmetic, Some(Variant_::Arithmetic)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Arithmetic(value)
                    }
                    (Variant_::BitOperation, Some(Variant_::BitOperation)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::BitOperation(value)
                    }
                    (Variant_::CumulativeSum, Some(Variant_::CumulativeSum)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::CumulativeSum(value)
                    }
                    (Variant_::Derivative, Some(Variant_::Derivative)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Derivative(value)
                    }
                    (Variant_::RollingOperation, Some(Variant_::RollingOperation)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::RollingOperation(value)
                    }
                    (Variant_::UnaryArithmetic, Some(Variant_::UnaryArithmetic)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::UnaryArithmetic(value)
                    }
                    (Variant_::TimeDifference, Some(Variant_::TimeDifference)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeDifference(value)
                    }
                    (Variant_::TimeRangeFilter, Some(Variant_::TimeRangeFilter)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeRangeFilter(value)
                    }
                    (Variant_::TimeShift, Some(Variant_::TimeShift)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeShift(value)
                    }
                    (Variant_::Union, Some(Variant_::Union)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Union(value)
                    }
                    (Variant_::ValueDifference, Some(Variant_::ValueDifference)) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::ValueDifference(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            NumericSeriesNode::Unknown(Unknown { type_, value })
                        } else {
                            return Err(
                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
                            )
                        }
                    }
                    (variant, Some(key)) => {
                        return Err(
                            de::Error::invalid_value(
                                de::Unexpected::Str(key.as_str()),
                                &variant.as_str(),
                            ),
                        );
                    }
                    (variant, None) => {
                        return Err(de::Error::missing_field(variant.as_str()));
                    }
                }
            }
            Some(UnionField_::Value(variant)) => {
                let value = match &variant {
                    Variant_::Raw => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Raw(value)
                    }
                    Variant_::Arithmetic => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Arithmetic(value)
                    }
                    Variant_::BitOperation => {
                        let value = map.next_value()?;
                        NumericSeriesNode::BitOperation(value)
                    }
                    Variant_::CumulativeSum => {
                        let value = map.next_value()?;
                        NumericSeriesNode::CumulativeSum(value)
                    }
                    Variant_::Derivative => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Derivative(value)
                    }
                    Variant_::RollingOperation => {
                        let value = map.next_value()?;
                        NumericSeriesNode::RollingOperation(value)
                    }
                    Variant_::UnaryArithmetic => {
                        let value = map.next_value()?;
                        NumericSeriesNode::UnaryArithmetic(value)
                    }
                    Variant_::TimeDifference => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeDifference(value)
                    }
                    Variant_::TimeRangeFilter => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeRangeFilter(value)
                    }
                    Variant_::TimeShift => {
                        let value = map.next_value()?;
                        NumericSeriesNode::TimeShift(value)
                    }
                    Variant_::Union => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Union(value)
                    }
                    Variant_::ValueDifference => {
                        let value = map.next_value()?;
                        NumericSeriesNode::ValueDifference(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        NumericSeriesNode::Unknown(Unknown {
                            type_: type_.clone(),
                            value,
                        })
                    }
                };
                if map.next_key::<UnionTypeField_>()?.is_none() {
                    return Err(de::Error::missing_field("type"));
                }
                let type_variant = map.next_value::<Variant_>()?;
                if variant != type_variant {
                    return Err(
                        de::Error::invalid_value(
                            de::Unexpected::Str(type_variant.as_str()),
                            &variant.as_str(),
                        ),
                    );
                }
                value
            }
            None => return Err(de::Error::missing_field("type")),
        };
        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
            return Err(de::Error::invalid_length(3, &"type and value fields"));
        }
        Ok(v)
    }
}
#[derive(PartialEq)]
enum Variant_ {
    Raw,
    Arithmetic,
    BitOperation,
    CumulativeSum,
    Derivative,
    RollingOperation,
    UnaryArithmetic,
    TimeDifference,
    TimeRangeFilter,
    TimeShift,
    Union,
    ValueDifference,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Raw => "raw",
            Variant_::Arithmetic => "arithmetic",
            Variant_::BitOperation => "bitOperation",
            Variant_::CumulativeSum => "cumulativeSum",
            Variant_::Derivative => "derivative",
            Variant_::RollingOperation => "rollingOperation",
            Variant_::UnaryArithmetic => "unaryArithmetic",
            Variant_::TimeDifference => "timeDifference",
            Variant_::TimeRangeFilter => "timeRangeFilter",
            Variant_::TimeShift => "timeShift",
            Variant_::Union => "union",
            Variant_::ValueDifference => "valueDifference",
            Variant_::Unknown(_) => "unknown variant",
        }
    }
}
impl<'de> de::Deserialize<'de> for Variant_ {
    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_str(VariantVisitor_)
    }
}
struct VariantVisitor_;
impl<'de> de::Visitor<'de> for VariantVisitor_ {
    type Value = Variant_;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("string")
    }
    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
    where
        E: de::Error,
    {
        let v = match value {
            "raw" => Variant_::Raw,
            "arithmetic" => Variant_::Arithmetic,
            "bitOperation" => Variant_::BitOperation,
            "cumulativeSum" => Variant_::CumulativeSum,
            "derivative" => Variant_::Derivative,
            "rollingOperation" => Variant_::RollingOperation,
            "unaryArithmetic" => Variant_::UnaryArithmetic,
            "timeDifference" => Variant_::TimeDifference,
            "timeRangeFilter" => Variant_::TimeRangeFilter,
            "timeShift" => Variant_::TimeShift,
            "union" => Variant_::Union,
            "valueDifference" => Variant_::ValueDifference,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the NumericSeriesNode union.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Unknown {
    type_: Box<str>,
    value: conjure_object::Any,
}
impl Unknown {
    /// Returns the unknown variant's type name.
    #[inline]
    pub fn type_(&self) -> &str {
        &self.type_
    }
}
