import '../models/vocabulary_book_model.dart';
import '../models/vocabulary_book_category.dart';
import '../models/word_model.dart';

/// 词书数据工厂类
class VocabularyBookFactory {
  /// 创建所有系统词书
  static List<VocabularyBook> createAllSystemBooks() {
    final List<VocabularyBook> books = [];
    
    // 学段基础词汇
    books.addAll(_createAcademicStageBooks());
    
    // 国内应试类词汇
    books.addAll(_createDomesticTestBooks());
    
    // 出国考试类词汇
    books.addAll(_createInternationalTestBooks());
    
    // 职业与专业类词汇
    books.addAll(_createProfessionalBooks());
    
    // 功能型词库
    books.addAll(_createFunctionalBooks());
    
    return books;
  }

  /// 创建学段基础词汇书
  static List<VocabularyBook> _createAcademicStageBooks() {
    final now = DateTime.now();
    
    return [
      VocabularyBook(
        id: 'academic_primary_core',
        name: '小学英语核心词汇',
        description: '小学阶段必备的1000-1500个核心词汇，涵盖基础场景和日常用语',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.beginner,
        coverImageUrl: 'assets/images/vocabulary_books/primary_school.png',
        totalWords: 1200,
        isPublic: true,
        tags: ['小学', '基础', '核心词汇', '日常用语'],
        category: '学段基础词汇',
        mainCategory: VocabularyBookMainCategory.academicStage,
        subCategory: AcademicStageCategory.primarySchool.name,
        targetLevels: ['小学1-6年级'],
        estimatedDays: 60,
        dailyWordCount: 20,
        downloadCount: 15420,
        rating: 4.8,
        reviewCount: 2341,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'academic_junior_high',
        name: '初中英语词汇',
        description: '初中阶段1500-2500词，结合教材要求，为中考做准备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.elementary,
        coverImageUrl: 'assets/images/vocabulary_books/junior_high.png',
        totalWords: 2000,
        isPublic: true,
        tags: ['初中', '中考', '教材配套'],
        category: '学段基础词汇',
        mainCategory: VocabularyBookMainCategory.academicStage,
        subCategory: AcademicStageCategory.juniorHigh.name,
        targetLevels: ['初中1-3年级'],
        estimatedDays: 100,
        dailyWordCount: 20,
        downloadCount: 23156,
        rating: 4.7,
        reviewCount: 3892,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'academic_senior_high',
        name: '高中英语词汇',
        description: '高中阶段2500-3500词，涵盖课标与高考高频词汇',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/senior_high.png',
        totalWords: 3200,
        isPublic: true,
        tags: ['高中', '高考', '课标词汇', '高频词'],
        category: '学段基础词汇',
        mainCategory: VocabularyBookMainCategory.academicStage,
        subCategory: AcademicStageCategory.seniorHigh.name,
        targetLevels: ['高中1-3年级'],
        estimatedDays: 160,
        dailyWordCount: 20,
        downloadCount: 34782,
        rating: 4.9,
        reviewCount: 5673,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'academic_university',
        name: '大学英语教材词汇',
        description: '大学英语精读/泛读配套词汇，提升学术英语水平',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/university.png',
        totalWords: 4500,
        isPublic: true,
        tags: ['大学', '学术英语', '精读', '泛读'],
        category: '学段基础词汇',
        mainCategory: VocabularyBookMainCategory.academicStage,
        subCategory: AcademicStageCategory.university.name,
        targetLevels: ['大学1-4年级'],
        estimatedDays: 225,
        dailyWordCount: 20,
        downloadCount: 18934,
        rating: 4.6,
        reviewCount: 2847,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }

  /// 创建国内应试类词汇书
  static List<VocabularyBook> _createDomesticTestBooks() {
    final now = DateTime.now();
    
    return [
      VocabularyBook(
        id: 'domestic_cet4',
        name: '大学英语四级词汇',
        description: 'CET-4核心词汇，助力四级考试高分通过',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/cet4.png',
        totalWords: 4500,
        isPublic: true,
        tags: ['四级', 'CET-4', '考试必备'],
        category: '国内应试类词汇',
        mainCategory: VocabularyBookMainCategory.domesticTest,
        subCategory: DomesticTestCategory.cet4.name,
        targetLevels: ['大学生'],
        estimatedDays: 90,
        dailyWordCount: 50,
        downloadCount: 89234,
        rating: 4.8,
        reviewCount: 12456,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'domestic_cet6',
        name: '大学英语六级词汇',
        description: 'CET-6核心词汇，突破六级考试难关',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/cet6.png',
        totalWords: 5500,
        isPublic: true,
        tags: ['六级', 'CET-6', '高级词汇'],
        category: '国内应试类词汇',
        mainCategory: VocabularyBookMainCategory.domesticTest,
        subCategory: DomesticTestCategory.cet6.name,
        targetLevels: ['大学生'],
        estimatedDays: 110,
        dailyWordCount: 50,
        downloadCount: 67891,
        rating: 4.7,
        reviewCount: 9823,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'domestic_postgraduate',
        name: '考研英语核心词汇',
        description: '考研英语必备词汇，涵盖英语一和英语二',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/postgraduate.png',
        totalWords: 5500,
        isPublic: true,
        tags: ['考研', '研究生', '英语一', '英语二'],
        category: '国内应试类词汇',
        mainCategory: VocabularyBookMainCategory.domesticTest,
        subCategory: DomesticTestCategory.postgraduate.name,
        targetLevels: ['本科生', '考研学生'],
        estimatedDays: 120,
        dailyWordCount: 45,
        downloadCount: 45672,
        rating: 4.9,
        reviewCount: 7234,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'domestic_tem4',
        name: '英语专业四级词汇',
        description: 'TEM-4专业词汇，英语专业学生必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/tem4.png',
        totalWords: 6000,
        isPublic: true,
        tags: ['专四', 'TEM-4', '英语专业'],
        category: '国内应试类词汇',
        mainCategory: VocabularyBookMainCategory.domesticTest,
        subCategory: DomesticTestCategory.tem4.name,
        targetLevels: ['英语专业学生'],
        estimatedDays: 100,
        dailyWordCount: 60,
        downloadCount: 23456,
        rating: 4.6,
        reviewCount: 3421,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'domestic_tem8',
        name: '英语专业八级词汇',
        description: 'TEM-8高级词汇，英语专业最高水平认证',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.expert,
        coverImageUrl: 'assets/images/vocabulary_books/tem8.png',
        totalWords: 8000,
        isPublic: true,
        tags: ['专八', 'TEM-8', '高级词汇'],
        category: '国内应试类词汇',
        mainCategory: VocabularyBookMainCategory.domesticTest,
        subCategory: DomesticTestCategory.tem8.name,
        targetLevels: ['英语专业学生'],
        estimatedDays: 160,
        dailyWordCount: 50,
        downloadCount: 18923,
        rating: 4.8,
        reviewCount: 2567,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }

  /// 创建出国考试类词汇书
  static List<VocabularyBook> _createInternationalTestBooks() {
    final now = DateTime.now();
    
    return [
      VocabularyBook(
        id: 'international_ielts_academic',
        name: '雅思学术类词汇',
        description: 'IELTS Academic核心词汇，助力雅思高分',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/ielts_academic.png',
        totalWords: 6500,
        isPublic: true,
        tags: ['雅思', 'IELTS', '学术类', '出国留学'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.ieltsAcademic.name,
        targetLevels: ['出国留学生'],
        estimatedDays: 130,
        dailyWordCount: 50,
        downloadCount: 56789,
        rating: 4.8,
        reviewCount: 8934,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'international_ielts_general',
        name: '雅思培训类词汇',
        description: 'IELTS General Training词汇，移民必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/ielts_general.png',
        totalWords: 5500,
        isPublic: true,
        tags: ['雅思', 'IELTS', '培训类', '移民'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.ieltsGeneral.name,
        targetLevels: ['移民申请者'],
        estimatedDays: 110,
        dailyWordCount: 50,
        downloadCount: 34567,
        rating: 4.7,
        reviewCount: 5432,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'international_toefl',
        name: '托福词汇',
        description: 'TOEFL iBT核心词汇，美国留学必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/toefl.png',
        totalWords: 7000,
        isPublic: true,
        tags: ['托福', 'TOEFL', 'iBT', '美国留学'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.toeflIbt.name,
        targetLevels: ['美国留学生'],
        estimatedDays: 140,
        dailyWordCount: 50,
        downloadCount: 67234,
        rating: 4.9,
        reviewCount: 9876,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'international_gre',
        name: 'GRE词汇',
        description: 'GRE核心词汇，研究生申请必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.expert,
        coverImageUrl: 'assets/images/vocabulary_books/gre.png',
        totalWords: 8500,
        isPublic: true,
        tags: ['GRE', '研究生申请', '学术词汇'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.gre.name,
        targetLevels: ['研究生申请者'],
        estimatedDays: 170,
        dailyWordCount: 50,
        downloadCount: 43210,
        rating: 4.7,
        reviewCount: 6543,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'international_gmat',
        name: 'GMAT词汇',
        description: 'GMAT商科词汇，MBA申请必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.expert,
        coverImageUrl: 'assets/images/vocabulary_books/gmat.png',
        totalWords: 7500,
        isPublic: true,
        tags: ['GMAT', 'MBA', '商科', '管理类'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.gmat.name,
        targetLevels: ['MBA申请者'],
        estimatedDays: 150,
        dailyWordCount: 50,
        downloadCount: 32109,
        rating: 4.6,
        reviewCount: 4321,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'international_sat',
        name: 'SAT词汇',
        description: 'SAT核心词汇，美本申请必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/sat.png',
        totalWords: 6000,
        isPublic: true,
        tags: ['SAT', '美本申请', '高中生'],
        category: '出国考试类词汇',
        mainCategory: VocabularyBookMainCategory.internationalTest,
        subCategory: InternationalTestCategory.sat.name,
        targetLevels: ['高中生', '美本申请者'],
        estimatedDays: 120,
        dailyWordCount: 50,
        downloadCount: 28765,
        rating: 4.8,
        reviewCount: 3987,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }

  /// 创建职业与专业类词汇书
  static List<VocabularyBook> _createProfessionalBooks() {
    final now = DateTime.now();
    
    return [
      VocabularyBook(
        id: 'professional_business',
        name: '商务英语词汇',
        description: '商务场景核心词汇，职场沟通必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/business.png',
        totalWords: 3500,
        isPublic: true,
        tags: ['商务英语', '职场', '商务沟通'],
        category: '职业与专业类词汇',
        mainCategory: VocabularyBookMainCategory.professional,
        subCategory: ProfessionalCategory.businessEnglish.name,
        targetLevels: ['职场人士'],
        estimatedDays: 70,
        dailyWordCount: 50,
        downloadCount: 45678,
        rating: 4.7,
        reviewCount: 6789,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'professional_bec_higher',
        name: 'BEC高级词汇',
        description: 'BEC Higher商务英语高级词汇',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/bec_higher.png',
        totalWords: 4500,
        isPublic: true,
        tags: ['BEC', '高级', '商务英语证书'],
        category: '职业与专业类词汇',
        mainCategory: VocabularyBookMainCategory.professional,
        subCategory: ProfessionalCategory.becHigher.name,
        targetLevels: ['商务人士'],
        estimatedDays: 90,
        dailyWordCount: 50,
        downloadCount: 23456,
        rating: 4.8,
        reviewCount: 3456,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'professional_medical',
        name: '医学英语词汇',
        description: '医学专业词汇，医护人员必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.expert,
        coverImageUrl: 'assets/images/vocabulary_books/medical.png',
        totalWords: 5500,
        isPublic: true,
        tags: ['医学', '医护', '专业词汇'],
        category: '职业与专业类词汇',
        mainCategory: VocabularyBookMainCategory.professional,
        subCategory: ProfessionalCategory.medical.name,
        targetLevels: ['医护人员', '医学生'],
        estimatedDays: 110,
        dailyWordCount: 50,
        downloadCount: 18765,
        rating: 4.9,
        reviewCount: 2345,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'professional_legal',
        name: '法律英语词汇',
        description: '法律专业词汇，法律从业者必备',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.expert,
        coverImageUrl: 'assets/images/vocabulary_books/legal.png',
        totalWords: 4800,
        isPublic: true,
        tags: ['法律', '法务', '专业词汇'],
        category: '职业与专业类词汇',
        mainCategory: VocabularyBookMainCategory.professional,
        subCategory: ProfessionalCategory.legal.name,
        targetLevels: ['法律从业者', '法学生'],
        estimatedDays: 96,
        dailyWordCount: 50,
        downloadCount: 15432,
        rating: 4.6,
        reviewCount: 1987,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'professional_it',
        name: 'IT英语词汇',
        description: '计算机科学与软件工程词汇',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/it.png',
        totalWords: 4200,
        isPublic: true,
        tags: ['IT', '计算机', '软件工程', '人工智能'],
        category: '职业与专业类词汇',
        mainCategory: VocabularyBookMainCategory.professional,
        subCategory: ProfessionalCategory.computerScience.name,
        targetLevels: ['程序员', 'IT从业者'],
        estimatedDays: 84,
        dailyWordCount: 50,
        downloadCount: 34567,
        rating: 4.8,
        reviewCount: 5432,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }

  /// 创建功能型词库
  static List<VocabularyBook> _createFunctionalBooks() {
    final now = DateTime.now();
    
    return [
      VocabularyBook(
        id: 'functional_roots_affixes',
        name: '词根词缀词汇',
        description: '通过词根词缀快速扩展词汇量，掌握记忆技巧',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/roots_affixes.png',
        totalWords: 3000,
        isPublic: true,
        tags: ['词根词缀', '记忆技巧', '词汇扩展'],
        category: '功能型词库',
        mainCategory: VocabularyBookMainCategory.functional,
        subCategory: FunctionalCategory.rootsAffixes.name,
        targetLevels: ['中级以上学习者'],
        estimatedDays: 60,
        dailyWordCount: 50,
        downloadCount: 67890,
        rating: 4.9,
        reviewCount: 8765,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'functional_synonyms_antonyms',
        name: '同义词反义词库',
        description: '丰富表达方式，提升写作和口语水平',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.intermediate,
        coverImageUrl: 'assets/images/vocabulary_books/synonyms.png',
        totalWords: 2500,
        isPublic: true,
        tags: ['同义词', '反义词', '写作', '口语'],
        category: '功能型词库',
        mainCategory: VocabularyBookMainCategory.functional,
        subCategory: FunctionalCategory.synonymsAntonyms.name,
        targetLevels: ['中级以上学习者'],
        estimatedDays: 50,
        dailyWordCount: 50,
        downloadCount: 45678,
        rating: 4.7,
        reviewCount: 6543,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'functional_academic_writing',
        name: '学术写作搭配库',
        description: '学术写作常用搭配，提升论文写作水平',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.advanced,
        coverImageUrl: 'assets/images/vocabulary_books/academic_writing.png',
        totalWords: 2000,
        isPublic: true,
        tags: ['学术写作', '搭配', '论文', 'Collocations'],
        category: '功能型词库',
        mainCategory: VocabularyBookMainCategory.functional,
        subCategory: FunctionalCategory.academicWritingCollocations.name,
        targetLevels: ['研究生', '学者'],
        estimatedDays: 40,
        dailyWordCount: 50,
        downloadCount: 23456,
        rating: 4.8,
        reviewCount: 3210,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'functional_daily_spoken',
        name: '日常口语搭配库',
        description: '日常口语常用搭配，提升口语表达自然度',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.elementary,
        coverImageUrl: 'assets/images/vocabulary_books/daily_spoken.png',
        totalWords: 1800,
        isPublic: true,
        tags: ['日常口语', '搭配', '口语表达'],
        category: '功能型词库',
        mainCategory: VocabularyBookMainCategory.functional,
        subCategory: FunctionalCategory.dailySpokenCollocations.name,
        targetLevels: ['初级以上学习者'],
        estimatedDays: 36,
        dailyWordCount: 50,
        downloadCount: 56789,
        rating: 4.6,
        reviewCount: 7654,
        createdAt: now,
        updatedAt: now,
      ),
      VocabularyBook(
        id: 'functional_daily_life',
        name: '日常生活英语',
        description: '旅游、点餐、购物、出行、租房等生活场景词汇',
        type: VocabularyBookType.system,
        difficulty: VocabularyBookDifficulty.elementary,
        coverImageUrl: 'assets/images/vocabulary_books/daily_life.png',
        totalWords: 2200,
        isPublic: true,
        tags: ['日常生活', '旅游', '购物', '出行', '租房'],
        category: '功能型词库',
        mainCategory: VocabularyBookMainCategory.functional,
        subCategory: FunctionalCategory.dailyLifeEnglish.name,
        targetLevels: ['生活英语学习者'],
        estimatedDays: 44,
        dailyWordCount: 50,
        downloadCount: 78901,
        rating: 4.8,
        reviewCount: 9876,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }

  /// 根据主分类获取词书列表
  static List<VocabularyBook> getBooksByMainCategory(VocabularyBookMainCategory category) {
    final allBooks = createAllSystemBooks();
    return allBooks.where((book) => book.mainCategory == category).toList();
  }

  /// 根据子分类获取词书列表
  static List<VocabularyBook> getBooksBySubCategory(String subCategory) {
    final allBooks = createAllSystemBooks();
    return allBooks.where((book) => book.subCategory == subCategory).toList();
  }

  /// 获取推荐词书（基于下载量和评分）
  static List<VocabularyBook> getRecommendedBooks({int limit = 10}) {
    final allBooks = createAllSystemBooks();
    allBooks.sort((a, b) {
      // 综合评分：下载量权重0.3，评分权重0.7
      final scoreA = (a.downloadCount / 100000) * 0.3 + a.rating * 0.7;
      final scoreB = (b.downloadCount / 100000) * 0.3 + b.rating * 0.7;
      return scoreB.compareTo(scoreA);
    });
    return allBooks.take(limit).toList();
  }

  /// 根据难度获取词书列表
  static List<VocabularyBook> getBooksByDifficulty(VocabularyBookDifficulty difficulty) {
    final allBooks = createAllSystemBooks();
    return allBooks.where((book) => book.difficulty == difficulty).toList();
  }

  /// 搜索词书
  static List<VocabularyBook> searchBooks(String query) {
    final allBooks = createAllSystemBooks();
    final lowerQuery = query.toLowerCase();
    
    return allBooks.where((book) {
      return book.name.toLowerCase().contains(lowerQuery) ||
             (book.description?.toLowerCase().contains(lowerQuery) ?? false) ||
             book.tags.any((tag) => tag.toLowerCase().contains(lowerQuery));
    }).toList();
  }
}