use std::io::Read;
use std::str::FromStr;

use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};

use super::*;

impl ElementReader for DocDefaults {
    fn read<R: Read>(
        r: &mut EventReader<R>,
        _attrs: &[OwnedAttribute],
    ) -> Result<Self, ReaderError> {
        let mut doc_defaults = DocDefaults::new();
        loop {
            let e = r.next();
            match e {
                Ok(XmlEvent::StartElement {
                    attributes, name, ..
                }) => {
                    let e = XMLElement::from_str(&name.local_name).unwrap();
                    if let XMLElement::RunProperty = e {
                        let run_pr = RunProperty::read(r, &attributes)?;
                        doc_defaults = doc_defaults.run_property(run_pr);
                        continue;
                    }
                    if let XMLElement::ParagraphProperty = e {
                        let paragraph_pr = ParagraphProperty::read(r, &attributes)?;
                        doc_defaults = doc_defaults.paragraph_property(paragraph_pr);
                        continue;
                    }
                }
                Ok(XmlEvent::EndElement { name, .. }) => {
                    let e = XMLElement::from_str(&name.local_name).unwrap();
                    if let XMLElement::DocDefaults = e {
                        return Ok(doc_defaults);
                    }
                }
                Err(_) => return Err(ReaderError::XMLReadError),
                _ => {}
            }
        }
    }
}
