package com.tsiwebserver.service.impl;

import com.tsiwebserver.service.FilesStorageService;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.CopyOption;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class FilesStorageServiceImpl implements FilesStorageService {
    private final Path root;

    public FilesStorageServiceImpl(@Value("${image-directory}")String path){
        this.root = Paths.get(path);
    }

    public void init() {
        try {
            Files.createDirectory(this.root);
        } catch (IOException var2) {
            throw new RuntimeException("Could not initialize folder for upload!");
        }
    }

    public void save(MultipartFile file) {
        try {
            log.info("file : {}",file);
            Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()), new CopyOption[0]);
        } catch (IOException var3) {
//            throw new RuntimeException("Could not store the file. Error: " + var3.getMessage());
            throw new RuntimeException("Could not store the file. Error: IOException");
        }
    }

    public boolean delete(String filename) {
        try {
            Path file = this.root.resolve(filename);
            return Files.deleteIfExists(file);
        } catch (IOException var3) {
            throw new RuntimeException("File Delete - Cause IOException");
//            throw new RuntimeException("Error: " + var3.getMessage());
        }
    }

    public Resource load(String filename) {
        try {
            Path file = this.root.resolve(filename);
            Resource resource = new UrlResource(file.toUri());
            if (!resource.exists() && !resource.isReadable()) {
                throw new RuntimeException("Could not read the file!");
            } else {
                return resource;
            }
        } catch (MalformedURLException var4) {
//            throw new RuntimeException("Error: " + var4.getMessage());
            throw new RuntimeException("Cause MalformedURLException");
        }
    }

    public void deleteAll() {
        FileSystemUtils.deleteRecursively(this.root.toFile());
    }

    public Stream<Path> loadAll() {
        try {
            Stream<Path> var10000 = Files.walk(this.root, 1, new FileVisitOption[0]).filter((path) -> {
                return !path.equals(this.root);
            });
            Path var10001 = this.root;
            Objects.requireNonNull(var10001);
            return var10000.map(var10001::relativize);
        } catch (IOException var2) {
            throw new RuntimeException("Could not load the files!");
        }
    }
}
