package duplicate

import (
	"bufio"
	"io/fs"
	"os"
	"path/filepath"
	"skapp/pkg/sdk/file/hash"

	"skapp/pkg/logger"
	fileSdk "skapp/pkg/sdk/file"
)

var log = logger.Log

type Support struct {
	fileSupport *fileSdk.Support
	hash        *hash.Support
}

func New(fileSupport *fileSdk.Support, hash *hash.Support) *Support {
	return &Support{fileSupport, hash}
}

var (
	maxReadSize int64 = 1024 * 1024 * 5
	chunkSize         = 1024 * 1024
)

// RecursiveScan 递归 目录下需要扫描的文件
func (s *Support) RecursiveScan(dir string, addFile bool, addDir bool) []string {
	//nodeModules, _ := regexp.Compile("node_modules")
	//if nodeModules.Match([]byte(dir)) {
	//	return []string{}
	//}

	files := make([]string, 0)
	fileMap := make(map[string]bool)
	//suffixReg, _ := regexp.Compile(".*[\\.j(t)s|\\.vue|\\.jsx|\\.tsx]$")

	absPath, _ := filepath.Abs(dir)
	_ = filepath.Walk(absPath, func(file string, info fs.FileInfo, err error) error {
		if info.Mode() == os.ModeSymlink {
			file, err = os.Readlink(file)
			s.RecursiveScan(file, addFile, addDir)
			return err
		}

		if addFile {
			if s.fileSupport.IsFile(file) {
				//if suffixReg.Match([]byte(file)) {
				log.Infof("[扫描文件] 添加扫描文件 %s", file)
				fileMap[file] = true
				//}
			}
		}

		if addDir {
			if s.fileSupport.IsDir(file) {
				log.Infof("[扫描文件] 添加扫描文件夹 %s", file)
				fileMap[file] = true
			}
		}

		return nil
	})

	files = make([]string, 0, len(fileMap))
	for file := range fileMap {
		files = append(files, file)
	}

	return files
}

func Reader(file *os.File) *bufio.Reader {
	return bufio.NewReaderSize(file, chunkSize)
}

func (s *Support) CalcSHA1(path string) (sha1hex string, err error) {
	return s.hash.CalcSHA1(path, maxReadSize)
}

func (s *Support) CalcMD5(path string) (md5hex string, err error) {
	return s.hash.CalcMD5(path, maxReadSize)
}
