package wml

import (
	"encoding/xml"

	"go.devnw.com/ooxml"
)

type EG_HdrFtrReferences struct {
	// Header Reference
	HeaderReference *CT_HdrFtrRef
	// Footer Reference
	FooterReference *CT_HdrFtrRef
}

func NewEG_HdrFtrReferences() *EG_HdrFtrReferences {
	ret := &EG_HdrFtrReferences{}
	return ret
}

func (m *EG_HdrFtrReferences) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.HeaderReference != nil {
		seheaderReference := xml.StartElement{Name: xml.Name{Local: "w:headerReference"}}
		e.EncodeElement(m.HeaderReference, seheaderReference)
	}
	if m.FooterReference != nil {
		sefooterReference := xml.StartElement{Name: xml.Name{Local: "w:footerReference"}}
		e.EncodeElement(m.FooterReference, sefooterReference)
	}
	return nil
}

func (m *EG_HdrFtrReferences) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
lEG_HdrFtrReferences:
	for {
		tok, err := d.Token()
		if err != nil {
			return err
		}
		switch el := tok.(type) {
		case xml.StartElement:
			switch el.Name {
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "headerReference"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "headerReference"}:
				m.HeaderReference = NewCT_HdrFtrRef()
				if err := d.DecodeElement(m.HeaderReference, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "footerReference"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "footerReference"}:
				m.FooterReference = NewCT_HdrFtrRef()
				if err := d.DecodeElement(m.FooterReference, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on EG_HdrFtrReferences %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lEG_HdrFtrReferences
		case xml.CharData:
		}
	}
	return nil
}

// Validate validates the EG_HdrFtrReferences and its children
func (m *EG_HdrFtrReferences) Validate() error {
	return m.ValidateWithPath("EG_HdrFtrReferences")
}

// ValidateWithPath validates the EG_HdrFtrReferences and its children, prefixing error messages with path
func (m *EG_HdrFtrReferences) ValidateWithPath(path string) error {
	if m.HeaderReference != nil {
		if err := m.HeaderReference.ValidateWithPath(path + "/HeaderReference"); err != nil {
			return err
		}
	}
	if m.FooterReference != nil {
		if err := m.FooterReference.ValidateWithPath(path + "/FooterReference"); err != nil {
			return err
		}
	}
	return nil
}
