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 VizDefinition {
    Cartesian(Box<super::CartesianChartDefinition>),
    Checklist(Box<super::ChecklistChartDefinition>),
    Frequency(Box<super::FrequencyChartDefinition>),
    Geo(Box<super::GeoVizDefinition>),
    Geo3d(Box<super::Geo3dDefinition>),
    Histogram(Box<super::HistogramChartDefinition>),
    Log(Box<super::LogPanelDefinition>),
    Markdown(Box<super::MarkdownPanelDefinition>),
    Plotly(Box<super::PlotlyPanelDefinition>),
    Procedure(Box<super::ProcedureVizDefinition>),
    TimeSeries(Box<super::TimeSeriesChartDefinition>),
    TimeSeriesTable(Box<super::TimeSeriesTablePanelDefinition>),
    ValueTable(Box<super::ValueTableDefinition>),
    Video(Box<super::VideoVizDefinition>),
    /// An unknown variant.
    Unknown(Unknown),
}
impl ser::Serialize for VizDefinition {
    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 {
            VizDefinition::Cartesian(value) => {
                map.serialize_entry(&"type", &"cartesian")?;
                map.serialize_entry(&"cartesian", value)?;
            }
            VizDefinition::Checklist(value) => {
                map.serialize_entry(&"type", &"checklist")?;
                map.serialize_entry(&"checklist", value)?;
            }
            VizDefinition::Frequency(value) => {
                map.serialize_entry(&"type", &"frequency")?;
                map.serialize_entry(&"frequency", value)?;
            }
            VizDefinition::Geo(value) => {
                map.serialize_entry(&"type", &"geo")?;
                map.serialize_entry(&"geo", value)?;
            }
            VizDefinition::Geo3d(value) => {
                map.serialize_entry(&"type", &"geo3d")?;
                map.serialize_entry(&"geo3d", value)?;
            }
            VizDefinition::Histogram(value) => {
                map.serialize_entry(&"type", &"histogram")?;
                map.serialize_entry(&"histogram", value)?;
            }
            VizDefinition::Log(value) => {
                map.serialize_entry(&"type", &"log")?;
                map.serialize_entry(&"log", value)?;
            }
            VizDefinition::Markdown(value) => {
                map.serialize_entry(&"type", &"markdown")?;
                map.serialize_entry(&"markdown", value)?;
            }
            VizDefinition::Plotly(value) => {
                map.serialize_entry(&"type", &"plotly")?;
                map.serialize_entry(&"plotly", value)?;
            }
            VizDefinition::Procedure(value) => {
                map.serialize_entry(&"type", &"procedure")?;
                map.serialize_entry(&"procedure", value)?;
            }
            VizDefinition::TimeSeries(value) => {
                map.serialize_entry(&"type", &"timeSeries")?;
                map.serialize_entry(&"timeSeries", value)?;
            }
            VizDefinition::TimeSeriesTable(value) => {
                map.serialize_entry(&"type", &"timeSeriesTable")?;
                map.serialize_entry(&"timeSeriesTable", value)?;
            }
            VizDefinition::ValueTable(value) => {
                map.serialize_entry(&"type", &"valueTable")?;
                map.serialize_entry(&"valueTable", value)?;
            }
            VizDefinition::Video(value) => {
                map.serialize_entry(&"type", &"video")?;
                map.serialize_entry(&"video", value)?;
            }
            VizDefinition::Unknown(value) => {
                map.serialize_entry(&"type", &value.type_)?;
                map.serialize_entry(&value.type_, &value.value)?;
            }
        }
        map.end()
    }
}
impl<'de> de::Deserialize<'de> for VizDefinition {
    fn deserialize<D>(d: D) -> Result<VizDefinition, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_map(Visitor_)
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = VizDefinition;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("union VizDefinition")
    }
    fn visit_map<A>(self, mut map: A) -> Result<VizDefinition, 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_::Cartesian, Some(Variant_::Cartesian)) => {
                        let value = map.next_value()?;
                        VizDefinition::Cartesian(value)
                    }
                    (Variant_::Checklist, Some(Variant_::Checklist)) => {
                        let value = map.next_value()?;
                        VizDefinition::Checklist(value)
                    }
                    (Variant_::Frequency, Some(Variant_::Frequency)) => {
                        let value = map.next_value()?;
                        VizDefinition::Frequency(value)
                    }
                    (Variant_::Geo, Some(Variant_::Geo)) => {
                        let value = map.next_value()?;
                        VizDefinition::Geo(value)
                    }
                    (Variant_::Geo3d, Some(Variant_::Geo3d)) => {
                        let value = map.next_value()?;
                        VizDefinition::Geo3d(value)
                    }
                    (Variant_::Histogram, Some(Variant_::Histogram)) => {
                        let value = map.next_value()?;
                        VizDefinition::Histogram(value)
                    }
                    (Variant_::Log, Some(Variant_::Log)) => {
                        let value = map.next_value()?;
                        VizDefinition::Log(value)
                    }
                    (Variant_::Markdown, Some(Variant_::Markdown)) => {
                        let value = map.next_value()?;
                        VizDefinition::Markdown(value)
                    }
                    (Variant_::Plotly, Some(Variant_::Plotly)) => {
                        let value = map.next_value()?;
                        VizDefinition::Plotly(value)
                    }
                    (Variant_::Procedure, Some(Variant_::Procedure)) => {
                        let value = map.next_value()?;
                        VizDefinition::Procedure(value)
                    }
                    (Variant_::TimeSeries, Some(Variant_::TimeSeries)) => {
                        let value = map.next_value()?;
                        VizDefinition::TimeSeries(value)
                    }
                    (Variant_::TimeSeriesTable, Some(Variant_::TimeSeriesTable)) => {
                        let value = map.next_value()?;
                        VizDefinition::TimeSeriesTable(value)
                    }
                    (Variant_::ValueTable, Some(Variant_::ValueTable)) => {
                        let value = map.next_value()?;
                        VizDefinition::ValueTable(value)
                    }
                    (Variant_::Video, Some(Variant_::Video)) => {
                        let value = map.next_value()?;
                        VizDefinition::Video(value)
                    }
                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
                        if type_ == b {
                            let value = map.next_value()?;
                            VizDefinition::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_::Cartesian => {
                        let value = map.next_value()?;
                        VizDefinition::Cartesian(value)
                    }
                    Variant_::Checklist => {
                        let value = map.next_value()?;
                        VizDefinition::Checklist(value)
                    }
                    Variant_::Frequency => {
                        let value = map.next_value()?;
                        VizDefinition::Frequency(value)
                    }
                    Variant_::Geo => {
                        let value = map.next_value()?;
                        VizDefinition::Geo(value)
                    }
                    Variant_::Geo3d => {
                        let value = map.next_value()?;
                        VizDefinition::Geo3d(value)
                    }
                    Variant_::Histogram => {
                        let value = map.next_value()?;
                        VizDefinition::Histogram(value)
                    }
                    Variant_::Log => {
                        let value = map.next_value()?;
                        VizDefinition::Log(value)
                    }
                    Variant_::Markdown => {
                        let value = map.next_value()?;
                        VizDefinition::Markdown(value)
                    }
                    Variant_::Plotly => {
                        let value = map.next_value()?;
                        VizDefinition::Plotly(value)
                    }
                    Variant_::Procedure => {
                        let value = map.next_value()?;
                        VizDefinition::Procedure(value)
                    }
                    Variant_::TimeSeries => {
                        let value = map.next_value()?;
                        VizDefinition::TimeSeries(value)
                    }
                    Variant_::TimeSeriesTable => {
                        let value = map.next_value()?;
                        VizDefinition::TimeSeriesTable(value)
                    }
                    Variant_::ValueTable => {
                        let value = map.next_value()?;
                        VizDefinition::ValueTable(value)
                    }
                    Variant_::Video => {
                        let value = map.next_value()?;
                        VizDefinition::Video(value)
                    }
                    Variant_::Unknown(type_) => {
                        let value = map.next_value()?;
                        VizDefinition::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_ {
    Cartesian,
    Checklist,
    Frequency,
    Geo,
    Geo3d,
    Histogram,
    Log,
    Markdown,
    Plotly,
    Procedure,
    TimeSeries,
    TimeSeriesTable,
    ValueTable,
    Video,
    Unknown(Box<str>),
}
impl Variant_ {
    fn as_str(&self) -> &'static str {
        match *self {
            Variant_::Cartesian => "cartesian",
            Variant_::Checklist => "checklist",
            Variant_::Frequency => "frequency",
            Variant_::Geo => "geo",
            Variant_::Geo3d => "geo3d",
            Variant_::Histogram => "histogram",
            Variant_::Log => "log",
            Variant_::Markdown => "markdown",
            Variant_::Plotly => "plotly",
            Variant_::Procedure => "procedure",
            Variant_::TimeSeries => "timeSeries",
            Variant_::TimeSeriesTable => "timeSeriesTable",
            Variant_::ValueTable => "valueTable",
            Variant_::Video => "video",
            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 {
            "cartesian" => Variant_::Cartesian,
            "checklist" => Variant_::Checklist,
            "frequency" => Variant_::Frequency,
            "geo" => Variant_::Geo,
            "geo3d" => Variant_::Geo3d,
            "histogram" => Variant_::Histogram,
            "log" => Variant_::Log,
            "markdown" => Variant_::Markdown,
            "plotly" => Variant_::Plotly,
            "procedure" => Variant_::Procedure,
            "timeSeries" => Variant_::TimeSeries,
            "timeSeriesTable" => Variant_::TimeSeriesTable,
            "valueTable" => Variant_::ValueTable,
            "video" => Variant_::Video,
            value => Variant_::Unknown(value.to_string().into_boxed_str()),
        };
        Ok(v)
    }
}
///An unknown variant of the VizDefinition 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_
    }
}
