package com.fjhx.common.service.file.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjhx.common.service.file.FtpFileService;
import com.fjhx.common.utils.Utils;
import com.ruoyi.common.exception.ServiceException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FtpFileServiceImpl implements FtpFileService {

    public static final String fileServiceUrl = SpringUtil.getProperty("hx.fileServiceUrl");

    /**
     * 创建临时文件夹
     */
    @Override
    public JSONObject createTempFolder() {
        try {
            if (ObjectUtil.isEmpty(fileServiceUrl)) {
                throw new ServiceException("未配置文件服务控制程序!!!");
            }

            String tempFolderPath = String.format("/temp/%s", new Date().getTime());

            HttpPost httpPost = new HttpPost(fileServiceUrl + "/fileService/createFolder");
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("folderPath", tempFolderPath));
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            JSONObject responseJson = Utils.getJSON(httpPost);
            if (responseJson.getInteger("code") != 200) {
                throw new ServiceException(responseJson.getString("msg"));
            }
            return responseJson.getJSONObject("data");
        } catch (IOException e) {
            throw new ServiceException("操作失败：" + e.getMessage());
        }
    }

    /**
     * 将临时文件移动到永久文件夹
     *
     * @return
     */
    @Override
    public JSONObject moveFolder(String sourceFolderPath, String targetFolderPath) {
        targetFolderPath = String.format("/permanent/%s", targetFolderPath);

        try {
            if (ObjectUtil.isEmpty(fileServiceUrl)) {
                throw new ServiceException("未配置文件服务控制程序!!!");
            }
            HttpPost httpPost = new HttpPost(fileServiceUrl + "/fileService/moveTempFolderToPermanent");


            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("sourceFolderPath", sourceFolderPath));
            params.add(new BasicNameValuePair("targetFolderPath", targetFolderPath));
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            JSONObject responseJson = Utils.getJSON(httpPost);

            if (responseJson.getInteger("code") != 200) {
                throw new ServiceException(responseJson.getString("msg"));
            }
            return responseJson.getJSONObject("data");
        } catch (IOException e) {
            throw new ServiceException("操作失败：" + e.getMessage());
        }
    }

    @Override
    public List<String> getFileList(String folderPath) {
        try {
            HttpPost httpPost = new HttpPost(fileServiceUrl + "/fileService/getFileList");
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("folderPath", folderPath));
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            JSONObject responseJson = Utils.getJSON(httpPost);
            if (responseJson.getInteger("code") != 200) {
                throw new ServiceException(responseJson.getString("msg"));
            }
            return responseJson.getList("data", String.class);
        } catch (IOException e) {
            throw new ServiceException("操作失败：" + e.getMessage());
        }
    }

}
