// +build ignore

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/aymerick/douceur/parser"
	"os"
)

const fileHeader = `// THIS FILE WAS AUTOMATICALLY GENERATED BY A TOOL
// Use ` + "`go generate`" + ` to generate this.

package semanticiconsugc

// SaneIcons are the icons that are deemed to be safe to be accepted from user
// generated content.
var SaneIcons = []string{
`

func main() {
	resp, err := http.Get("https://raw.githubusercontent.com/Semantic-Org/Semantic-UI/master/dist/components/icon.css")
	if err != nil {
		panic(err)
	}
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	s := strings.Replace(string(b), "-webkit-", "", -1)
	data, err := parser.Parse(s)
	if err != nil {
		panic(err)
	}

	f, err := os.Create("sane.go")
	defer f.Close()
	if err != nil {
		panic(err)
	}
	f.WriteString(fileHeader)
	
	blacklist := [...]string{
		"icons",
		"icon:first-child",
		"\\35 00px", // seems like a semantic bug
		"fitted",
		"hover",
		"active",
		"emphasized",
		"icons",
		"link",
		"circular",
		"bordered",
		"miny",
		"tiny",
		"small",
		"large",
		"big",
		"huge",
		"massive",
		"corner",
	}
	written := make([]string, 0, 500)
	// four nested for-ranges! jesus h. christ!
	for _, r := range data.Rules {
		if r == nil {
			continue
		}
		for _, s := range r.Selectors {
			s = strings.TrimPrefix(s, "i.")
			s = strings.TrimSuffix(s, ":before")
			s = strings.TrimSuffix(s, ".icon")
			if s == "" {
				continue
			}
			classes := strings.Split(s, ".")
			for _, p := range classes {
				var b bool
				for _, bi := range blacklist {
					if bi == p {
						b = true
						break
					}
				}
				if b {
					break
				}
				for _, w := range written {
					if w == p {
						b = true
						break
					}
				}
				if b {
					continue
				}
				written = append(written, p)
				fmt.Fprintf(f, "\t%q,\n", p)
			}
		}
	}

	f.WriteString("}\n")
}
