import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:mnemo_cards/managers/audio_player.dart';

import '../../../main.dart';
import '../../../widgets/game_card_widget.dart';
import 'test_image.dart';

class CommonTestQuestionWidget extends StatelessWidget {
  final String? text;
  final String? image;
  final String? audio;

  const CommonTestQuestionWidget({
    this.text,
    this.image,
    this.audio,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).textTheme;

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (image != null)
          Flexible(
            child: Container(
              alignment: Alignment.center,
              margin: const EdgeInsets.all(4.0),
              decoration: BoxDecoration(
                borderRadius:
                    BorderRadius.circular(GameCardWidget.borderRadius),
              ),
              child: AspectRatio(
                aspectRatio: text == null ? 1 : 1.5,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    TestImageWidget(
                      TestImage.image(image!),
                    ),
                    if (audio != null && text == null)
                      Container(
                        padding: EdgeInsets.all(4.0.h),
                        alignment: Alignment.topRight,
                        child: IconButton(
                          icon: Image.asset(
                            'icons/sound_on.png',
                            width: 26,
                          ),
                          onPressed: () => AudioPlayer.playAudio(audio),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          )
        else if (text == null && audio != null)
          Flexible(
            child: Center(
              child: IconButton(
                icon: Image.asset(
                  'icons/sound_on.png',
                  width: 80,
                  height: 80,
                ),
                onPressed: () => AudioPlayer.playAudio(audio),
              ),
            ),
          ),
        if (text != null)
          Container(
            alignment: Alignment.center,
            padding: const EdgeInsets.only(top: 8.0),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  text!,
                  style: theme.headlineLarge,
                ),
                if (audio != null)
                  Container(
                    padding: EdgeInsets.only(left: 4.w, top: 4.h),
                    child: IconButton(
                      icon: Image.asset(
                        'icons/sound_on.png',
                        width: 26,
                        height: theme.headlineLarge?.fontSize,
                      ),
                      onPressed: () => AudioPlayer.playAudio(audio),
                    ),
                  )
              ],
            ),
          ),
      ],
    );
  }
}
