package mtree

import (
	"fmt"
	"strings"

	"github.com/vbatts/go-mtree/pkg/govis"
)

// DefaultVisFlags is the set of Vis flags used when encoding filenames and
// other similar entries.
const DefaultVisFlags govis.VisFlag = govis.VisWhite | govis.VisOctal | govis.VisGlob

// Keyword is the string name of a keyword, with some convenience functions for
// determining whether it is a default or bsd standard keyword.
type Keyword string

// Default returns whether this keyword is in the default set of keywords
func (k Keyword) Default() bool {
	return InKeywordSlice(k, DefaultKeywords)
}

// Bsd returns whether this keyword is in the upstream FreeBSD mtree(8)
func (k Keyword) Bsd() bool {
	return InKeywordSlice(k, BsdKeywords)
}

// Synonym returns the canonical name for this keyword. This is provides the
// same functionality as KeywordSynonym()
func (k Keyword) Synonym() Keyword {
	return KeywordSynonym(string(k))
}

// InKeywordSlice checks for the presence of `a` in `list`
func InKeywordSlice(a Keyword, list []Keyword) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}
func inKeyValSlice(a KeyVal, list []KeyVal) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

// ToKeywords makes a list of Keyword from a list of string
func ToKeywords(list []string) []Keyword {
	ret := make([]Keyword, len(list))
	for i := range list {
		ret[i] = Keyword(list[i])
	}
	return ret
}

// FromKeywords makes a list of string from a list of Keyword
func FromKeywords(list []Keyword) []string {
	ret := make([]string, len(list))
	for i := range list {
		ret[i] = string(list[i])
	}
	return ret
}

// KeyValToString constructs a list of string from the list of KeyVal
func KeyValToString(list []KeyVal) []string {
	ret := make([]string, len(list))
	for i := range list {
		ret[i] = string(list[i])
	}
	return ret
}

// StringToKeyVals constructs a list of KeyVal from the list of strings, like "keyword=value"
func StringToKeyVals(list []string) []KeyVal {
	ret := make([]KeyVal, len(list))
	for i := range list {
		ret[i] = KeyVal(list[i])
	}
	return ret
}

// KeyVal is a "keyword=value"
type KeyVal string

// Keyword is the mapping to the available keywords
func (kv KeyVal) Keyword() Keyword {
	if !strings.Contains(string(kv), "=") {
		return Keyword("")
	}
	chunks := strings.SplitN(strings.TrimSpace(string(kv)), "=", 2)[0]
	if !strings.Contains(chunks, ".") {
		return Keyword(chunks)
	}
	return Keyword(strings.SplitN(chunks, ".", 2)[0])
}

// KeywordSuffix is really only used for xattr, as the keyword is a prefix to
// the xattr "namespace.key"
func (kv KeyVal) KeywordSuffix() string {
	if !strings.Contains(string(kv), "=") {
		return ""
	}
	chunks := strings.SplitN(strings.TrimSpace(string(kv)), "=", 2)[0]
	if !strings.Contains(chunks, ".") {
		return ""
	}
	return strings.SplitN(chunks, ".", 2)[1]
}

// Value is the data/value portion of "keyword=value"
func (kv KeyVal) Value() string {
	if !strings.Contains(string(kv), "=") {
		return ""
	}
	return strings.SplitN(strings.TrimSpace(string(kv)), "=", 2)[1]
}

// ChangeValue changes the value of a KeyVal
func (kv KeyVal) ChangeValue(newval string) string {
	return fmt.Sprintf("%s=%s", kv.Keyword(), newval)
}

// KeyValEqual returns whether two KeyVal are equivalent. This takes
// care of certain odd cases such as tar_mtime, and should be used over
// using == comparisons directly unless you really know what you're
// doing.
func KeyValEqual(a, b KeyVal) bool {
	// TODO: Implement handling of tar_mtime.
	return a.Keyword() == b.Keyword() && a.Value() == b.Value()
}

// keyvalSelector takes an array of KeyVal ("keyword=value") and filters out that only the set of keywords
func keyvalSelector(keyval []KeyVal, keyset []Keyword) []KeyVal {
	retList := []KeyVal{}
	for _, kv := range keyval {
		if InKeywordSlice(kv.Keyword(), keyset) {
			retList = append(retList, kv)
		}
	}
	return retList
}

