package com.yfd.platform.system.controller;

import cn.hutool.core.util.NumberUtil;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.utils.FtpClient;
import com.yfd.platform.utils.TLSFTPUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;

/**
 * <p>
 * 消息通知 前端控制器
 * </p>
 *
 * @since 2023-03-19
 */
@RestController
@RequestMapping("/ftp/client")
@Api(tags = "FTP客户端服务")
@Slf4j
public class FtpclientController {
    @Resource
    private FtpClient ftpClient;
    @Resource
    private TLSFTPUtils tlsftpUtils;

    @PostMapping("/uploadfile")
    @ApiOperation("测试上传文件")
    public ResponseResult uploadfile(String fileName, String remoteFileName) throws Exception {
//        ftpClient.uploadFileNew(fileName, remoteFileName);
        ftpClient.uploadFile(fileName, remoteFileName);
        return ResponseResult.success();
    }

    @PostMapping("/uploadfileSystem")
    @ApiOperation("服务器文件上传")
    public ResponseResult uploadfileSystem(String ip, String port, String name, String password, String fileName, String remoteFileName, String status) throws Exception {
        if (tlsftpUtils.isConnected()) {
            log.info("ftp连接未关闭");
            try {
                Thread.sleep(10000);
                if (tlsftpUtils.isConnected()) {
                    log.info("ftp连接等待10秒后仍未关闭");
                    tlsftpUtils.disconnect();
                }
            } catch (InterruptedException e) {
                log.error("ftp连接未关闭");
                log.error(e.getMessage());;
            }
        }
        tlsftpUtils.connect(ip, name, password,NumberUtil.parseInt(port));
        try {
            tlsftpUtils.upload(remoteFileName, new File(fileName));
            tlsftpUtils.disconnect();
        } catch (Exception e) {
            tlsftpUtils.disconnect();
        }
//        ftpClient.uploadfileSystem(ip, port, name, password, fileName, remoteFileName);
        return ResponseResult.success();
    }


    @PostMapping("/downloadFileSystem")
    @ApiOperation("测试服务器下载")
    public ResponseResult downloadFileSystem(String ip, String port, String name, String password, String localDir, String remoteFileName, String status) throws Exception {
        if (tlsftpUtils.isConnected()) {
            log.info("ftp连接未关闭");
            try {
                Thread.sleep(10000);
                if (tlsftpUtils.isConnected()) {
                    log.info("ftp连接等待10秒后仍未关闭");
                    tlsftpUtils.disconnect();
                }
            } catch (InterruptedException e) {
                log.error("ftp连接未关闭");
                log.error(e.getMessage());;
            }
        }
        tlsftpUtils.connect(ip, name, password, NumberUtil.parseInt(port));
        try {
            tlsftpUtils.download(localDir, remoteFileName);
            tlsftpUtils.disconnect();
        } catch (Exception e) {
            tlsftpUtils.disconnect();
        }
        //        ftpClient.uploadfileSystem(ip, port, name, password, fileName, remoteFileName);
        return ResponseResult.success();
    }

    @PostMapping("/downloadFile")
    @ApiOperation("测试下在文件")
    public ResponseResult downloadFile(String fileName, String remoteFileName) throws Exception {
        ftpClient.downloadFile(fileName, remoteFileName);
        return ResponseResult.success();
    }

}
