package wml

import (
	"encoding/xml"
	"fmt"

	"go.devnw.com/ooxml"
)

type CT_Frameset struct {
	// Nested Frameset Size
	Sz *CT_String
	// Frameset Splitter Properties
	FramesetSplitbar *CT_FramesetSplitbar
	// Frameset Layout
	FrameLayout *CT_FrameLayout
	Title       *CT_String
	Choice      []*CT_FramesetChoice
}

func NewCT_Frameset() *CT_Frameset {
	ret := &CT_Frameset{}
	return ret
}

func (m *CT_Frameset) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	e.EncodeToken(start)
	if m.Sz != nil {
		sesz := xml.StartElement{Name: xml.Name{Local: "w:sz"}}
		e.EncodeElement(m.Sz, sesz)
	}
	if m.FramesetSplitbar != nil {
		seframesetSplitbar := xml.StartElement{Name: xml.Name{Local: "w:framesetSplitbar"}}
		e.EncodeElement(m.FramesetSplitbar, seframesetSplitbar)
	}
	if m.FrameLayout != nil {
		seframeLayout := xml.StartElement{Name: xml.Name{Local: "w:frameLayout"}}
		e.EncodeElement(m.FrameLayout, seframeLayout)
	}
	if m.Title != nil {
		setitle := xml.StartElement{Name: xml.Name{Local: "w:title"}}
		e.EncodeElement(m.Title, setitle)
	}
	if m.Choice != nil {
		for _, c := range m.Choice {
			c.MarshalXML(e, xml.StartElement{})
		}
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_Frameset) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
lCT_Frameset:
	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/wordprocessingml/2006/main", Local: "sz"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "sz"}:
				m.Sz = NewCT_String()
				if err := d.DecodeElement(m.Sz, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "framesetSplitbar"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "framesetSplitbar"}:
				m.FramesetSplitbar = NewCT_FramesetSplitbar()
				if err := d.DecodeElement(m.FramesetSplitbar, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "frameLayout"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "frameLayout"}:
				m.FrameLayout = NewCT_FrameLayout()
				if err := d.DecodeElement(m.FrameLayout, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "title"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "title"}:
				m.Title = NewCT_String()
				if err := d.DecodeElement(m.Title, &el); err != nil {
					return err
				}
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "frameset"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "frameset"}:
				tmp := NewCT_FramesetChoice()
				if err := d.DecodeElement(&tmp.Frameset, &el); err != nil {
					return err
				}
				m.Choice = append(m.Choice, tmp)
			case xml.Name{Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main", Local: "frame"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/wordprocessingml/main", Local: "frame"}:
				tmp := NewCT_FramesetChoice()
				if err := d.DecodeElement(&tmp.Frame, &el); err != nil {
					return err
				}
				m.Choice = append(m.Choice, tmp)
			default:
				office.Log("skipping unsupported element on CT_Frameset %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_Frameset
		case xml.CharData:
		}
	}
	return nil
}

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

// ValidateWithPath validates the CT_Frameset and its children, prefixing error messages with path
func (m *CT_Frameset) ValidateWithPath(path string) error {
	if m.Sz != nil {
		if err := m.Sz.ValidateWithPath(path + "/Sz"); err != nil {
			return err
		}
	}
	if m.FramesetSplitbar != nil {
		if err := m.FramesetSplitbar.ValidateWithPath(path + "/FramesetSplitbar"); err != nil {
			return err
		}
	}
	if m.FrameLayout != nil {
		if err := m.FrameLayout.ValidateWithPath(path + "/FrameLayout"); err != nil {
			return err
		}
	}
	if m.Title != nil {
		if err := m.Title.ValidateWithPath(path + "/Title"); err != nil {
			return err
		}
	}
	for i, v := range m.Choice {
		if err := v.ValidateWithPath(fmt.Sprintf("%s/Choice[%d]", path, i)); err != nil {
			return err
		}
	}
	return nil
}
