package hash

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

type Support struct {
	fileSupport *fileSdk.Support
}

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

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

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

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

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

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
}
