package wml

import (
	"encoding/xml"
	"fmt"
)

type CT_Control struct {
	// Unique Name for Embedded Control
	NameAttr *string
	// Shape Reference
	ShapeidAttr *string
	IdAttr      *string
}

func NewCT_Control() *CT_Control {
	ret := &CT_Control{}
	return ret
}

func (m *CT_Control) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.NameAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:name"},
			Value: fmt.Sprintf("%v", *m.NameAttr)})
	}
	if m.ShapeidAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:shapeid"},
			Value: fmt.Sprintf("%v", *m.ShapeidAttr)})
	}
	if m.IdAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "r:id"},
			Value: fmt.Sprintf("%v", *m.IdAttr)})
	}
	e.EncodeToken(start)
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_Control) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
	for _, attr := range start.Attr {
		if attr.Name.Space == "http://schemas.openxmlformats.org/officeDocument/2006/relationships" && attr.Name.Local == "id" ||
			attr.Name.Space == "http://purl.oclc.org/ooxml/officeDocument/relationships" && attr.Name.Local == "id" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.IdAttr = &parsed
			continue
		}
		if attr.Name.Local == "name" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.NameAttr = &parsed
			continue
		}
		if attr.Name.Local == "shapeid" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.ShapeidAttr = &parsed
			continue
		}
	}
	// skip any extensions we may find, but don't support
	for {
		tok, err := d.Token()
		if err != nil {
			return fmt.Errorf("parsing CT_Control: %s", err)
		}
		if el, ok := tok.(xml.EndElement); ok && el.Name == start.Name {
			break
		}
	}
	return nil
}

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

// ValidateWithPath validates the CT_Control and its children, prefixing error messages with path
func (m *CT_Control) ValidateWithPath(path string) error {
	return nil
}
