import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:mnemo_cards/features/tests/test_manager.dart';
import 'package:mnemo_cards/features/tests/test_widgets/common_test_question_widget.dart';
import 'package:mnemo_cards/features/tests/test_widgets/test_image.dart';
import 'package:mnemo_cards/main.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';

import '../../../di/locator.dart';
import '../../../widgets/game_card_widget.dart';
import '../question_states/simple_test_state.dart';
import 'test_button.dart';

class SimpleTestWidget extends StatelessWidget {
  final SimpleTestQuestionBody _simpleTestModel;

  SimpleTestQuestionBody get model => _simpleTestModel;

  bool get isCorrect => state.answer.startsWith(model.answer);

  SimpleTestState get state => manager.getState(model.id!) as SimpleTestState;

  TestManager get manager => locator.testManager;

  Color get testColor => manager.color;

  const SimpleTestWidget(this._simpleTestModel, {super.key});

  @override
  Widget build(BuildContext context) {
    final random = Random(state.seed);
    final buttons = [...model.buttons]..shuffle(random);

    return LayoutBuilder(builder: (context, constraints) {
      return Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: CommonTestQuestionWidget(
                text: model.text,
                image: model.image,
                audio: model.audio,
              ),
            ),
            SizedBox(
              height: 16.0,
            ),
            StatefulBuilder(builder: (context, setState) {
              void setAnswer(String answer) {
                setState(() {
                  manager.setState(
                    model.id!,
                    state.copyWith(
                      answer: answer,
                    ),
                  );
                  // only setting isCorrect once
                  manager.setState(
                    model.id!,
                    state.copyWith(
                      isAnswered: true,
                      isCorrect: state.isAnswered ? state.isCorrect : isCorrect,
                    ),
                  );
                });
                if (isCorrect) {
                  Future.delayed(
                    Duration(milliseconds: 300),
                    manager.nextTest,
                  );
                }
              }

              bool buttonLine = (model.image != null &&
                  buttons.where((element) => element.image != null).length !=
                      2);
              final imageSide = buttonLine
                  ? constraints.maxWidth / 4 - 8
                  : constraints.maxWidth / 2 - 16;
              final spacing = buttonLine ? 4.0 : 8.0;

              return AbsorbPointer(
                absorbing: state.isCorrect,
                child: Wrap(
                  spacing: spacing,
                  runSpacing: 8.0,
                  children: buttons
                      .map(
                        (e) => e.isTextButton
                            ? Container(
                                width: constraints.maxWidth / 2 - 16,
                                height: 90.h,
                                child: TestButton(
                                  text: e.text,
                                  onTap: () {
                                    setAnswer(e.id);
                                  },
                                  // borderColor: testColor,
                                  color: state.answer == e.id
                                      ? isCorrect
                                          ? Colors.green[200]
                                          : Colors.red[200]
                                      : Colors.white,
                                ),
                              )
                            : SizedBox(
                                width: imageSide,
                                height: imageSide,
                                child: TestButton(
                                  image: TestImage.image(e.image!),
                                  onTap: () {
                                    setAnswer(e.id);
                                  },
                                  // borderColor: testColor,
                                  color: e.id == state.answer
                                      ? isCorrect
                                          ? Colors.green[200]
                                          : Colors.red[200]
                                      : Colors.white,
                                ),
                              ),
                      )
                      .toList(),
                ),
              );
            })
          ],
        ),
      );
    });
  }
}
