package wikipress

import "strings"

type File struct {
	Name     string
	Deleted  bool
	Versions []Version
}

func (f File) GetExtension() string {
	lastDot := strings.LastIndex(f.Name, ".")
	if lastDot == -1 {
		return ""
	}
	return f.Name[lastDot+1:]
}

func (f File) GetFolder() string {
	lastSlash := strings.LastIndex(f.Name, "/")
	if lastSlash == -1 {
		return ""
	}
	return f.Name[0:lastSlash]
}

func (f File) GetName() string {
	lastSlash := strings.LastIndex(f.Name, "/")
	lastDot := strings.LastIndex(f.Name, ".")

	return f.Name[lastSlash+1 : lastDot]
}

func (f File) GetFolders() []string {
	folder := f.GetFolder()
	res := strings.Split(folder, "/")
	return res
}

func (f File) GetTitle() string {
	title := f.GetLastVersion().GetHeader("title", f.GetName())
	return title
}

func (f File) GetLastVersion() Version {
	return f.Versions[len(f.Versions)-1]
}

func (f File) GetMenuPosition() int {
	if len(f.Versions) == 0 {
		return 1000
	}

	return f.GetLastVersion().GetHeaderInt("order", 1500)
}
