package pml

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

	"go.devnw.com/ooxml"
)

type CT_CommonSlideViewProperties struct {
	// Snap Objects to Grid
	SnapToGridAttr *bool
	// Snap Objects to Objects
	SnapToObjectsAttr *bool
	// Show Guides in View
	ShowGuidesAttr *bool
	// Base properties for Slide View
	CViewPr *CT_CommonViewProperties
	// List of Guides
	GuideLst *CT_GuideList
}

func NewCT_CommonSlideViewProperties() *CT_CommonSlideViewProperties {
	ret := &CT_CommonSlideViewProperties{}
	ret.CViewPr = NewCT_CommonViewProperties()
	return ret
}

func (m *CT_CommonSlideViewProperties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.SnapToGridAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "snapToGrid"},
			Value: fmt.Sprintf("%d", b2i(*m.SnapToGridAttr))})
	}
	if m.SnapToObjectsAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "snapToObjects"},
			Value: fmt.Sprintf("%d", b2i(*m.SnapToObjectsAttr))})
	}
	if m.ShowGuidesAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "showGuides"},
			Value: fmt.Sprintf("%d", b2i(*m.ShowGuidesAttr))})
	}
	e.EncodeToken(start)
	secViewPr := xml.StartElement{Name: xml.Name{Local: "p:cViewPr"}}
	e.EncodeElement(m.CViewPr, secViewPr)
	if m.GuideLst != nil {
		seguideLst := xml.StartElement{Name: xml.Name{Local: "p:guideLst"}}
		e.EncodeElement(m.GuideLst, seguideLst)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_CommonSlideViewProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
	m.CViewPr = NewCT_CommonViewProperties()
	for _, attr := range start.Attr {
		if attr.Name.Local == "snapToGrid" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.SnapToGridAttr = &parsed
			continue
		}
		if attr.Name.Local == "snapToObjects" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.SnapToObjectsAttr = &parsed
			continue
		}
		if attr.Name.Local == "showGuides" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.ShowGuidesAttr = &parsed
			continue
		}
	}
lCT_CommonSlideViewProperties:
	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/presentationml/2006/main", Local: "cViewPr"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/presentationml/main", Local: "cViewPr"}:
				if err := d.DecodeElement(m.CViewPr, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/presentationml/2006/main", Local: "guideLst"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/presentationml/main", Local: "guideLst"}:
				m.GuideLst = NewCT_GuideList()
				if err := d.DecodeElement(m.GuideLst, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_CommonSlideViewProperties %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_CommonSlideViewProperties
		case xml.CharData:
		}
	}
	return nil
}

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

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