package sml

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

	"go.devnw.com/ooxml"
)

type CT_CustomFilters struct {
	// And
	AndAttr *bool
	// Custom Filter Criteria
	CustomFilter []*CT_CustomFilter
}

func NewCT_CustomFilters() *CT_CustomFilters {
	ret := &CT_CustomFilters{}
	return ret
}

func (m *CT_CustomFilters) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if m.AndAttr != nil {
		start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "and"},
			Value: fmt.Sprintf("%d", b2i(*m.AndAttr))})
	}
	e.EncodeToken(start)
	secustomFilter := xml.StartElement{Name: xml.Name{Local: "ma:customFilter"}}
	for _, c := range m.CustomFilter {
		e.EncodeElement(c, secustomFilter)
	}
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}

func (m *CT_CustomFilters) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	// initialize to default
	for _, attr := range start.Attr {
		if attr.Name.Local == "and" {
			parsed, err := strconv.ParseBool(attr.Value)
			if err != nil {
				return err
			}
			m.AndAttr = &parsed
			continue
		}
	}
lCT_CustomFilters:
	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: "customFilter"},
				xml.Name{Space: "http://purl.oclc.org/ooxml/spreadsheetml/main", Local: "customFilter"}:
				tmp := NewCT_CustomFilter()
				if err := d.DecodeElement(tmp, &el); err != nil {
					return err
				}
				m.CustomFilter = append(m.CustomFilter, tmp)
			default:
				office.Log("skipping unsupported element on CT_CustomFilters %v", el.Name)
				if err := d.Skip(); err != nil {
					return err
				}
			}
		case xml.EndElement:
			break lCT_CustomFilters
		case xml.CharData:
		}
	}
	return nil
}

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

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