import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:jeweled/config/default_game_settings.dart';
import 'package:jeweled/config/default_global_settings.dart';
import 'package:jeweled/cubit/game_cubit.dart';
import 'package:jeweled/cubit/settings_game_cubit.dart';
import 'package:jeweled/cubit/settings_global_cubit.dart';
import 'package:jeweled/ui/painters/parameter_painter.dart';

class Parameters extends StatelessWidget {
  const Parameters({super.key});

  final double separatorHeight = 8.0;

  List<Widget> buildParametersLine({
    required String code,
    required bool isGlobal,
  }) {
    final List<Widget> parameterButtons = [];

    final List<int> availableValues = isGlobal
        ? DefaultGlobalSettings.getAvailableValues(code)
        : DefaultGameSettings.getAvailableValues(code);

    availableValues.forEach((value) {
      final Widget parameterButton = BlocBuilder<GameSettingsCubit, GameSettingsState>(
        builder: (BuildContext context, GameSettingsState gameSettingsState) {
          return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
            builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
              final GameSettingsCubit gameSettingsCubit =
                  BlocProvider.of<GameSettingsCubit>(context);
              final GlobalSettingsCubit globalSettingsCubit =
                  BlocProvider.of<GlobalSettingsCubit>(context);

              final int currentValue = isGlobal
                  ? globalSettingsCubit.getParameterValue(code)
                  : gameSettingsCubit.getParameterValue(code);

              final bool isActive = (value == currentValue);

              final double displayWidth = MediaQuery.of(context).size.width;
              final double itemWidth = displayWidth / availableValues.length - 25;

              return TextButton(
                child: Container(
                  margin: EdgeInsets.all(0),
                  padding: EdgeInsets.all(0),
                  child: CustomPaint(
                    size: Size(itemWidth, itemWidth),
                    willChange: false,
                    painter: ParameterPainter(
                      code: code,
                      value: value,
                      isSelected: isActive,
                      gameSettings: gameSettingsState.settings,
                      globalSettings: globalSettingsState.settings,
                    ),
                    isComplex: true,
                  ),
                ),
                onPressed: () => isGlobal
                    ? globalSettingsCubit.setParameterValue(code, value)
                    : gameSettingsCubit.setParameterValue(code, value),
              );
            },
          );
        },
      );

      parameterButtons.add(parameterButton);
    });

    return parameterButtons;
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> lines = [];

    // Game settings
    DefaultGameSettings.availableParameters.forEach((code) {
      lines.add(Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: buildParametersLine(
          code: code,
          isGlobal: false,
        ),
      ));

      lines.add(SizedBox(height: separatorHeight));
    });

    // Global settings
    DefaultGlobalSettings.availableParameters.forEach((code) {
      lines.add(Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: buildParametersLine(
          code: code,
          isGlobal: true,
        ),
      ));

      lines.add(SizedBox(height: separatorHeight));
    });

    lines.add(SizedBox(height: separatorHeight));
    lines.add(buildStartNewGameButton());

    return Column(
      children: lines,
    );
  }

  static Image buildImageWidget(String imageAssetCode) {
    return Image(
      image: AssetImage('assets/icons/' + imageAssetCode + '.png'),
      fit: BoxFit.fill,
    );
  }

  static Container buildImageContainerWidget(String imageAssetCode) {
    return Container(
      child: buildImageWidget(imageAssetCode),
    );
  }

  static Column buildDecorationImageWidget() {
    return Column(
      children: [
        TextButton(
          child: buildImageContainerWidget('placeholder'),
          onPressed: () => null,
        ),
      ],
    );
  }

  static Widget buildStartNewGameButton() {
    const double blockMargin = 3.0;
    const double blockPadding = 2.0;

    return BlocBuilder<GameSettingsCubit, GameSettingsState>(
      builder: (BuildContext context, GameSettingsState gameSettingsState) {
        return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
          builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
            final GameCubit gameCubit = BlocProvider.of<GameCubit>(context);

            return Container(
              margin: EdgeInsets.all(blockMargin),
              padding: EdgeInsets.all(blockPadding),
              child: Table(
                defaultColumnWidth: IntrinsicColumnWidth(),
                children: [
                  TableRow(
                    children: [
                      buildDecorationImageWidget(),
                      Column(
                        children: [
                          TextButton(
                            child: buildImageContainerWidget('button_start'),
                            onPressed: () => gameCubit.startNewGame(
                              gameSettings: gameSettingsState.settings,
                              globalSettings: globalSettingsState.settings,
                            ),
                          ),
                        ],
                      ),
                      buildDecorationImageWidget(),
                    ],
                  ),
                ],
              ),
            );
          },
        );
      },
    );
  }
}