package com.example.fuzzControll.service.impl;

import com.example.fuzzControll.conf.KittyProperties;
import com.example.fuzzControll.constents.CmdConstent;
import com.example.fuzzControll.exception.MysqlException;
import com.example.fuzzControll.mapper.KittyMapper;
import com.example.fuzzControll.pojo.vo.KittyDataParams;
import com.example.fuzzControll.pojo.vo.KittyResult;
import com.example.fuzzControll.pojo.vo.KittyPackageFile;
import com.example.fuzzControll.service.KittyFuzzPersistenceService;
import com.example.fuzzControll.tools.CmdTools;
import com.example.fuzzControll.tools.FileTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("KittyFuzzPersistenceService")
@Slf4j
public class KittyFuzzPersistenceServiceImpl implements KittyFuzzPersistenceService {
    @Autowired
    KittyMapper kittyMapper;
    FileTools fileTools = new FileTools();
    CmdTools cmdTools = new CmdTools();
    @Autowired
    KittyProperties kittyProperties;

    @Override
    public List<KittyResult> getKittyResults() {
        return kittyMapper.getKittyResults();
    }

    @Override
    public int KittyPackagesBackup(int missionId) {
        List<String> files = cmdTools.runCmd(CmdConstent.GET_FILE_NAME + kittyProperties.getLogOutPath(), "getKittyLogFiles");
        if (files.size() > 3) {
            log.error("kittyLogs backup failed!");
        }
        System.out.println("1");
        List<String> logFiles = files.stream().filter(file -> file.contains("pcap")).collect(Collectors.toList());
        logFiles.forEach(System.out::println);
        //todo 需要传入任务id
        KittyPackageFile kittyPackageFile = new KittyPackageFile(1, logFiles.get(0), logFiles.get(1),
                fileTools.fileReadAndTranstoBytes(kittyProperties.getLogOutPath(), logFiles.get(0)),
                fileTools.fileReadAndTranstoBytes(kittyProperties.getLogOutPath(), logFiles.get(1)));
        System.out.println(kittyPackageFile);
        System.out.println("2");

        return kittyMapper.kittyPackagesBackup(kittyPackageFile);
    }

    @Override
    public int kittyResultsBackup(KittyResult kittyResult) {
        return kittyMapper.kittyResultsBackup(kittyResult);
    }

    @Override
    public void loadKittyLogFileInLocal(KittyDataParams kittyDataParams) {
        KittyPackageFile kittyPackageFile = null;
        try {
            kittyPackageFile = kittyMapper.getKittyPackageFileById(kittyDataParams.getMissionId());
        } catch (MysqlException e) {
            e.printStackTrace();
            throw new MysqlException("KittyMapper.getKittyPackageFileById Error!");
        }
        fileTools.loadFileInLocal(kittyDataParams.getFilPath(), kittyPackageFile.getKittyRecvFileName(),kittyPackageFile.getKittyRecvFile());
        fileTools.loadFileInLocal(kittyDataParams.getFilPath(), kittyPackageFile.getKittySendFileName(),kittyPackageFile.getKittySendFile());
    }
}