import 'package:flutter/material.dart';

import 'package:momomotus/provider/data.dart';
import 'package:momomotus/utils/game_utils.dart';

class Keyboard {
  static Container buildWidget(Data myProvider) {
    final List<List<String>> keys = [
      ['A', 'Z', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
      ['Q', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M'],
      ['<', ' ', 'W', 'X', 'C', 'V', 'B', 'N', ' ', '!'],
    ];

    Widget buildKeyWidget(String key) {
      String keyText = key;
      if (key == '<') {
        keyText = '⬅️';
      } else if (key == '!') {
        keyText = '☑️';
      }

      Color keyColor = Colors.black;

      if (key == ' ') {
        return const SizedBox();
      }

      return Stack(
        alignment: Alignment.center,
        children: <Widget>[
          const Image(
            image: AssetImage('assets/icons/key.png'),
            fit: BoxFit.fill,
          ),
          Center(
            child: TextButton(
              style: TextButton.styleFrom(
                padding: const EdgeInsets.all(0),
              ),
              child: Text(
                keyText,
                style: TextStyle(
                  color: keyColor,
                  fontSize: 30.0,
                  fontWeight: FontWeight.w800,
                ),
                textAlign: TextAlign.center,
              ),
              onPressed: () {
                if (key == '<') {
                  GameUtils.removeLetter(myProvider);
                } else if (key == '!') {
                  GameUtils.submitWord(myProvider);
                } else if (key != ' ') {
                  GameUtils.addLetter(myProvider, key);
                }
              },
            ),
          ),
        ],
      );
    }

    List<TableRow> tableRows = [];
    for (var row in keys) {
      List<TableCell> tableCells = [];
      for (var key in row) {
        tableCells.add(TableCell(
          child: buildKeyWidget(key),
        ));
      }
      tableRows.add(TableRow(children: tableCells));
    }

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 2),
      padding: const EdgeInsets.all(2),
      child: Table(
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        children: tableRows,
      ),
    );
  }
}