import 'dart:async';
import 'package:design/design.dart';
import 'package:flutter/material.dart';
import 'dialog_how_to_play_alert_design_model.dart';
import 'dialog_standard_container.dart';

class DialogHowToPlayContent {
  final Image image;
  final String contentText;
  final TextStyle? contentTextStyle;
  final double? contentFontSize;
  final Color? contentTextColor;
  final double? contentTextHeight;
  final int? contentLines;
  DialogHowToPlayContent({
    required this.image,
    required this.contentText,
    this.contentTextStyle,
    this.contentFontSize,
    this.contentTextColor,
    this.contentTextHeight,
    this.contentLines,
  });
}

class DialogHowTopPlayAlertSpaceConfig {
  final double? titleTopSpace;
  final double? imageTopSpace;
  final double? contentTopSpace;
  final double? indicatorTopSpace;
  final double? buttonTopSpace;
  final double? buttonBottomSpace;
  DialogHowTopPlayAlertSpaceConfig({
    this.titleTopSpace,
    this.imageTopSpace,
    this.contentTopSpace,
    this.indicatorTopSpace,
    this.buttonTopSpace,
    this.buttonBottomSpace,
  });
}

class DialogHowToPlayAlertWidget extends StatefulWidget {
  final String titleText;
  final TextStyle? titleTextStyle;
  final double? titleFontSize;
  final Color? titleColor;
  final Color? backgroundColor;
  final List<DialogHowToPlayContent> contentList;
  final Color? indicatorSelectColor;
  final Color? indicatorUnselectColor;
  final TextStyle? descriptionTextStyle;
  final String buttonText;
  final String lastButtonText;
  final TextStyle? buttonTextStyle;
  final double? buttonFontSize;
  final Color? buttonTextColor;
  final Color? buttonBackgroundColor;
  final VoidCallback? buttonCallback;
  final DialogHowTopPlayAlertSpaceConfig? spaceConfig;
  const DialogHowToPlayAlertWidget({
    Key? key,
    this.backgroundColor,
    required this.titleText,
    this.titleTextStyle,
    this.titleFontSize,
    this.titleColor,
    this.indicatorSelectColor,
    this.indicatorUnselectColor,
    required this.contentList,
    this.descriptionTextStyle,
    required this.buttonText,
    required this.lastButtonText,
    this.buttonFontSize,
    this.buttonTextColor,
    this.buttonTextStyle,
    this.buttonBackgroundColor,
    this.buttonCallback,
    this.spaceConfig,
  }) : super(key: key);

  @override
  _DialogHowToPlayAlertWidgetState createState() => _DialogHowToPlayAlertWidgetState();
}

class _DialogHowToPlayAlertWidgetState extends State<DialogHowToPlayAlertWidget> {
  final StreamController<int> _streamController = StreamController.broadcast();
  Stream<int> get _stream => _streamController.stream.asBroadcastStream();
  DialogHowToPlayAlertDesignSpec get designSpec => DialogHowToPlayAlertDesignSpec.get();
  DialogHowToPlayAlertContentDesignSpec get contentSpec => designSpec.contentSpec;

