package cd.casic.ci.process.process.service.testCase.impl;

import cd.casic.ci.process.constant.TestCaseAIGeneratorConstant;
import cd.casic.ci.process.dto.req.testCase.TestCaseAITargetUploadReq;
import cd.casic.ci.process.dto.req.testCase.TestCaseAITaskCreateReq;
import cd.casic.ci.process.process.service.testCase.TestCaseAIGeneratorService;
import cd.casic.ci.process.properties.TestCaseAIProperties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amazonaws.util.IOUtils;
import jakarta.annotation.Resource;
import net.schmizz.sshj.transport.kex.ECDHNistP;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

@Service
public class TestCaseAIGeneratorServiceImpl implements TestCaseAIGeneratorService {
    @Resource
    private TestCaseAIProperties properties;
    @Resource
    private RestTemplate restTemplate;


    @Override
    public String targetUpload(String binaryPath, InputStream inputStream) throws IOException {
        File tempFile = File.createTempFile("target", ".zip");
        tempFile.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(tempFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry entry = new ZipEntry(tempFile.getName());
        zos.putNextEntry(entry);
        IOUtils.copy(inputStream,zos);
        zos.closeEntry();
        zos.close();
        inputStream.close();
        FileSystemResource fsr = new FileSystemResource(tempFile);
        String url = properties.getBaseUrl()+ TestCaseAIGeneratorConstant.targetUpload;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String,Object> body = new LinkedMultiValueMap<>();
        body.add("file",fsr);
        body.add("binary_path",binaryPath);
        HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(body,headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, new HashMap<>());
        return exchange.getBody();
    }

    @Override
    public String taskCreate(TestCaseAITaskCreateReq req) {
        if (StringUtils.hasLength(req.getBinaryId()) &&StringUtils.hasLength(req.getCaseFormat())) {
            return "";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("binary_id",req.getBinaryId());
        jsonObject.put("count",req.getCount());
        jsonObject.put("custom_prompt",req.getCustomPrompt());
        jsonObject.put("case_format",req.getCaseFormat());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(jsonObject),headers);
        String url = properties.getBaseUrl() + TestCaseAIGeneratorConstant.taskCreate;
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, new HashMap<>());
        return exchange.getBody();
    }

    @Override
    public String getTaskStatus(String taskId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("{}",headers);
        String url = properties.getBaseUrl() + TestCaseAIGeneratorConstant.getTaskStatus+taskId;
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new HashMap<>());
        return exchange.getBody();
    }

    @Override
    public String getTaskLog(String taskId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("{}",headers);
        String url = properties.getBaseUrl() + TestCaseAIGeneratorConstant.getTaskLog+taskId;
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new HashMap<>());
        return exchange.getBody();
    }

    @Override
    public void downloadTestCase(String taskId, OutputStream outputStream) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("{}",headers);
        String url = properties.getBaseUrl() + TestCaseAIGeneratorConstant.downloadTestCase+taskId;
        ResponseEntity<byte[]> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class, new HashMap<>());
        outputStream.write(exchange.getBody());
    }

}
