from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel


class GachaPoolClient(BaseModel):
    gachaPoolId: str  # 卡池ID
    gachaIndex: int  # 卡池索引
    openTime: int  # 开始时间戳
    endTime: int  # 结束时间戳
    gachaPoolName: str  # 卡池名称
    gachaPoolSummary: str  # 卡池概述
    gachaPoolDetail: Optional[str] = None  # 卡池详细信息
    guarantee5Avail: int  # 保证5星次数
    guarantee5Count: int  # 保证5星次数计数
    LMTGSID: Optional[str] = None  # 可选字段
    CDPrimColor: Optional[str] = None  # 可选字段
    CDSecColor: Optional[str] = None  # 可选字段
    freeBackColor: Optional[str] = None  # 可选字段
    gachaRuleType: str  # 卡池规则类型
    dynMeta: Optional[dict] = None  # 动态元数据
    linkageRuleId: Optional[str] = None  # 可选字段
    linkageParam: Optional[dict] = None  # 可选字段
    limitParam: Optional[dict] = None  # 可选字段

    def get_start_time(self) -> datetime:
        """返回卡池开始时间"""
        return datetime.fromtimestamp(self.openTime)

    def get_end_time(self) -> datetime:
        """返回卡池结束时间"""
        return datetime.fromtimestamp(self.endTime)

    def is_active(self, current_timestamp: int) -> bool:
        """判断卡池是否正在进行"""
        return self.openTime <= current_timestamp < self.endTime


class limitTenGacha(BaseModel):
    itemId: str
    endTime: int


class linkageTenGacha(BaseModel):
    itemId: str
    endTime: int


class normalGacha(BaseModel):
    itemId: str
    endTime: int


class GachaData(BaseModel):
    gachaPoolClient: List[GachaPoolClient]
    limitTenGachaItem: List[limitTenGacha]
    linkageTenGachaItem: List[linkageTenGacha]
    normalGachaItem: List[normalGacha]
    recruitDetail: str


# 示例数据加载函数
def 加载json(file_path: str) -> GachaData:
    import json

    with open(file_path, "r", encoding="utf-8") as f:
        return GachaData.parse_obj(json.load(f))


# 示例如何使用
if __name__ == "__main__":
    # 假设 gacha_data 是加载的 JSON 数据
    gacha_data = 加载json("ArknightsGameData\zh_CN\gamedata\excel\gacha_table.json")

    current_timestamp = int(datetime.now().timestamp())  # 当前时间戳

    # 遍历卡池并输出信息
    for pool in gacha_data.gachaPoolClient:
        print("卡池名称:", pool.gachaPoolName)
        print("卡池类型:", pool.gachaRuleType)
        print("卡池开始时间:", pool.get_start_time())
        print("卡池结束时间:", pool.get_end_time())
        print("卡池是否正在进行:", pool.is_active(current_timestamp))
        print("卡池详细信息:", pool.gachaPoolDetail)
        print()