  DialogHowTopPlayAlertSpaceConfig? spaceConfig;
  var index = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _streamController.add(0);
    spaceConfig = widget.spaceConfig;
  }

  @override
  Widget build(BuildContext context) {
    if (widget.contentList == null || widget.contentList.isEmpty) {
      return Container();
    }
    return DialogStandardContainer(
        backgroundColor: widget.backgroundColor,
        showCloseImage: false,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Padding(
              padding: spaceConfig?.titleTopSpace != null
                  ? EdgeInsets.only(top: spaceConfig!.titleTopSpace!, left: contentSpec.titleInsets.left, right: contentSpec.titleInsets.right)
                  : contentSpec.titleInsets,
              child: Text(
                widget.titleText,
                style: TextStyle(
                  fontSize: widget.titleFontSize ?? contentSpec.titleFontSize,
                  color: widget.titleColor ?? const Color(0xff191b32),
                  fontWeight: FontWeight.w700,
                ),
                textAlign: TextAlign.center,
              ),
            ),
            StreamBuilder<int>(
                stream: _stream,
                builder: (context, snapshot) {
                  int index = snapshot.data ?? 0;
                  return Padding(
                    padding: spaceConfig?.imageTopSpace != null
                        ? EdgeInsets.only(top: spaceConfig!.imageTopSpace!, left: contentSpec.imageInsets.left, right: contentSpec.imageInsets.right)
                        : contentSpec.imageInsets,
                    child: widget.contentList[index].image,
                  );
                }),
            StreamBuilder<int>(
                stream: _stream,
                builder: (context, snapshot) {
                  int index = snapshot.data ?? 0;
                  DialogHowToPlayContent contentConfig = widget.contentList[index];
                  return Padding(
                    padding: spaceConfig?.contentTopSpace != null
                        ? EdgeInsets.only(
                            top: spaceConfig!.contentTopSpace!, left: contentSpec.descriptionInsets.left, right: contentSpec.descriptionInsets.right)
                        : contentSpec.descriptionInsets,
                    child: Stack(
                      children: [
                        Text(
                          "\n" * (contentConfig.contentLines ?? 1),
                          style: widget.descriptionTextStyle ??
                              TextStyle(
                                  fontSize: contentSpec.descriptionFontSize,
                                  color: contentConfig.contentTextColor ?? const Color(0xff000000),
                                  fontWeight: FontWeight.w500,
                                  height: contentConfig.contentTextHeight ?? 1.43),
                          textAlign: TextAlign.center,
                        ),
                        Text(
                          contentConfig.contentText,
                          style: widget.descriptionTextStyle ??
                              TextStyle(
                                  fontSize: contentSpec.descriptionFontSize,
                                  color: contentConfig.contentTextColor ?? const Color(0xff000000),
                                  fontWeight: FontWeight.w500,
                                  height: contentConfig.contentTextHeight ?? 1.43),
                          textAlign: TextAlign.center,
                          maxLines: contentConfig.contentLines,
                        ),
                      ],
                    ),
                  );
                }),
            SizedBox(
              height: spaceConfig?.indicatorTopSpace ?? contentSpec.indicatorTopSpace,
            ),
            StreamBuilder<int>(
                stream: _stream,
                builder: (context, snapshot) {
                  int index = snapshot.data ?? 0;
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: getIndicators(index),
                  );
                }),
            Padding(
              padding: EdgeInsets.only(
                  top: spaceConfig?.buttonTopSpace ?? contentSpec.buttonTopSpace,
                  bottom: spaceConfig?.buttonBottomSpace ?? contentSpec.buttonBottomSpace),
              child: GestureDetector(
                onTap: () {
                  index += 1;
                  if (index < widget.contentList.length) {
                    _streamController.add(index);
                  } else {
                    widget.buttonCallback?.call();
                  }
                },
                child: Container(
                    width: contentSpec.buttonWidth,
                    height: contentSpec.buttonHeight,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(color: widget.buttonBackgroundColor ?? const Color(0xff008B54), borderRadius: contentSpec.buttonRadius),
                    child: Text(
                      index < widget.contentList.length - 1 ? widget.buttonText : widget.lastButtonText,
                      style: widget.buttonTextStyle ??
                          TextStyle(
                              fontSize: widget.buttonFontSize ?? contentSpec.buttonFontSize,
                              color: widget.buttonTextColor ?? Colors.white,
                              fontWeight: FontWeight.w600),
                      textAlign: TextAlign.center,
                    )),
              ),
            )
          ],
        ));
  }

  List<Widget> getIndicators(int selectIndex) {
    List<Widget> list = [];
    for (int i = 0; i < widget.contentList.length; i++) {
      list.add(Container(
        margin: EdgeInsets.symmetric(horizontal: contentSpec.indicatorSpace),
        width: contentSpec.indicatorSize,
        height: contentSpec.indicatorSize,
        decoration: BoxDecoration(
          color: i == selectIndex
              ? widget.indicatorSelectColor ?? const Color(0xff008B54)
              : widget.indicatorUnselectColor ?? const Color(0xff000000).withOpacity(0.2),
          borderRadius: BorderRadius.circular(contentSpec.indicatorSize / 2.0),
        ),
      ));
    }
    return list;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _streamController.close();
  }
}
