package math

import (
	"encoding/xml"

	"go.devnw.com/ooxml"
)

type CT_RPR struct {
	Lit    *CT_OnOff
	Choice *CT_RPRChoice
	Brk    *CT_ManualBreak
	Aln    *CT_OnOff
}

func NewCT_RPR() *CT_RPR {
	ret := &CT_RPR{}
	return ret
}

func (m *CT_RPR) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	e.EncodeToken(start)
	if m.Lit != nil {
		selit := xml.StartElement{Name: xml.Name{Local: "m:lit"}}
		e.EncodeElement(m.Lit, selit)
	}
	if m.Choice != nil {
		m.Choice.MarshalXML(e, xml.StartElement{})
	}
	if m.Brk != nil {
		sebrk := xml.StartElement{Name: xml.Name{Local: "m:brk"}}
		e.EncodeElement(m.Brk, sebrk)
	}
	if m.Aln != nil {
		sealn := xml.StartElement{Name: xml.Name{Local: "m:aln"}}
		e.EncodeElement(m.Aln, sealn)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_RPR) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
lCT_RPR:
	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: "lit"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/officeDocument/math", Local: "lit"}:
				m.Lit = NewCT_OnOff()
				if err := d.DecodeElement(m.Lit, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/officeDocument/2006/math", Local: "nor"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/officeDocument/math", Local: "nor"}:
				m.Choice = NewCT_RPRChoice()
				if err := d.DecodeElement(&m.Choice.Nor, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/officeDocument/2006/math", Local: "brk"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/officeDocument/math", Local: "brk"}:
				m.Brk = NewCT_ManualBreak()
				if err := d.DecodeElement(m.Brk, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/officeDocument/2006/math", Local: "aln"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/officeDocument/math", Local: "aln"}:
				m.Aln = NewCT_OnOff()
				if err := d.DecodeElement(m.Aln, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_RPR %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_RPR
		case xml.CharData:
		}
	}
	return nil
}

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

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