import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:recording_tool/features/competition_teams/model/model_competition_team.dart';

final competitionTeamsServerProvider = Provider<CompetitionTeamsServer>((ref) {
  return const CompetitionTeamsServer();
});

class CompetitionTeamsServer {
  const CompetitionTeamsServer();

  static const totalCount = 10;

  Future<CompetitionTeamPageResult> fetchPage({
    required int page,
    required int pageSize,
  }) async {
    final start = (page - 1) * pageSize;
    if (start >= totalCount) {
      return CompetitionTeamPageResult(
        items: const [],
        total: totalCount,
        page: page,
        pageSize: pageSize,
      );
    }

    final end = (start + pageSize).clamp(0, totalCount);
    final items = List.generate(
      end - start,
      (index) => _buildItem(start + index),
    );
    return CompetitionTeamPageResult(
      items: items,
      total: totalCount,
      page: page,
      pageSize: pageSize,
    );
  }

  CompetitionTeamListItem _buildItem(int index) {
    final number = index + 1;
    final group = index.isEven ? '小学组' : '初中组';
    final hour = 9 + index ~/ 2;
    return CompetitionTeamListItem(
      id: 'competition-$number',
      eventName: '全国青少年无人机大赛',
      itemName: index % 3 == 0 ? '空中足球赛' : '空中格斗赛',
      groupName: group,
      matchPlace: '场地 ${index % 4 + 1}',
      matchStartTime: '${hour.toString().padLeft(2, '0')}:00',
      matchEndTime: '${hour.toString().padLeft(2, '0')}:30',
      completed: index == totalCount - 1,
      matchups: [
        _buildMatchup(itemNumber: number, matchNumber: 1),
        _buildMatchup(itemNumber: number, matchNumber: 2),
      ],
    );
  }

  CompetitionMatchup _buildMatchup({
    required int itemNumber,
    required int matchNumber,
  }) {
    final matchupId = 'match-$itemNumber-$matchNumber';
    final teamAIndex = (itemNumber + matchNumber - 2) % _teamNames.length;
    final teamBIndex = (teamAIndex + 1) % _teamNames.length;
    return CompetitionMatchup(
      id: matchupId,
      teamA: _buildTeam(
        id: '$matchupId-team-a',
        name: _teamNames[teamAIndex],
        playerOffset: teamAIndex * 3,
      ),
      teamB: _buildTeam(
        id: '$matchupId-team-b',
        name: _teamNames[teamBIndex],
        playerOffset: teamBIndex * 3,
      ),
    );
  }

  CompetitionTeam _buildTeam({
    required String id,
    required String name,
    required int playerOffset,
  }) {
    return CompetitionTeam(
      id: id,
      name: name,
      players: List.generate(3, (index) {
        final playerIndex = (playerOffset + index) % _playerNames.length;
        return CompetitionPlayer(
          id: '$id-player-$index',
          name: _playerNames[playerIndex],
        );
      }),
    );
  }

  static const _teamNames = ['王多鱼队', '钱多多队', '逐风少年队', '蓝翼飞行队', '星火队'];
  static const _playerNames = [
    '王伟',
    '李庆超',
    '王大亮',
    '韩宁政',
    '阮晴桦',
    '刘美玲',
    '陈子航',
    '周白芷',
    '林培伦',
    '蔡依婷',
    '夏志豪',
    '赵云飞',
    '孙雨泽',
    '吴佳琪',
    '郑凯文',
  ];
}
