package core

import (
	"crypto/sha256"
	"hash"
	"io"

	"github.com/gabriel-vasile/mimetype"
)

type contentProps struct {
	dest            io.WriteCloser
	len             int
	sum             hash.Hash
	contents        []byte
	successCallback func(int, hash.Hash, string) error
}

func computeProps(dest io.WriteCloser, successCallback func(int, hash.Hash, string) error) io.WriteCloser {
	return &contentProps{
		dest:            dest,
		sum:             sha256.New(),
		contents:        make([]byte, 0, 3072),
		successCallback: successCallback,
	}
}

func (c *contentProps) Write(p []byte) (n int, err error) {
	n, err = c.dest.Write(p)
	c.len += n
	if c.sum != nil {
		c.sum.Write(p)
	}
	remain := min(cap(c.contents)-len(c.contents), len(p))
	if remain > 0 {
		c.contents = append(c.contents, p[:remain]...)
	}
	return
}

func min(a, b int) int {
	if b < a {
		return b
	}
	return a

}

func (c *contentProps) Close() error {
	if err := c.dest.Close(); err != nil {
		return err
	}
	return c.successCallback(c.len, c.sum, mimetype.Detect(c.contents).String())
}
