import 'package:flutter/material.dart';

import 'package:puzzlegame/config/default_game_settings.dart';
import 'package:puzzlegame/config/default_global_settings.dart';
import 'package:puzzlegame/models/settings/settings_game.dart';
import 'package:puzzlegame/models/settings/settings_global.dart';
import 'package:puzzlegame/ui/helpers/styled_button.dart';
import 'package:puzzlegame/ui/parameters/parameter_painter.dart';
import 'package:puzzlegame/utils/tools.dart';

class ParameterWidget extends StatelessWidget {
  const ParameterWidget({
    super.key,
    required this.code,
    required this.value,
    required this.isSelected,
    required this.size,
    required this.gameSettings,
    required this.globalSettings,
    required this.onPressed,
  });

  final String code;
  final String value;
  final bool isSelected;
  final double size;
  final GameSettings gameSettings;
  final GlobalSettings globalSettings;
  final VoidCallback onPressed;

  static const Color buttonColorActive = Colors.blue;
  static const Color buttonColorInactive = Colors.white;
  static const double buttonBorderWidth = 4.0;
  static const double buttonBorderRadius = 12.0;

  @override
  Widget build(BuildContext context) {
    Widget content = const SizedBox.shrink();

    switch (code) {
      case DefaultGameSettings.parameterCodeTilesetSize:
        content = getTilesetSizeParameterItem();
        break;
      case DefaultGlobalSettings.parameterCodeSkin:
        content = getSkinParameterItem();
        break;
      default:
        printlog('Unknown parameter: $code/$value');
        content = getUnknownParameterItem();
    }

    final Color buttonColor = isSelected ? buttonColorActive : buttonColorInactive;

    return Container(
      decoration: BoxDecoration(
        color: buttonColor,
        borderRadius: BorderRadius.circular(buttonBorderRadius),
        border: Border.all(
          color: buttonColor,
          width: buttonBorderWidth,
        ),
      ),
      child: content,
    );
  }

  // "unknown" parameter -> simple block with text
  Widget getUnknownParameterItem() {
    return StyledButton.text(
      caption: '$code / $value',
      color: Colors.grey,
      onPressed: null,
    );
  }

  Widget getTilesetSizeParameterItem() {
    Color backgroundColor = Colors.grey;

    switch (value) {
      case DefaultGameSettings.tilesetSizeValueSmall:
        backgroundColor = Colors.green;
        break;
      case DefaultGameSettings.tilesetSizeValueMedium:
        backgroundColor = Colors.orange;
        break;
      case DefaultGameSettings.tilesetSizeValueLarge:
        backgroundColor = Colors.red;
        break;
      default:
        printlog('Wrong value for size parameter value: $value');
    }

    return StyledButton(
      color: backgroundColor,
      onPressed: onPressed,
      child: CustomPaint(
        size: Size(size, size),
        willChange: false,
        painter: ParameterPainter(
          code: code,
          value: value,
          gameSettings: gameSettings,
          globalSettings: globalSettings,
        ),
        isComplex: true,
      ),
    );
  }

  Widget getSkinParameterItem() {
    return StyledButton(
      color: Colors.green.shade800,
      onPressed: onPressed,
      child: Image(
        image: AssetImage('assets/ui/${DefaultGlobalSettings.parameterCodeSkin}_$value.png'),
        fit: BoxFit.fill,
      ),
    );
  }
}