package photosapi

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
)

type Storage struct {
	BasePath string
	Path     string
}

func NewStorage(basePath, path string) *Storage {
	return &Storage{basePath, path}
}

func (s *Storage) Join(paths ...string) string {
	return filepath.Join(s.BasePath, s.Path, filepath.Join(paths...))
}

func (s *Storage) Create(paths ...string) error {
	return os.MkdirAll(s.Join(paths...), 0755)
}

func (s *Storage) Exists(paths ...string) bool {
	f, err := s.Stat(paths...)
	if err != nil {
		return false
	}
	return f.IsDir()
}

func (s *Storage) Delete(paths ...string) error {
	if s.Exists(paths...) {
		return os.RemoveAll(s.Join(paths...))
	} else {
		return fmt.Errorf("%s doesn't exists", s.Join(paths...))
	}
}

func (s *Storage) Stat(paths ...string) (fs.FileInfo, error) {
	return os.Stat(s.Join(paths...))
}