func keyValDifference(this, that []KeyVal) []KeyVal {
	if len(this) == 0 {
		return that
	}
	diff := []KeyVal{}
	for _, kv := range this {
		if !inKeyValSlice(kv, that) {
			diff = append(diff, kv)
		}
	}
	return diff
}
func keyValCopy(set []KeyVal) []KeyVal {
	ret := make([]KeyVal, len(set))
	for i := range set {
		ret[i] = set[i]
	}
	return ret
}

// Has the "keyword" present in the list of KeyVal, and returns the
// corresponding KeyVal, else an empty string.
func Has(keyvals []KeyVal, keyword string) KeyVal {
	return HasKeyword(keyvals, Keyword(keyword))
}

// HasKeyword the "keyword" present in the list of KeyVal, and returns the
// corresponding KeyVal, else an empty string.
func HasKeyword(keyvals []KeyVal, keyword Keyword) KeyVal {
	for i := range keyvals {
		if keyvals[i].Keyword() == keyword {
			return keyvals[i]
		}
	}
	return emptyKV
}

var emptyKV = KeyVal("")

// MergeSet takes the current setKeyVals, and then applies the entryKeyVals
// such that the entry's values win. The union is returned.
func MergeSet(setKeyVals, entryKeyVals []string) []KeyVal {
	retList := StringToKeyVals(setKeyVals)
	eKVs := StringToKeyVals(entryKeyVals)
	return MergeKeyValSet(retList, eKVs)
}

// MergeKeyValSet does a merge of the two sets of KeyVal, and the KeyVal of
// entryKeyVals win when there is a duplicate Keyword.
func MergeKeyValSet(setKeyVals, entryKeyVals []KeyVal) []KeyVal {
	retList := keyValCopy(setKeyVals)
	seenKeywords := []Keyword{}
	for i := range retList {
		word := retList[i].Keyword()
		if ekv := HasKeyword(entryKeyVals, word); ekv != emptyKV {
			retList[i] = ekv
		}
		seenKeywords = append(seenKeywords, word)
	}
	for i := range entryKeyVals {
		if !InKeywordSlice(entryKeyVals[i].Keyword(), seenKeywords) {
			retList = append(retList, entryKeyVals[i])
		}
	}
	return retList
}

var (
	// DefaultKeywords has the several default keyword producers (uid, gid,
	// mode, nlink, type, size, mtime)
	DefaultKeywords = []Keyword{
		"size",
		"type",
		"uid",
		"gid",
		"mode",
		"link",
		"nlink",
		"time",
	}

	// DefaultTarKeywords has keywords that should be used when creating a manifest from
	// an archive. Currently, evaluating the # of hardlinks has not been implemented yet
	DefaultTarKeywords = []Keyword{
		"size",
		"type",
		"uid",
		"gid",
		"mode",
		"link",
		"tar_time",
	}

	// BsdKeywords is the set of keywords that is only in the upstream FreeBSD mtree
	BsdKeywords = []Keyword{
		"cksum",
		"device",
		"flags", // this one is really mostly BSD specific ...
		"ignore",
		"gid",
		"gname",
		"link",
		"md5",
		"md5digest",
		"mode",
		"nlink",
		"nochange",
		"optional",
		"ripemd160digest",
		"rmd160",
		"rmd160digest",
		"sha1",
		"sha1digest",
		"sha256",
		"sha256digest",
		"sha384",
		"sha384digest",
		"sha512",
		"sha512digest",
		"size",
		"tags",
		"time",
		"type",
		"uid",
		"uname",
	}

	// SetKeywords is the default set of keywords calculated for a `/set` SpecialType
	SetKeywords = []Keyword{
		"uid",
		"gid",
	}
)

// KeywordSynonym returns the canonical name for keywords that have synonyms,
// and just returns the name provided if there is no synonym. In this way it
// ought to be safe to wrap any keyword name.
func KeywordSynonym(name string) Keyword {
	var retname string
	switch name {
	case "md5":
		retname = "md5digest"
	case "rmd160":
		retname = "ripemd160digest"
	case "rmd160digest":
		retname = "ripemd160digest"
	case "sha1":
		retname = "sha1digest"
	case "sha256":
		retname = "sha256digest"
	case "sha384":
		retname = "sha384digest"
	case "sha512":
		retname = "sha512digest"
	case "xattrs":
		retname = "xattr"
	default:
		retname = name
	}
	return Keyword(retname)
}
