package com.example.fuzzControll.controller.dataController;

import com.example.fuzzControll.domain.bo.AflnetDataParams;
import com.example.fuzzControll.domain.vo.AjaxResult;
import com.example.fuzzControll.service.AflnetPersistenceService;
import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.system.SystemRunningParams;
import com.example.fuzzControll.tools.test.TestCmdTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/aflnet")
@Slf4j
public class AflnetDataController {
    @Autowired
    AflnetPersistenceService aflnetPersistenceService;
    @Autowired
    TestService testService;
    TestCmdTools testCmdTools = new TestCmdTools();

    /**
     * 读取数据库文件至指定目录
     */
    @RequestMapping(value = "/loadFile", method = RequestMethod.GET)
    public AjaxResult AflnetResultSelect(@RequestBody AflnetDataParams aflnetDataParams) {
        try {
            aflnetPersistenceService.loadInFile(aflnetDataParams.getMissionId(), aflnetDataParams.getFilPath());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("File load failed!");
        }
        return AjaxResult.success("File loaded successfully!");
    }
    /**
     * 异常重放
     */
    @RequestMapping(value = "/replay", method = RequestMethod.GET)
    public AjaxResult replay(@RequestParam String targetServer,@RequestParam String targetServerCodePath,
                             @RequestParam String targetServerPort,@RequestParam String protocol,
                             @RequestParam String pathToCrash) {
        try {
            testService.replay(targetServer,targetServerCodePath,targetServerPort,protocol,pathToCrash);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Replay failed!");
        }
        return AjaxResult.success("Replay successfully!");
    }
    /**
     * 异常分析
     */
    @RequestMapping(value = "/analyse", method = RequestMethod.GET)
    public AjaxResult analyse(@RequestParam String codePath, @RequestParam String programName) {
        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    testService.analyse(codePath, programName);
                }
            }).start();
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Analyse failed!");
        }
        return AjaxResult.success("Analyse success!");
    }

    /**
     * 读取异常定位分析结果
     */
    @RequestMapping(value = "/getreplayresult", method = RequestMethod.GET)
    public AjaxResult readReplayResult() {
        List<String> result = new ArrayList<String>();
        try {
            result = testService.getReplayResult();
        } catch (Exception e) {
            return AjaxResult.error("Get replayResult failed!");
        }
        return AjaxResult.success(result);
    }
}