from typing import Sequence

from clarabot_nano.common.filesystem.operation.base import AbstractIOQueueOperation


class AbstractWriteOperation(AbstractIOQueueOperation):
    def __init__(self, account_id: int, path: str, final_path: str, data: bytes):
        AbstractIOQueueOperation.__init__(self, account_id)
        self.path = path
        self.final_path = final_path
        self.data = data

    def paths(self) -> Sequence[str]:
        return (self.path, self.final_path)

    def execute(self):
        raise NotImplementedError()


class WriteTruncateOperation(AbstractWriteOperation):
    OPERATION_KEY = "WRT"

    def execute(self):
        with open(self.path, "wb") as f:
            f.write(self.data)


class SeekWriteOperation(AbstractWriteOperation):
    OPERATION_KEY = "SWR"

    def __init__(self, account_id: int, path: str, final_path: str, data: bytes, at: int):
        AbstractWriteOperation.__init__(self, account_id, path, final_path, data)
        self.at = at

    def execute(self):
        with open(self.path, "r+b") as f:
            f.seek(self.at)
            f.write(self.data)
