package org.matrix.socket;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.database.entity.TestCase;
import org.matrix.util.SpringUtils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

import static org.matrix.socket.SocketHandler.*;
import static org.matrix.socket.SocketPool.add;

/**
 * @author zhoujian
 * 自定义封装的连接的客户端
 */
@Slf4j
@Data
public class ClientSocket implements Runnable{

    private Socket socket;
    private DataInputStream inputStream;
    private DataOutputStream outputStream;
    private Long key;
    private String message;
    private CaseActuator caseActuator;

    ClientSocket(){
       this.caseActuator =  SpringUtils.getBean("caseActuator");
    }

    @Override
    public void run() {
        this.key = Thread.currentThread().getId();
        add(this);
        System.out.println("-----key:"+key);
        while (true){
            System.out.println("1111111");
            byte[] bytes = new byte[1024];
            try {
                this.getInputStream().read(bytes);
                System.out.println(new String(bytes, StandardCharsets.UTF_8));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        System.out.println("意外停止了");




        //接受socket消息，根据前端提交的消息来判断，是否应该执行下一步
//        while (true){
//            String message = onMessage(this);
//            System.out.println("接受到了客户端传来的消息");
//            System.out.println(message);
//            String json = "{\n" +
//                    "    \"url\":\"http://127.0.0.1:8080/test/sendMessage\",\n" +
//                    "    \"method\":\"GET\",\n" +
//                    "    \"requestType\":\"QUERY\",\n" +
//                    "    \"headers\":[\n" +
//                    "        {\n" +
//                    "            \"name\":\"cookie\",\n" +
//                    "            \"value\":\"123456\"\n" +
//                    "        }\n" +
//                    "    ],\n" +
//                    "    \"requestBodies\":[\n" +
//                    "        {\n" +
//                    "            \"key\":\"tableName\",\n" +
//                    "            \"type\":\"TEXT\",\n" +
//                    "            \"value\":\"张三\"\n" +
//                    "        }\n" +
//                    "    ]\n" +
//                    "}";
//            System.out.println(json);
//            TestCase testCase = new TestCase();
//            testCase.setName("name");
//            testCase.setType(1);
//            testCase.setDetail(json);
//            testCase.setAbnormalCheckpoint(1);
//            testCase.setContainCheckpoint("张三,李四");
//            testCase.setNoContainCheckpoint("张三,李四");
//            testCase.setJsonpathCheckpoint("contains({$..category},'${componentName}[0]')");
//            TestCaseExecuteResult testCaseExecuteResult = caseActuator.executeTestCase(testCase,1L,1L);
//            System.out.println(testCaseExecuteResult);
//            if (!StringUtils.isEmpty(message)){
//                break;
//            }
//        }
//        //每5秒进行一次客户端连接，判断是否需要释放资源
//        while (true){
//            try {
//                TimeUnit.SECONDS.sleep(5);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            if (isSocketClosed(this)){
//                log.info("客户端已关闭,其Key值为：{}", this.getKey());
//                //关闭对应的服务端资源
//                close(this);
//                break;
//            }
//        }
    }
}