package gallery

import (
	"os"
	"path"
)

type fileWithPath struct {
	os.FileInfo
	path string
}

func (f fileWithPath) Name() string {
	return path.Join(f.path, f.FileInfo.Name())
}

func (s *Service) readDirRecursive(path string) (files []os.FileInfo, err error) {
	return s.readDirRecursiveTo([]os.FileInfo{}, path)
}

func (s *Service) readDirRecursiveTo(files []os.FileInfo, p ...string) ([]os.FileInfo, error) {
	localFiles, err := s.fs.ReadDir(path.Join(p...))
	if err != nil {
		return files, err
	}

	for _, file := range localFiles {
		if file.IsDir() {
			newP := append(p, file.Name())
			files, err = s.readDirRecursiveTo(files, newP...)
			if err != nil {
				return files, err
			}
		} else {
			files = append(files, fileWithPath{file, path.Join(p[1:]...)})
		}
	}

	return files, nil
}
