#ifndef CURLFTP_H
#define CURLFTP_H

#include "curl/curl.h"
#include <string>
#include <vector>
#include <mutex>

#include "CurlFtpInfo.h"


/**
 * 使用CURL进行FTP操作,这个类有两种方式,一种是直接使用静态方法,另一种是创建实例
 * 使用静态函数可以进行一些简单的操作,但是如果需要进行多次操作,最好创建实例
 *
 *     1.默认超时时间是10s
 *     2.默认端口是21
 *     3.设置好IP和端口号后,后续输入的文件路径都是想对你路径,无需添加IP和端口号
 * 
 */


class CurlFtp
{
public:
    CurlFtp();
    ~CurlFtp();
    /* 列出FTP文件夹 */
    static std::string listDir(const std::string &ftpUrl, const std::string &username, const std::string &password);
    /* 列出文件夹中的所有文件 */
    static bool listFiles(const std::string &ftpUrl, const std::string &username, const std::string &password, std::vector<std::string>& fileList);
    /* 创建文件夹 */
    static bool createDir(const std::string &ftpUrl, const std::string &username, const std::string &password, const std::string &dirName);

    /*********************** 成员函数 *********************/
    /* 设置IP和端口 */
    bool setFtpIPAndPort(const std::string& IP, const int port);
    /* 设置用户名和密码 */
    bool setFtpUsernameAndPassword(const std::string& username, const std::string& password);
    /* 列出文件列表 */
    bool getFileList(std::string dir, std::vector<std::string>& fileList);
    /* 获取文件夹列表 */
    bool getDirList(std::string dir, std::vector<std::string>& dirList);
    /* 判断文件夹是否存在 */
    bool isDirExist(const std::string& dir);
    /* 创建FTP文件夹 */
    bool createDirectory(const std::string& ftpDir);
    /* 创建FTP文件夹,递归创建 */
    bool createDirectories(const std::string& ftpDir);

    /* 下载文件 */
    bool downloadFile(const std::string& remoteFile, const std::string& localFile);
    /* 上传文件 */
    bool uploadFile(const std::string& localFile, const std::string& remoteFile);

private:
    /* 列出所有内容 */
    bool listAll(CURL* curl, std::string dir, std::vector<CF_FileInfo>& fileInfoList);
    /* 检查FTP文件夹路径是否合规,不合规就修改 */
    std::string checkDirPath(const std::string& dir);
    /* 检查FTP文件路径是否合规 */
    std::string checkFilePath(const std::string& file);
    /* 检查本地文件夹是否存在,不存在则创建 */
    bool checkLocalDir(const std::string& localDir);

private:
    std::mutex m_mutexCurl;                 /* curl互斥锁 */
    std::string m_currentDir;               /* 当前目录 */

    std::string m_IP;                       /* IP */
    int m_port = 21;                        /* 端口 */
    std::string m_ftpUrl;                   /* ftpUrl */

    std::string m_username;
    std::string m_password;
};



#endif /* CURLFTP_H */