package sync

import (
	"fmt"
	"path/filepath"
	"strings"

	"srs.epita.fr/fic-server/libfic"
)

type CheckExceptions map[string]string

func (c *CheckExceptions) GetExerciceExceptions(e *fic.Exercice) *CheckExceptions {
	return c.GetFileExceptions(filepath.Base(e.Path))
}

func (c *CheckExceptions) GetFileExceptions(paths ...string) *CheckExceptions {
	if c == nil {
		return nil
	}

	ret := CheckExceptions{}

	for k, v := range *c {
		cols := strings.SplitN(k, ":", 2)
		if len(cols) < 2 {
			continue
		}

		for _, path := range paths {
			if strings.HasPrefix(cols[0], path) {
				k = strings.TrimPrefix(k, path)

				if strings.HasPrefix(k, "/") {
					k = strings.TrimPrefix(k, "/")
				}

				ret[k] = v
				break
			} else if eval, err := filepath.Match(cols[0], path); err == nil && eval {
				ret[k] = v
				break
			}
		}
	}

	return &ret
}

func (c *CheckExceptions) Filter2ndCol(str string) *CheckExceptions {
	if c == nil {
		return nil
	}

	ret := CheckExceptions{}

	for k, v := range *c {
		cols := strings.SplitN(k, ":", 3)
		if len(cols) < 2 {
			continue
		}

		if eval, err := filepath.Match(cols[1], str); err == nil && eval {
			ret[k] = v
			break
		}
	}

	return &ret
}

func (c *CheckExceptions) HasException(ref string) bool {
	if c == nil {
		return false
	}

	for k, _ := range *c {
		if strings.HasSuffix(k, ref) {
			return true
		}
	}

	return false
}

func LoadException(i Importer, th *fic.Theme) (exceptions *CheckExceptions) {
	if fexcept, err := GetFileContent(i, filepath.Join(th.Path, "repochecker-ack.txt")); err == nil {
		exceptions = &CheckExceptions{}
		for n, line := range strings.Split(fexcept, "\n") {
			(*exceptions)[strings.TrimSpace(line)] = fmt.Sprintf("repochecker-ack.txt:%d", n+1)
		}
	}

	return
}
