package atom

import (
	"encoding/xml"
	"errors"
	"fmt"
)

type Icon struct {
	XMLName xml.Name `xml:"icon"`
	*CommonAttributes
	URI string `xml:",chardata"` // IRI
}

// NewIcon creates a new Icon. It takes in a string uri and returns a *Icon.
func NewIcon(uri string) *Icon {
	return &Icon{
		CommonAttributes: NewCommonAttributes(),
		URI:              uri,
	}
}

// Check checks the Icon for incompatibilities with RFC4287. It returns an
// error.
func (i *Icon) Check() error {
	if i.URI == "" {
		return errors.New("uri element of icon empty")
	} else {
		if !isValidIRI(i.URI) {
			return fmt.Errorf("uri attribute of icon %v not correctly formatted", i)
		}
	}

	return nil
}