package sml

import (
	"encoding/xml"
	"fmt"
	"strconv"

	"go.devnw.com/ooxml"
)

type CT_OleObject struct {
	// Embedded Object ProgId
	ProgIdAttr *string
	// Data or View Aspect
	DvAspectAttr ST_DvAspect
	// Embedded Object's Link Moniker
	LinkAttr *string
	// Linked Embedded Object Update
	OleUpdateAttr ST_OleUpdate
	// Auto Load
	AutoLoadAttr *bool
	// Shape Id
	ShapeIdAttr uint32
	IdAttr      *string
	// Embedded Object Properties
	ObjectPr *CT_ObjectPr
}

func NewCT_OleObject() *CT_OleObject {
	ret := &CT_OleObject{}
	return ret
}

func (m *CT_OleObject) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.ProgIdAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "progId"},
			Value: fmt.Sprintf("%v", *m.ProgIdAttr)})
	}
	if m.DvAspectAttr != ST_DvAspectUnset {
		attr, err := m.DvAspectAttr.MarshalXMLAttr(xml.Name{Local: "dvAspect"})
		if err != nil {
			return err
		}
		start.Attr = append(start.Attr, attr)
	}
	if m.LinkAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "link"},
			Value: fmt.Sprintf("%v", *m.LinkAttr)})
	}
	if m.OleUpdateAttr != ST_OleUpdateUnset {
		attr, err := m.OleUpdateAttr.MarshalXMLAttr(xml.Name{Local: "oleUpdate"})
		if err != nil {
			return err
		}
		start.Attr = append(start.Attr, attr)
	}
	if m.AutoLoadAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "autoLoad"},
			Value: fmt.Sprintf("%d", b2i(*m.AutoLoadAttr))})
	}
	start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "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)
	if m.ObjectPr != nil {
		seobjectPr := xml.StartElement{Name: xml.Name{Local: "ma:objectPr"}}
		e.EncodeElement(m.ObjectPr, seobjectPr)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_OleObject) 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 == "dvAspect" {
			m.DvAspectAttr.UnmarshalXMLAttr(attr)
			continue
		}
		if attr.Name.Local == "link" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.LinkAttr = &parsed
			continue
		}
		if attr.Name.Local == "oleUpdate" {
			m.OleUpdateAttr.UnmarshalXMLAttr(attr)
			continue
		}
		if attr.Name.Local == "autoLoad" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.AutoLoadAttr = &parsed
			continue
		}
		if attr.Name.Local == "shapeId" {
			parsed, err := strconv.ParseUint(attr.Value, 10, 32)
			if err != nil {
				return err
			}
			m.ShapeIdAttr = uint32(parsed)
			continue
		}
		if attr.Name.Local == "progId" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.ProgIdAttr = &parsed
			continue
		}
	}
lCT_OleObject:
	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/spreadsheetml/2006/main", Local: "objectPr"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/spreadsheetml/main", Local: "objectPr"}:
				m.ObjectPr = NewCT_ObjectPr()
				if err := d.DecodeElement(m.ObjectPr, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_OleObject %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_OleObject
		case xml.CharData:
		}
	}
	return nil
}

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

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