from __future__ import annotations

from datetime import datetime

from cli115.client.base import AccountClient as BaseAccountClient
from cli115.client.models import AccountInfo, Usage
from .base import BaseClient, Endpoint


class AccountClient(BaseAccountClient, BaseClient):

    def info(self) -> AccountInfo:
        resp = self._api.get(
            Endpoint.MY,
            params={"ct": "ajax", "ac": "nav"},
        )
        data = resp.json()["data"]
        expire_ts = data.get("expire")
        expire = datetime.fromtimestamp(expire_ts) if expire_ts else None
        return AccountInfo(
            user_name=data.get("user_name", ""),
            user_id=int(data.get("user_id", 0)),
            vip=bool(data.get("vip", 0)),
            expire=expire,
        )

    def usage(self) -> Usage:
        resp = self._api.get(
            Endpoint.WEBAPI + "/files/index_info",
            params={"count_space_nums": 0},
        )
        space = resp.json()["data"]["space_info"]
        return Usage(
            total=int(space.get("all_total", {}).get("size", 0)),
            used=int(space.get("all_use", {}).get("size", 0)),
            remaining=int(space.get("all_remain", {}).get("size", 0)),
        )
