package com.youlai.boot.system.service.impl;

import com.youlai.boot.common.constant.SystemConstants;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.system.model.entity.DirectoryNode;
import com.youlai.boot.system.model.entity.DirectoryTree;
import com.youlai.boot.system.schedule.FTPUploadTask;
import com.youlai.boot.system.service.ConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

@Service
@RequiredArgsConstructor
@Slf4j
public class FTPService {


    private final RestTemplate restTemplate;

    private final ConfigService configService;


    public void uploadFile(String server, String localFilePath) {
        // 创建 FTP 上传任务
        if (isAliyunServer()) {
            return;
        }

        syncFolders(server);

        FTPUploadTask uploadTask = new FTPUploadTask(localFilePath);

        new Thread(uploadTask).start();

        log.info("ftp 文件 传输中");
    }

    private boolean syncFolders(String server) {

        String path = configService.getSystemConfig(SystemConstants.UPLOAD_PATH).toString();

        DirectoryNode directoryNode = DirectoryTree.getDirectoryNode(new File(path));

        Result result = restTemplate.postForObject("http://" + server + ":8087/prod-api/sync/folder", directoryNode, Result.class);

        assert result != null;

        if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
            log.info("sync folders success.{}", result.getMsg());
            return true;
        } else {
            log.error("sync folders failed.{}", result.getMsg());
            return false;
        }


    }


    boolean isAliyunServer() {
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                // 获取与该网络接口关联的所有IP地址
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();

                    // 检查是否是IPv4地址，并且不是环回地址（127.0.0.1）
                    if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
                        String ipAddress = inetAddress.getHostAddress();
                        if ("172.28.31.115".equals(ipAddress)) return true;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();

        }
        return false;


    }


}