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 BooleanSeries {
    GreaterThan(super::GreaterThanSeries),
    LessThan(super::LessThanSeries),
    EqualTo(super::EqualToSeries),
    NotEqualTo(super::NotEqualToSeries),
    GreaterThanOrEqualTo(super::GreaterThanOrEqualToSeries),
    LessThanOrEqualTo(super::LessThanOrEqualToSeries),
    Not(super::NotSeries),
    And(super::AndSeries),
    Or(super::OrSeries),
    OnChange(super::OnChangeSeries),
    ToStartOfInterval(super::BooleanToStartOfIntervalSeries),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for BooleanSeries {
    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 {
            BooleanSeries::GreaterThan(value) => {
                map.serialize_entry(&"type", &"greaterThan")?;
                map.serialize_entry(&"greaterThan", value)?;
            }
            BooleanSeries::LessThan(value) => {
                map.serialize_entry(&"type", &"lessThan")?;
                map.serialize_entry(&"lessThan", value)?;
            }
            BooleanSeries::EqualTo(value) => {
                map.serialize_entry(&"type", &"equalTo")?;
                map.serialize_entry(&"equalTo", value)?;
            }
            BooleanSeries::NotEqualTo(value) => {
                map.serialize_entry(&"type", &"notEqualTo")?;
                map.serialize_entry(&"notEqualTo", value)?;
            }
            BooleanSeries::GreaterThanOrEqualTo(value) => {
                map.serialize_entry(&"type", &"greaterThanOrEqualTo")?;
                map.serialize_entry(&"greaterThanOrEqualTo", value)?;
            }
            BooleanSeries::LessThanOrEqualTo(value) => {
                map.serialize_entry(&"type", &"lessThanOrEqualTo")?;
                map.serialize_entry(&"lessThanOrEqualTo", value)?;
            }
            BooleanSeries::Not(value) => {
                map.serialize_entry(&"type", &"not")?;
                map.serialize_entry(&"not", value)?;
            }
            BooleanSeries::And(value) => {
                map.serialize_entry(&"type", &"and")?;
                map.serialize_entry(&"and", value)?;
            }
            BooleanSeries::Or(value) => {
                map.serialize_entry(&"type", &"or")?;
                map.serialize_entry(&"or", value)?;
            }
            BooleanSeries::OnChange(value) => {
                map.serialize_entry(&"type", &"onChange")?;
                map.serialize_entry(&"onChange", value)?;
            }
            BooleanSeries::ToStartOfInterval(value) => {
                map.serialize_entry(&"type", &"toStartOfInterval")?;
                map.serialize_entry(&"toStartOfInterval", value)?;
            }
            BooleanSeries::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for BooleanSeries {
    fn deserialize<D>(d: D) -> Result<BooleanSeries, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = BooleanSeries;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union BooleanSeries")
    }
    fn visit_map<A>(self, mut map: A) -> Result<BooleanSeries, 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_::GreaterThan, Some(Variant_::GreaterThan)) => {
                        let value = map.next_value()?;
                        BooleanSeries::GreaterThan(value)
                    }
                    (Variant_::LessThan, Some(Variant_::LessThan)) => {
                        let value = map.next_value()?;
                        BooleanSeries::LessThan(value)
                    }
                    (Variant_::EqualTo, Some(Variant_::EqualTo)) => {
                        let value = map.next_value()?;
                        BooleanSeries::EqualTo(value)
                    }
                    (Variant_::NotEqualTo, Some(Variant_::NotEqualTo)) => {
                        let value = map.next_value()?;
                        BooleanSeries::NotEqualTo(value)
                    }
                    (
                        Variant_::GreaterThanOrEqualTo,
                        Some(Variant_::GreaterThanOrEqualTo),
                    ) => {
                        let value = map.next_value()?;
                        BooleanSeries::GreaterThanOrEqualTo(value)
                    }
                    (Variant_::LessThanOrEqualTo, Some(Variant_::LessThanOrEqualTo)) => {
                        let value = map.next_value()?;
                        BooleanSeries::LessThanOrEqualTo(value)
                    }
                    (Variant_::Not, Some(Variant_::Not)) => {
                        let value = map.next_value()?;
                        BooleanSeries::Not(value)
                    }
                    (Variant_::And, Some(Variant_::And)) => {
                        let value = map.next_value()?;
                        BooleanSeries::And(value)
                    }
                    (Variant_::Or, Some(Variant_::Or)) => {
                        let value = map.next_value()?;
                        BooleanSeries::Or(value)
                    }
                    (Variant_::OnChange, Some(Variant_::OnChange)) => {
                        let value = map.next_value()?;
                        BooleanSeries::OnChange(value)
                    }
                    (Variant_::ToStartOfInterval, Some(Variant_::ToStartOfInterval)) => {
                        let value = map.next_value()?;
                        BooleanSeries::ToStartOfInterval(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            BooleanSeries::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_::GreaterThan => {
                        let value = map.next_value()?;
                        BooleanSeries::GreaterThan(value)
                    }
                    Variant_::LessThan => {
                        let value = map.next_value()?;
                        BooleanSeries::LessThan(value)
                    }
                    Variant_::EqualTo => {
                        let value = map.next_value()?;
                        BooleanSeries::EqualTo(value)
                    }
                    Variant_::NotEqualTo => {
                        let value = map.next_value()?;
                        BooleanSeries::NotEqualTo(value)
                    }
                    Variant_::GreaterThanOrEqualTo => {
                        let value = map.next_value()?;
                        BooleanSeries::GreaterThanOrEqualTo(value)
                    }
                    Variant_::LessThanOrEqualTo => {
                        let value = map.next_value()?;
                        BooleanSeries::LessThanOrEqualTo(value)
                    }
                    Variant_::Not => {
                        let value = map.next_value()?;
                        BooleanSeries::Not(value)
                    }
                    Variant_::And => {
                        let value = map.next_value()?;
                        BooleanSeries::And(value)
                    }
                    Variant_::Or => {
                        let value = map.next_value()?;
                        BooleanSeries::Or(value)
                    }
                    Variant_::OnChange => {
                        let value = map.next_value()?;
                        BooleanSeries::OnChange(value)
                    }
                    Variant_::ToStartOfInterval => {
                        let value = map.next_value()?;
                        BooleanSeries::ToStartOfInterval(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        BooleanSeries::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_ {
    GreaterThan,
    LessThan,
    EqualTo,
    NotEqualTo,
    GreaterThanOrEqualTo,
    LessThanOrEqualTo,
    Not,
    And,
    Or,
    OnChange,
    ToStartOfInterval,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::GreaterThan => "greaterThan",
            Variant_::LessThan => "lessThan",
            Variant_::EqualTo => "equalTo",
            Variant_::NotEqualTo => "notEqualTo",
            Variant_::GreaterThanOrEqualTo => "greaterThanOrEqualTo",
            Variant_::LessThanOrEqualTo => "lessThanOrEqualTo",
            Variant_::Not => "not",
            Variant_::And => "and",
            Variant_::Or => "or",
            Variant_::OnChange => "onChange",
            Variant_::ToStartOfInterval => "toStartOfInterval",
            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 {
            "greaterThan" => Variant_::GreaterThan,
            "lessThan" => Variant_::LessThan,
            "equalTo" => Variant_::EqualTo,
            "notEqualTo" => Variant_::NotEqualTo,
            "greaterThanOrEqualTo" => Variant_::GreaterThanOrEqualTo,
            "lessThanOrEqualTo" => Variant_::LessThanOrEqualTo,
            "not" => Variant_::Not,
            "and" => Variant_::And,
            "or" => Variant_::Or,
            "onChange" => Variant_::OnChange,
            "toStartOfInterval" => Variant_::ToStartOfInterval,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the BooleanSeries 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_
    }
}
