import shutil
import stat
from pathlib import Path
from typing import Sequence

from clarabot_nano.server.fstx.filesystem.operation.base import AbstractIOQueueOperation


class CopyOperation(AbstractIOQueueOperation):
    OPERATION_KEY = "CPY"

    def __init__(self, account_id: int, src: str, dst: str):
        AbstractIOQueueOperation.__init__(self, account_id)
        self.src = src
        self.dst = dst

    def paths(self) -> Sequence[str]:
        return (self.src, self.dst)

    def execute(self):
        path = Path(self.src)
        s = path.lstat()
        if stat.S_ISDIR(s.st_mode):
            shutil.copytree(self.src, self.dst, ignore_dangling_symlinks=True)
        else:
            shutil.copy(self.src, self.dst)
