package dml

import (
	"encoding/xml"

	"go.devnw.com/ooxml"
)

type CT_Backdrop struct {
	Anchor *CT_Point3D
	Norm   *CT_Vector3D
	Up     *CT_Vector3D
	ExtLst *CT_OfficeArtExtensionList
}

func NewCT_Backdrop() *CT_Backdrop {
	ret := &CT_Backdrop{}
	ret.Anchor = NewCT_Point3D()
	ret.Norm = NewCT_Vector3D()
	ret.Up = NewCT_Vector3D()
	return ret
}

func (m *CT_Backdrop) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	e.EncodeToken(start)
	seanchor := xml.StartElement{Name: xml.Name{Local: "a:anchor"}}
	e.EncodeElement(m.Anchor, seanchor)
	senorm := xml.StartElement{Name: xml.Name{Local: "a:norm"}}
	e.EncodeElement(m.Norm, senorm)
	seup := xml.StartElement{Name: xml.Name{Local: "a:up"}}
	e.EncodeElement(m.Up, seup)
	if m.ExtLst != nil {
		seextLst := xml.StartElement{Name: xml.Name{Local: "a:extLst"}}
		e.EncodeElement(m.ExtLst, seextLst)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_Backdrop) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
	m.Anchor = NewCT_Point3D()
	m.Norm = NewCT_Vector3D()
	m.Up = NewCT_Vector3D()
lCT_Backdrop:
	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/drawingml/2006/main", Local: "anchor"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/drawingml/main", Local: "anchor"}:
				if err := d.DecodeElement(m.Anchor, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/drawingml/2006/main", Local: "norm"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/drawingml/main", Local: "norm"}:
				if err := d.DecodeElement(m.Norm, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/drawingml/2006/main", Local: "up"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/drawingml/main", Local: "up"}:
				if err := d.DecodeElement(m.Up, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/drawingml/2006/main", Local: "extLst"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/drawingml/main", Local: "extLst"}:
				m.ExtLst = NewCT_OfficeArtExtensionList()
				if err := d.DecodeElement(m.ExtLst, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_Backdrop %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_Backdrop
		case xml.CharData:
		}
	}
	return nil
}

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

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