import 'package:flutter/material.dart';

import '../provider/data.dart';
import '../utils/game_utils.dart';

class Board {
  static Container buildGameBoard(Data myProvider) {
    String skin = myProvider.skin;

    int maxGuessesCount = myProvider.maxGuessesCount;
    int wordLength = int.parse(myProvider.length);

    Widget buildCellWidget(
        String cellValue, String cellTip, bool hasFocus, String foundLetter) {
      Color textColor = Colors.white;
      Color focusBorderColor = Colors.yellow.shade700;
      Color defaultBorderColor = Colors.white;

      String cellImage = 'empty';
      if (cellTip != '') {
        cellImage = cellTip;
      }

      if ((foundLetter != ' ') && (cellValue == ' ')) {
        cellValue = foundLetter;
        cellImage = 'good';
      }

      Image imageWidget = Image(
        image: AssetImage('assets/skins/' + skin + '_' + cellImage + '.png'),
        fit: BoxFit.fill,
      );

      Widget cellBackground = Container(
        decoration: BoxDecoration(
          border: Border.all(
            width: 4.0,
            color: hasFocus ? focusBorderColor : defaultBorderColor,
            style: BorderStyle.solid,
          ),
        ),
        child: imageWidget,
      );

      Text textWidget = Text(
        cellValue,
        style: TextStyle(
          color: textColor,
          fontSize: 40.0,
          fontWeight: FontWeight.w900,
        ),
        textAlign: TextAlign.center,
      );

      return Stack(
        alignment: Alignment.center,
        children: <Widget>[
          cellBackground,
          Center(child: textWidget),
        ],
      );
    }

    List<String> guesses = myProvider.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 = myProvider.currentGuess;
      }

      List<String> tips = GameUtils.getTips(myProvider, 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];
        }

        bool hasFocus = (!myProvider.gameWon) &&
            (lineIndex == guesses.length) &&
            (colIndex == word.length);

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

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

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

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

    return Container(
      margin: EdgeInsets.symmetric(horizontal: horizontalMargins),
      padding: EdgeInsets.all(2),
      child: Table(
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        border: TableBorder.all(
          color: Colors.white,
          style: BorderStyle.none,
        ),
        children: tableRows,
      ),
    );
  }
}