package test.com.pjilisense.flxai.controller;

import cn.hutool.http.body.MultipartBody;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import test.com.pjilisense.flxai.TestUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class DigitalImgControllerTest {

    @BeforeEach
    public void setUpTestDataWithinTransaction() {
        // set up test data within the transaction
    }
    @AfterEach
    public void tearDownWithinTransaction() {
        // execute "tear down" logic within the transaction
    }

    @Test
    public void testuploadPicture() {
        String urls = TestUtils.WebconnectURL+"/api/robot/digitalimg/uploadPicture";
        String charset = TestUtils.UTF8;
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            File  file = new File("C:\\Users\\john\\Desktop\\30_200_200.jpg");
            try {
                URL url = new URL(urls);
                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                httpConn.setUseCaches(false);
                httpConn.setDoOutput(true); // Indicates that data will be sent
                httpConn.setDoInput(true); // Indicates that data will be received
                httpConn.setRequestMethod("POST"); // HTTP Request Method
                String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate any random value here
                httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

                OutputStream output = httpConn.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);

                // Send normal form data with name "file"
                writer.append("--" + boundary).append("\r\n");
                writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"").append("\r\n");
                writer.append("Content-Type: " + "application/octet-stream").append("\r\n");
                writer.append("\r\n").flush();
                writer.flush();

                // files data
                FileInputStream input = new FileInputStream(file);
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = input.read(buffer)) != -1) {
                    output.write(buffer, 0, bytesRead);
                }
                output.flush();
                input.close();

                // Send multipart form data terminator
                writer.append("\r\n").flush();
                writer.append("--" + boundary + "--\r\n").append("\r\n").flush();

                // Gets the response status code
                int status = httpConn.getResponseCode();
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                writer.close();
                output.close();
                httpConn.disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //{"code":0,"msg":"success","data":"uploads/202503/882d8b1d82de42308142f9a49b5716f8.jpg"}
    }
    @Test
    public void testgetPicture() {
        String url = TestUtils.WebconnectURL+"/api/robot/digitalimg/getPicture?id=1896743235380944897";
        TestUtils.doGetFile(url, "D:\\ceshi\\sysfile\\download\202503\\1896743235380944897.png");
    }
    @Test
    public void testGetAllAiTools() {
        String url = TestUtils.WebconnectURL+"/api/robot/digitalimg/getAllByType?imgType=1";
        String charset = TestUtils.UTF8;
        try {
            String body = TestUtils.doGet(url, charset);
            System.out.println(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //{"code":0,"msg":"success","data":[{"id":"1896741333490540546","name":"开源项目","imgPath":"uploads/202503/5c799cffa5864619b817063f77fe734d.png","imgType":"1"}]}
    }
    @Test
    public void testSave() {
        String url = TestUtils.WebconnectURL+"/api/robot/digitalimg";
        String charset = TestUtils.UTF8;
        try {
            Map<String, Object> params = new HashMap<String, Object>();
//            params.put("name","开源项目");
//            params.put("imgPath","uploads/202503/5c799cffa5864619b817063f77fe734d.png");
//            params.put("imgType","1");
//            params.put("name","开源项目");
//            params.put("imgPath","uploads/202503/d1f8398f9bb844079f774ef496347e12.png");
//            params.put("imgType","1");
            params.put("name","音色1");
            params.put("imgPath","uploads/202503/4eaee34079d74517ad6b9ea1e90e8308.jpg");
            params.put("imgType","4");
            String body = TestUtils.doPost(url, JSON.toJSONString(params), charset);
            System.out.println(body);
            //param ={"imgPath":"uploads/202503/d1f8398f9bb844079f774ef496347e12.png","name":"开源项目","imgType":"1"}
            //{"code":0,"msg":"success","data":null}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testupdate() {
        String url = TestUtils.WebconnectURL+"/api/robot/digitalimg";
        String charset = TestUtils.UTF8;
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id","1896741333490540546");
            params.put("name","开源项目2");
            params.put("imgPath","uploads/202503/5c799cffa5864619b817063f77fe734d.png");
            params.put("imgType","2");
            String body = TestUtils.doPut(url, JSON.toJSONString(params), charset);
            System.out.println(body);
            //param ={"imgPath":"uploads/202503/5c799cffa5864619b817063f77fe734d.png","name":"开源项目2","id":"1896741333490540546","imgType":"2"}
            //{"code":0,"msg":"success","data":null}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
