import 'package:design/design.dart';
import 'package:guru_rating/page/ios_thankyou_design_spec.dart';
import 'package:guru_rating/page/rating_style.dart';
import 'package:guru_widgets/button/circle_button.dart';
import 'package:guru_widgets/guru_widgets.dart';

class IosThankYouPage extends StatefulWidget {
  final IosThankYouStyle thankYouStyle;

  const IosThankYouPage(this.thankYouStyle, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => IosThankYouState();
}

class IosThankYouState extends State<IosThankYouPage> {
  IosThankYouDialogDesignSpec get designSpec => IosThankYouDesignSpec.get().iosThankYouDialogSpec;

  late RattingButtonTapDelegate delegate = RattingButtonTapDelegate(_onButtonTap);

  void _onButtonTap() {
    widget.thankYouStyle.okClick?.call();
    Navigator.pop(context);
  }

  Widget _buildCloseButton() {
    return Align(
      alignment: AlignmentDirectional.centerStart,
      child: Padding(
        padding: EdgeInsetsDirectional.only(start: designSpec.closeIconStartSpacing),
        child: CircleButton(
            radius: designSpec.closeIconInkRadius,
            content: Center(
                child: Image.asset(
              widget.thankYouStyle.closeImage,
              width: designSpec.closeIconSize,
              height: designSpec.closeIconSize,
              color: widget.thankYouStyle.closeImageColor,
            )),
            backgroundColor: Colors.transparent,
            onTapCallback: () {
              widget.thankYouStyle.closeClick?.call();
              Navigator.pop(context);
            }),
      ),
    );
  }

  Widget _buildContent() {
    return UnconstrainedBox(
      child: Center(
        child: FlexibleContainer(
          radius: designSpec.radius,
          color: widget.thankYouStyle.backgroundStyle.backgroundColor,
          gradient: widget.thankYouStyle.backgroundStyle.gradient,
          constraints: BoxConstraints(
            minHeight: designSpec.measuredSize.height,
            minWidth: designSpec.measuredSize.width,
            maxWidth: designSpec.measuredSize.width,
          ),
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    width: double.infinity,
                  ),
                  Center(
                    child: FlexibleContainer(
                      margin: EdgeInsets.only(top: designSpec.imageTopSpacing),
                      width: designSpec.imageSize.width,
                      height: designSpec.imageSize.height,
                      borderColor: kDebugMode ? Colors.red : null,
                      child: Center(
                        child: Image.asset(
                          widget.thankYouStyle.thankYouImage,
                          width: designSpec.imageSize.width,
                          height: designSpec.imageSize.height,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ),
                  PositionedDirectional(
                      start: 0, top: designSpec.closeIconTopSpacing, child: _buildCloseButton()),
                ],
              ),
              SizedSpacer(height: designSpec.titleTopSpacing),
              Text(
                widget.thankYouStyle.thankYouTitle,
                style: TextStyle(
                    fontSize: designSpec.titleFontSize,
                    fontWeight: FontWeight.w700,
                    color: widget.thankYouStyle.titleColor),
              ),
              SizedSpacer(height: designSpec.summaryTopSpacing),
              SizedBox(
                width: designSpec.summaryMaxWidth,
                child: Text(
                  widget.thankYouStyle.thankYouSummary,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: designSpec.summaryFontSize,
                    fontWeight: FontWeight.w500,
                    color: widget.thankYouStyle.summaryTextColor,
                  ),
                  strutStyle: const StrutStyle(height: 1.5),
                ),
              ),
              SizedSpacer(height: designSpec.buttonTopSpacing),
              widget.thankYouStyle.buttonStyle != null
                  ? GestureDetector(
                      onTap: delegate.onTap,
                      child: FlexibleContainer(
                        width: designSpec.buttonSize.width,
                        height: designSpec.buttonSize.height,
                        radius: designSpec.buttonRadius,
                        color: widget.thankYouStyle.buttonStyle!.buttonBgColor,
                        alignment: Alignment.center,
                        child: Text(
                          widget.thankYouStyle.buttonStyle!.buttonText,
                          style: TextStyle(
                            fontSize: designSpec.buttonTextSize,
                            color: widget.thankYouStyle.buttonStyle!.buttonTextColor,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    )
                  : widget.thankYouStyle.buttonBuilder!.call(delegate),
              SizedSpacer(height: designSpec.buttonBottomSpacing),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _buildContent();
  }
}
