"""
使用示例 - 演示如何在Python代码中使用各个模块
"""
from bilibili_dl.auth import BilibiliAuth
from bilibili_dl.parser import BilibiliParser
from bilibili_dl.downloader import BilibiliDownloader


def example_login():
    """示例: 扫码登录"""
    print("=== 扫码登录示例 ===\n")
    auth = BilibiliAuth()
    
    # 执行登录流程
    success = auth.login()
    
    if success:
        print("\n登录成功!")
    else:
        print("\n登录失败")


def example_check_status():
    """示例: 检查登录状态"""
    print("=== 检查登录状态示例 ===\n")
    auth = BilibiliAuth()
    
    # 加载保存的登录信息
    credentials = auth.load_credentials()
    
    if credentials:
        user_info = credentials['user_info']
        print(f"已登录用户: {user_info['username']}")
        print(f"会员状态: VIP Type {user_info['vip_type']}")
    else:
        print("未登录")


def example_parse_video():
    """示例: 解析视频信息"""
    print("=== 解析视频信息示例 ===\n")
    
    # 加载登录信息(可选)
    auth = BilibiliAuth()
    credentials = auth.load_credentials()
    cookies = credentials['cookies'] if credentials else None
    
    # 创建解析器
    parser = BilibiliParser(cookies)
    
    # 从URL提取BV号
    url = "https://www.bilibili.com/video/BV1ms4y1Y76i"
    bvid = parser.extract_bvid(url)
    print(f"BV号: {bvid}")
    
    # 获取视频基本信息
    video_info = parser.get_video_info(bvid)
    print(f"\n标题: {video_info['title']}")
    print(f"UP主: {video_info['owner']['name']}")
    print(f"时长: {video_info['duration']}秒")
    
    # 获取可用清晰度
    qualities = parser.get_available_qualities(bvid, video_info['cid'])
    print(f"\n可用清晰度:")
    for q in qualities:
        print(f"  - {q['name']}")


def example_download_video():
    """示例: 下载视频"""
    print("=== 下载视频示例 ===\n")
    
    # 加载登录信息(可选)
    auth = BilibiliAuth()
    credentials = auth.load_credentials()
    cookies = credentials['cookies'] if credentials else None
    
    # 创建解析器和下载器
    parser = BilibiliParser(cookies)
    downloader = BilibiliDownloader(output_dir="downloads", cookies=cookies)
    
    # 解析视频
    bvid = "BV1ms4y1Y76i"
    video_info = parser.get_video_info(bvid)
    
    # 获取视频流(1080P)
    stream_info = parser.get_video_stream(bvid, video_info['cid'], quality=80)
    selected_stream = parser.select_stream(stream_info, target_quality=80)
    
    # 下载视频
    success = downloader.download_video(
        video_url=selected_stream['video_url'],
        audio_url=selected_stream['audio_url'],
        title=video_info['title'],
        bvid=bvid,
        quality_name=selected_stream['quality_name'],
        merge_needed=selected_stream['merge_needed']
    )
    
    if success:
        print("\n下载成功!")
        
        # 可选: 下载封面
        downloader.download_cover(video_info['pic'], bvid)


def example_logout():
    """示例: 退出登录"""
    print("=== 退出登录示例 ===\n")
    auth = BilibiliAuth()
    auth.clear_credentials()


if __name__ == '__main__':
    # 运行示例
    # example_login()
    # example_check_status()
    # example_parse_video()
    # example_download_video()
    # example_logout()
    
    print("请取消注释相应的示例函数来运行")
