import plac
import pysftp
from pathlib import Path
from dataclasses import dataclass
import warnings


@dataclass
class FtpConfig:
    server: str
    username: str
    password: str
    path: str


class FtpClient:
    commands = ['upload', 'download_dir']

    def __init__(self):
        self.base_dir = Path(__file__).parent
        # cfg = json.load(open(self.base_dir.joinpath('ftp_config.json'), 'r'))
        # self.ftp_cfg = FtpConfig(**cfg)
        # self.ftp_cfg = FtpConfig("ftp.global-cube.com", "p33781016-vm", "Gcbs12ma-vm2020!!", "/")
        self.ftp_cfg = FtpConfig("ftp.global-cube.com", "u1339416173", "dihob#ratuy5kub%", "/")
        warnings.filterwarnings('ignore')

    @plac.pos('filename', '', type=str)
    def upload(self, filename='CARLO.csv'):
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None

        with pysftp.Connection(self.ftp_cfg.server,
                               username=self.ftp_cfg.username,
                               password=self.ftp_cfg.password,
                               cnopts=cnopts) as ftp_conn:
            ftp_conn.cwd(self.ftp_cfg.path)
            ftp_conn.put(filename)
            # print(res)

    @plac.pos('path_from', '', type=str)
    @plac.pos('path_to', '', type=str)
    def download_dir(self, path_from='/server2019', path_to='./'):
        cnopts = pysftp.CnOpts()
        cnopts.hostkeys = None

        with pysftp.Connection(self.ftp_cfg.server,
                               username=self.ftp_cfg.username,
                               password=self.ftp_cfg.password, cnopts=cnopts) as ftp_conn:
            ftp_conn.cwd(path_from)
            files_list = ftp_conn.listdir()
            print(files_list)
            ftp_conn.get_d(path_from, path_to)


if __name__ == '__main__':
    # plac.Interpreter.call(ftp)
    FtpClient().download_dir()