package pml

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

	"go.devnw.com/ooxml"
)

type CT_WebProperties struct {
	// Show animation in HTML output
	ShowAnimationAttr *bool
	// Resize graphics in HTML output
	ResizeGraphicsAttr *bool
	// Allow PNG in HTML output
	AllowPngAttr *bool
	// Rely on VML for HTML output
	RelyOnVmlAttr *bool
	// Organize HTML output in folders
	OrganizeInFoldersAttr *bool
	// Use long file names in HTML output
	UseLongFilenamesAttr *bool
	// Image size for HTML output
	ImgSzAttr ST_WebScreenSize
	// Encoding for HTML output
	EncodingAttr *string
	// Slide Navigation Colors for HTML output
	ClrAttr ST_WebColorType
	ExtLst  *CT_ExtensionList
}

func NewCT_WebProperties() *CT_WebProperties {
	ret := &CT_WebProperties{}
	return ret
}

func (m *CT_WebProperties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.ShowAnimationAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "showAnimation"},
			Value: fmt.Sprintf("%d", b2i(*m.ShowAnimationAttr))})
	}
	if m.ResizeGraphicsAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "resizeGraphics"},
			Value: fmt.Sprintf("%d", b2i(*m.ResizeGraphicsAttr))})
	}
	if m.AllowPngAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "allowPng"},
			Value: fmt.Sprintf("%d", b2i(*m.AllowPngAttr))})
	}
	if m.RelyOnVmlAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "relyOnVml"},
			Value: fmt.Sprintf("%d", b2i(*m.RelyOnVmlAttr))})
	}
	if m.OrganizeInFoldersAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "organizeInFolders"},
			Value: fmt.Sprintf("%d", b2i(*m.OrganizeInFoldersAttr))})
	}
	if m.UseLongFilenamesAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "useLongFilenames"},
			Value: fmt.Sprintf("%d", b2i(*m.UseLongFilenamesAttr))})
	}
	if m.ImgSzAttr != ST_WebScreenSizeUnset {
		attr, err := m.ImgSzAttr.MarshalXMLAttr(xml.Name{Local: "imgSz"})
		if err != nil {
			return err
		}
		start.Attr = append(start.Attr, attr)
	}
	if m.EncodingAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "encoding"},
			Value: fmt.Sprintf("%v", *m.EncodingAttr)})
	}
	if m.ClrAttr != ST_WebColorTypeUnset {
		attr, err := m.ClrAttr.MarshalXMLAttr(xml.Name{Local: "clr"})
		if err != nil {
			return err
		}
		start.Attr = append(start.Attr, attr)
	}
	e.EncodeToken(start)
	if m.ExtLst != nil {
		seextLst := xml.StartElement{Name: xml.Name{Local: "p:extLst"}}
		e.EncodeElement(m.ExtLst, seextLst)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_WebProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
	for _, attr := range start.Attr {
		if attr.Name.Local == "showAnimation" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.ShowAnimationAttr = &parsed
			continue
		}
		if attr.Name.Local == "resizeGraphics" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.ResizeGraphicsAttr = &parsed
			continue
		}
		if attr.Name.Local == "allowPng" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.AllowPngAttr = &parsed
			continue
		}
		if attr.Name.Local == "relyOnVml" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.RelyOnVmlAttr = &parsed
			continue
		}
		if attr.Name.Local == "organizeInFolders" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.OrganizeInFoldersAttr = &parsed
			continue
		}
		if attr.Name.Local == "useLongFilenames" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.UseLongFilenamesAttr = &parsed
			continue
		}
		if attr.Name.Local == "imgSz" {
			m.ImgSzAttr.UnmarshalXMLAttr(attr)
			continue
		}
		if attr.Name.Local == "encoding" {
			parsed, err := attr.Value, error(nil)
			if err != nil {
				return err
			}
			m.EncodingAttr = &parsed
			continue
		}
		if attr.Name.Local == "clr" {
			m.ClrAttr.UnmarshalXMLAttr(attr)
			continue
		}
	}
lCT_WebProperties:
	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: "extLst"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/presentationml/main", Local: "extLst"}:
				m.ExtLst = NewCT_ExtensionList()
				if err := d.DecodeElement(m.ExtLst, &el); err != nil {
					return err
				}
			default:
				office.Log("skipping unsupported element on CT_WebProperties %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_WebProperties
		case xml.CharData:
		}
	}
	return nil
}

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

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