import 'dart:math';

import 'package:flutter/material.dart';

import 'package:tetrisdual/provider/data.dart';

class Counter {
  // Current counter
  bool _match = false; // Does this new tetrimino touch an other tetrimino of same player
  int _lines = 0; // Count lines fully filled by this new tetrimino
  int _holes = 0; // Count non fillable holes caused by this new tetrimino

  // Points definitions
  static const int _base = 50;
  static const int _pointsIfMatch = 50;
  static const int _pointsPerLine = 60;
  static const int _pointsPerHole = -10;

  static const double iconSize = 30.0;
  static const double fontSize = 50.0;
  static const double spacerHeight = 7;

  // Counter categories icons
  static const Color categoryIconColor = Colors.green;
  static const Icon iconTouchingColor = Icon(
    Icons.join_full,
    color: categoryIconColor,
    size: iconSize,
  );
  static const Icon iconRowsCount = Icon(
    Icons.table_rows,
    color: categoryIconColor,
    size: iconSize,
  );
  static const Icon iconHolesCount = Icon(
    Icons.check_box_outline_blank,
    color: categoryIconColor,
    size: iconSize,
  );

  // Action buttons icons
  static const Color buttonIconColor = Colors.blue;
  static const Icon iconRemove = Icon(
    Icons.remove,
    color: buttonIconColor,
    size: iconSize,
  );
  static const Icon iconAdd = Icon(
    Icons.add,
    color: buttonIconColor,
    size: iconSize,
  );

  Widget buildCounterWidget(Data myProvider) {
    return Table(
      children: [
        buildMatchWidget(myProvider),
        buildSpacerRow(),
        buildLinesWidget(myProvider),
        buildSpacerRow(),
        buildHolesWidget(myProvider),
        buildSpacerRow(),
      ],
    );
  }

  TableRow buildSpacerRow() {
    return const TableRow(children: [
      SizedBox(height: spacerHeight),
      SizedBox(height: spacerHeight),
      SizedBox(height: spacerHeight),
      SizedBox(height: spacerHeight)
    ]);
  }

  TableRow buildMatchWidget(Data myProvider) {
    return TableRow(
      children: [
        iconTouchingColor,
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconRemove,
          onPressed: () {
            _match = false;
            myProvider.redraw();
          },
        ),
        Center(
          child: Icon(
            _match ? Icons.radio_button_checked : Icons.radio_button_unchecked,
            color: categoryIconColor,
            size: iconSize,
          ),
        ),
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconAdd,
          onPressed: () {
            _match = true;
            myProvider.redraw();
          },
        ),
      ],
    );
  }

  TableRow buildLinesWidget(Data myProvider) {
    return TableRow(
      children: [
        iconRowsCount,
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconRemove,
          onPressed: () {
            _lines = max(_lines - 1, 0);
            myProvider.redraw();
          },
        ),
        Center(
          child: Text(
            _lines.toString(),
            style: const TextStyle(
              fontFamily: 'Blocks',
              fontSize: fontSize,
            ),
            textHeightBehavior: const TextHeightBehavior(
              applyHeightToFirstAscent: false,
              applyHeightToLastDescent: false,
            ),
          ),
        ),
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconAdd,
          onPressed: () {
            _lines = min(_lines + 1, 4);
            myProvider.redraw();
          },
        ),
      ],
    );
  }

  TableRow buildHolesWidget(Data myProvider) {
    return TableRow(
      children: [
        iconHolesCount,
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconRemove,
          onPressed: () {
            _holes = max(_holes - 1, 0);
            myProvider.redraw();
          },
        ),
        Center(
          child: Text(
            _holes.toString(),
            style: const TextStyle(
              fontFamily: 'Blocks',
              fontSize: fontSize,
            ),
            textHeightBehavior: const TextHeightBehavior(
              applyHeightToFirstAscent: false,
              applyHeightToLastDescent: false,
            ),
          ),
        ),
        IconButton(
          padding: EdgeInsets.zero,
          constraints: const BoxConstraints(),
          style: const ButtonStyle(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          ),
          icon: iconAdd,
          onPressed: () {
            _holes = min(_holes + 1, 9);
            myProvider.redraw();
          },
        ),
      ],
    );
  }

  int computePoints() {
    return _base +
        (_match ? _pointsIfMatch : 0) +
        _lines * _pointsPerLine +
        _holes * _pointsPerHole;
  }

  void reset() {
    _match = false;
    _lines = 0;
    _holes = 0;
  }
}