package mock

import (
	"fmt"
	"io"
	"io/ioutil"

	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
)

var downloadurl = envtool.GetEnv("OTA_UPDATE_DOWNLOAD_URL", "https://upload-dev.fiskerdps.com")

// S3Mock S3 mock
type S3Mock struct {
	err  error
	data []byte
}

// PutBucket mock S3.PutBucket method
func (s *S3Mock) PutBucket(key string, reader io.Reader, contentType string) (string, error) {
	if s.err != nil {
		return "", s.err
	}
	var err error
	s.data, err = ioutil.ReadAll(reader)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%s/%s", downloadurl, key), nil
}

// SetError sets error for PutBucket
func (s *S3Mock) SetError(err error) {
	s.err = err
}

// GetData returns data from reader
func (s *S3Mock) GetData() []byte {
	return s.data
}
