package math

import (
	"encoding/xml"

	"go.devnw.com/ooxml"
)

type CT_OMathParaPr struct {
	Jc *CT_OMathJc
}

func NewCT_OMathParaPr() *CT_OMathParaPr {
	ret := &CT_OMathParaPr{}
	return ret
}

func (m *CT_OMathParaPr) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	e.EncodeToken(start)
	if m.Jc != nil {
		sejc := xml.StartElement{Name: xml.Name{Local: "m:jc"}}
		e.EncodeElement(m.Jc, sejc)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_OMathParaPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
lCT_OMathParaPr:
	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/officeDocument/2006/math", Local: "jc"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/officeDocument/math", Local: "jc"}:
				m.Jc = NewCT_OMathJc()
				if err := d.DecodeElement(m.Jc, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_OMathParaPr %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_OMathParaPr
		case xml.CharData:
		}
	}
	return nil
}

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

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