package testdata

import (
	"bytes"
	"embed"
	"io"
	"os"
	"strings"
)

//go:embed *
var fs embed.FS

func FS() mockFS {
	return mockFS{
		FS: fs,
	}
}

type mockFS struct {
	embed.FS
}

type mockMimer struct{ os.FileInfo }

func (m *mockMimer) ContentType() string {
	parts := strings.Split(m.Name(), ".")

	suffix := parts[len(parts)-1]

	switch suffix {
	case "jpg":
		return "image/jpeg"
	default:
		return ""
	}
}

func (m mockFS) Read(path string) (b []byte, err error) {
	file, err := m.Open(path)
	if err != nil {
		return
	}
	defer file.Close()

	buff := bytes.NewBuffer(make([]byte, 0, 1024))

	_, err = io.Copy(buff, file)

	return buff.Bytes(), err
}

func (m mockFS) Stat(path string) (info os.FileInfo, err error) {
	file, err := m.Open(path)
	if err != nil {
		return
	}

	info, err = file.Stat()
	info = &mockMimer{info}
	return
}
