"""天翼云盘适配器 v1.0.0"""

from ..base import BaseCloudDriveAdapter, FileInfo, TransferResult, VerifyResult
from ...errors import TransferError, TransferErrorCode
from .credential import Cloud189CredentialManager
from .transfer import Cloud189Transfer
from .cleanup import Cloud189Cleanup


class Cloud189Adapter(BaseCloudDriveAdapter):
    PLATFORM_NAME = "天翼云盘"
    PLATFORM_KEY = "cloud189"
    URL_PATTERNS = [r"cloud\.189\.cn/t/([A-Za-z0-9]+)"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._cred = Cloud189CredentialManager(self.config)
        self._transfer_engine = None
        self._cln = Cloud189Cleanup()

    def _setup_session(self):
        if self._cred:
            self._cred.login_if_needed(self.session)

    @property
    def _transfer(self):
        if self._transfer_engine is None:
            self._transfer_engine = Cloud189Transfer(
                self.session, self._cred, self.config, self.transfer_config)
        return self._transfer_engine

    def _get_share_detail(self, pwd_id, passcode=""):
        return self._transfer.get_share_info(pwd_id, passcode)

    def _save_files(self, pwd_id, detail, save_dir):
        return self._transfer.save_files(pwd_id, detail, save_dir)

    def _create_share(self, file_ids, title, password=""):
        return self._transfer.create_share(file_ids, title, password)

    def get_files(self, parent_fid="-11"):
        return self._transfer.list_files(parent_fid)

    def delete(self, file_ids):
        return self._cln.delete_files(self.session, self._cred, file_ids)
