import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:momomotus/config/default_game_settings.dart';
import 'package:momomotus/cubit/game_cubit.dart';
import 'package:momomotus/models/game/game.dart';
import 'package:momomotus/ui/widgets/game/game_cell.dart';

class GameBoardWidget extends StatelessWidget {
  const GameBoardWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<GameCubit, GameState>(
      builder: (BuildContext context, GameState gameState) {
        final Game currentGame = gameState.currentGame;
        final GameCubit gameCubit = BlocProvider.of<GameCubit>(context);

        const int maxGuessesCount = DefaultGameSettings.maxGuessesCount;
        final int wordLength = int.parse(currentGame.gameSettings.length);

        final List<String> guesses = currentGame.guesses;

        List<TableRow> tableRows = [];
        for (int lineIndex = 0; lineIndex < maxGuessesCount; lineIndex++) {
          String word = '';
          if (lineIndex < guesses.length) {
            word = guesses[lineIndex];
          } else if (lineIndex == guesses.length) {
            word = currentGame.currentGuess;
          }

          final List<String> tips = gameCubit.getTips(word);

          List<TableCell> tableCells = [];
          for (int colIndex = 0; colIndex < wordLength; colIndex++) {
            String cellValue = ' ';
            if (word.length > colIndex) {
              cellValue = word[colIndex];
            }

            String cellTip = '';
            if (lineIndex < guesses.length) {
              cellTip = tips[colIndex];
            }

            final bool hasFocus = (!currentGame.foundWord) &&
                (lineIndex == guesses.length) &&
                (colIndex == word.length);

            final String foundLetter =
                ((!currentGame.foundWord) && (lineIndex == guesses.length))
                    ? currentGame.foundLetters.substring(colIndex, colIndex + 1)
                    : ' ';

            tableCells.add(TableCell(
              child: GameCellWidget(
                cellValue: cellValue,
                cellTip: cellTip,
                hasFocus: hasFocus,
                foundLetter: foundLetter,
              ),
            ));
          }

          tableRows.add(TableRow(children: tableCells));
        }

        List<Widget> gameBoard = [
          Table(
            defaultVerticalAlignment: TableCellVerticalAlignment.middle,
            border: TableBorder.all(
              color: Colors.white,
              style: BorderStyle.none,
            ),
            children: tableRows,
          ),
        ];

        double horizontalMargins = 10;
        if (wordLength < 6) {
          horizontalMargins = 40;
          if (wordLength < 5) {
            horizontalMargins = 60;
          }
        }

        // Failed -> show word
        if (currentGame.isFinished && !currentGame.gameWon) {
          gameBoard.add(Text(
            currentGame.word,
            style: const TextStyle(
              fontSize: 40,
              fontWeight: FontWeight.bold,
            ),
          ));
        }

        return Container(
          margin: EdgeInsets.symmetric(horizontal: horizontalMargins),
          padding: const EdgeInsets.all(2),
          child: Column(
            children: gameBoard,
          ),
        );
      },
    );
  }
}