package hash

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"encoding/hex"
	fileSdk "skapp/pkg/sdk/file"
)

type Support struct {
	fileSupport *fileSdk.Support
}

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

func (s *Support) CalcSha512(path string, readSize int64) (hash string, err error) {
	h := sha512.New()
	_, err = s.fileSupport.CopyN(h, path, readSize)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(h.Sum(nil)), nil
}

func (s *Support) CalcSha256(path string, readSize int64) (hash string, err error) {
	h := sha256.New()
	_, err = s.fileSupport.CopyN(h, path, readSize)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(h.Sum(nil)), nil
}

func (s *Support) CalcSHA1(path string, readSize int64) (hash string, err error) {
	h := sha1.New()
	_, err = s.fileSupport.CopyN(h, path, readSize)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(h.Sum(nil)), nil
}

func (s *Support) CalcMD5(path string, readSize int64) (hash string, err error) {
	h := md5.New()
	_, err = s.fileSupport.CopyN(h, path, readSize)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(h.Sum(nil)), nil
}
