package com.css.simulation.resource.scheduler.app.service;

import api.common.util.FileUtil;
import api.common.util.HttpUtil;
import com.css.simulation.resource.scheduler.infra.configuration.custom.CustomConfiguration;
import com.css.simulation.resource.scheduler.infra.configuration.kubernetes.KubernetesConfiguration;
import com.css.simulation.resource.scheduler.infra.configuration.entity.NodeEntity;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class FileApplicationService {
    @Resource
    KubernetesConfiguration kubernetesConfiguration;
    @Resource
    CustomConfiguration customConfiguration;

    @SneakyThrows
    public void uploadOsgb(MultipartFile file) {
        final String targetFilePath = customConfiguration.getTemporaryDirectory() + "/esmini-osgb";
        FileUtil.writeInputStreamToLocalFile(file.getInputStream(), targetFilePath);
        final List<NodeEntity> nodeEntities = kubernetesConfiguration.getCpuNodeList();
        nodeEntities.forEach(nodeEntity -> HttpUtil.post(customConfiguration.getUploadOsgbUrl().replace("simulation-resource-video", nodeEntity.getIp()), new File(targetFilePath)));
    }
}
