import 'package:flutter/material.dart';

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

class Cell {
  int value;
  bool isFixed;
  int conflictsCount = 0;

  Cell(
    @required this.value,
    @required this.isFixed,
  );

  static double cellBorderWidth = 3;
  static Color cellBorderDarkColor = Colors.black;
  static Color cellBorderLightColor = Colors.grey;
  static Color cellBorderSelectedColor = Colors.red;

  Container widget(Data myProvider, Border borders, int row, int col) {
    String imageAsset = 'assets/skins/empty.png';
    if (this.value > 0) {
      imageAsset = 'assets/skins/' + myProvider.skin + '_' + this.value.toString() + '.png';
    }

    int size = myProvider.size;

    return Container(
      decoration: BoxDecoration(
        color: this.getBackgroundColor(myProvider),
        border: borders,
      ),
      child: GestureDetector(
        child: Image(
          image: AssetImage(imageAsset),
          fit: BoxFit.fill
        ),
        onTap: () {
          if (col != null && row != null) {
            if (!this.isFixed && (col != myProvider.currentCellCol || row != myProvider.currentCellRow)) {
              myProvider.selectCell(col, row);
            } else {
              myProvider.selectCell(null, null);
            }
          }
        },
      )
    );
  }

  Color getBackgroundColor(Data myProvider) {
    Color editableCellColor = Colors.grey[100];
    Color editableCellColorConflict = Colors.pink[100];
    Color fixedCellColor = Colors.grey[300];
    Color fixedCellColorConflict = Colors.pink[200];

    Color backgroundColor = editableCellColor;

    if (this.isFixed) {
      backgroundColor = fixedCellColor;
    }

    if (myProvider.showConflicts && (this.conflictsCount != 0)) {
      if (this.isFixed) {
        backgroundColor = fixedCellColorConflict;
      } else {
        backgroundColor = editableCellColorConflict;
      }
    }

    return backgroundColor;
  }

  Container widgetUpdateValue(Data myProvider) {
    String imageAsset = 'assets/skins/empty.png';
    if (this.value > 0) {
      imageAsset = 'assets/skins/' + myProvider.skin + '_' + this.value.toString() + '.png';
    }

    return Container(
      decoration: BoxDecoration(
        color: Colors.grey[200],
        border: Border.all(
          color: Colors.black,
          width: 2,
        ),
      ),
      child: GestureDetector(
        child: Image(
          image: AssetImage(imageAsset),
          fit: BoxFit.fill
        ),
        onTap: () {
          if (myProvider.currentCellCol != null && myProvider.currentCellRow != null) {
            myProvider.updateCellValue(myProvider.currentCellCol, myProvider.currentCellRow, this.value);
          }
          myProvider.selectCell(null, null);
        },
      )
    );
  }

  static Border getCellBorders(Data myProvider, int row, int col) {
    int size = myProvider.size;

    Border borders = Border.all(
      color: cellBorderSelectedColor,
      width: cellBorderWidth,
    );

    if (col != myProvider.currentCellCol || row != myProvider.currentCellRow) {
      borders = Border(
        top: BorderSide(width: cellBorderWidth, color: ((row % size) == 0) ? cellBorderDarkColor : cellBorderLightColor),
        left: BorderSide(width: cellBorderWidth, color: ((col % size) == 0) ? cellBorderDarkColor : cellBorderLightColor),
        right: BorderSide(width: cellBorderWidth, color: (((col + 1) % size) == 0) ? cellBorderDarkColor : cellBorderLightColor),
        bottom: BorderSide(width: cellBorderWidth, color: (((row + 1) % size) == 0) ? cellBorderDarkColor : cellBorderLightColor),
      );
    }

    return borders;
  }

}