import 'package:example/data/settings/ui_settings.dart';
import 'package:example/generated/assets.dart';
import 'package:example/generated/l10n.dart';
import 'package:example/pages/root/root_model.dart';
import 'package:example/pages/widgets/guru_demo_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:guru_utils/extensions/extensions.dart';
import 'package:guru_widgets/guru_widgets.dart';
import 'package:guru_popup/guru_popup.dart';
import '../../console/console_button.dart';
import 'dialog_controller.dart';
import 'package:guru_widgets/theme/guru_theme.dart';
import 'package:guru_widgets/pages/settings/guru_settings_page.dart';
import 'package:guru_widgets/appbar/guru_app_bar.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

class DialogPage extends GetWidget<DialogController> {
  DialogPage({Key? key}) : super(key: key);

  final RootDesignSpec designSpec = RootDesignSpec.get();

  final _divider = const HorizontalDivider(
    height: 0.3,
    indent: 16,
    endIndent: 16,
    thickness: 0.5,
    color: Colors.white30,
  );

  Widget buildEntranceItem(String text,
      {String? leadingAsset, VoidCallback? onTap, TextStyle? style}) {
    final List<Widget> leadingWidgets = [];
    if (leadingAsset != null) {
      leadingWidgets.addAll([
        Image.asset(leadingAsset,
            width: designSpec.itemIconSize, height: designSpec.itemIconSize),
        SizedSpacer(width: designSpec.itemIconEndSpacing)
      ]);
    }
    return Material(
      color: const Color(0xFF1D1D1D),
      child: Ink(
        height: designSpec.itemHeight,
        child: InkWell(
          onTap: () {
            onTap?.call();
          },
          child: Padding(
            padding: EdgeInsetsDirectional.only(
                start: designSpec.itemStartSpacing,
                end: designSpec.entryItemEndSpacing),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ...leadingWidgets,
                Expanded(
                  flex: 1,
                  child: AutoSizeText(text,
                      minFontSize: 10,
                      style: style ??
                          TextStyle(
                              fontSize: designSpec.itemTextFontSize,
                              fontWeight: GuruTheme.fwMedium,
                              color: Colors.white)),
                ),
                Image.asset(Assets.imagesIcArrowRight,
                    width: designSpec.entryItemIconSize,
                    height: designSpec.entryItemIconSize,
                    color: Colors.white),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildBody(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const SizedSpacer(height: 16),
        const Padding(
            padding: EdgeInsets.only(left: 12),
            child: Text('Daily challenge',
                style: TextStyle(color: Colors.white, fontSize: 18))),
        const SizedSpacer(height: 8),
        buildEntranceItem("Daily challenge", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              title: 'Daily Challenge',
              illustration: Image.asset(
                  "assets/images/daily_challenge/ic_dialog_main.png"),
              summary: "Well done! You've unlocked the Daily Challenge!",
              primaryButtonAction: 'Check it out');
        }),
        const SizedSpacer(height: 16),
        buildEntranceItem(
            "Cancelable Loading(+8s-Error; +5s-Loading; +15s-complete)",
            onTap: () async {
          final BehaviorSubject<String?> subject = BehaviorSubject.seeded(null);
          final completer = GuruPopup.instance
              .showCancelableLoading(errorStream: subject.stream);
          await Future.delayed(const Duration(seconds: 8));
          subject.add("Oops! Please check your Internet connection.");
          await Future.delayed(const Duration(seconds: 5));
          subject.add(null);
          await Future.delayed(const Duration(seconds: 15));
          completer?.complete();
        }),
        _divider,
        buildEntranceItem("Simple Loading(+5s-complete)", onTap: () async {
          final completer = GuruPopup.instance.showSimpleLoading();
          await Future.delayed(const Duration(seconds: 5));
          completer.complete();
        }),
        _divider,
        buildEntranceItem("Feedback Loading(+5s-complete)", onTap: () async {
          final successStream = Stream.fromFuture(
              Future.delayed(const Duration(seconds: 4)).then((_) => true));
          final completer = GuruPopup.instance.showFeedbackLoading(
              successStream: successStream,
              feedbackText: "short feedback text");
          await Future.delayed(const Duration(seconds: 7));
          completer.complete();
        }),
        _divider,
        buildEntranceItem("no title + no summary", onTap: () async {
          await GuruPopup.instance.showPurchaseStatusDialog();
        }),
        _divider,
        buildEntranceItem("no title + no summary", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset("assets/images/pic_problems.png"),
              primaryButtonAction: "OK");
        }),
        _divider,
        buildEntranceItem("no title + summary", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset("assets/images/pic_problems.png"),
              primaryButtonAction: "Contact Support",
              summary: "Having problems purc");
        }),
        _divider,
        buildEntranceItem("only title", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset("assets/images/pic_problems.png"),
              primaryButtonAction: "Action",
              title: "Main Title",
              positionedBuilders: [
                (context, designSpec) {
                  final top = designSpec.measuredMetrics.measureVertical(80.0);
                  final size = designSpec.measuredMetrics.measureHeight(480.0);
                  return Positioned(
                      top: top,
                      width: size,
                      height: size,
                      child: Container(
                        color: Colors.red,
                      ));
                }
              ]);
        }),
        _divider,
        buildEntranceItem("title + subtitle", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: S.of(context).settings,
              title: S.of(context).settings,
              subtitle: S.of(context).removeBannerAndPopupAds);
        }),
        _divider,
        buildEntranceItem("title + summary", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title: "Main Title",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("two line title + summary", onTap: () async {
          final String title = S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings;
          final String summary = S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds;
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: S.of(context).settings,
              secondaryButtonText: S.of(context).settings,
              title: title,
              subtitle: title,
              summary: summary);
        }),
        _divider,
        buildEntranceItem("title + subtitle + summary", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title: "Main Title",
              subtitle: "This is a Subtitle",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("2 lines title + long sub title + summary",
            onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title:
                  "This is a Main Title, two lines text Too long to reduce the size",
              subtitle: "This is a sub title, Too long to reduce the size",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("title + subtitle + summary + secondaryButton",
            onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title: "Main Title",
              subtitle: "This is a Subtitle",
              secondaryButtonText: "Secondary Button",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("two title + subtitle + summary + secondaryButton",
            onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title: "This is two lines Main Title",
              subtitle: "This is a Subtitle, This is a Subtitle",
              secondaryButtonText: "Secondary Button",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("title + summary + secondaryButton", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title: "Main Title",
              secondaryButtonText: "Secondary Button",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("two title + summary + secondaryButton",
            onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              title:
                  "This is two lines Main Title，This is two lines Main Title",
              secondaryButtonText: "Secondary Button",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("title + summary + customButton1", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonChild: const GuruButton(
                size: Size(271, 48),
                action: "Action",
                leading: Assets.imagesIcAds,
                tintLeading: true,
              ),
              title: "Main Title",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("title + summary + customButton2", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              primaryButtonInfo: "×100",
              primaryButtonLeading: Assets.imagesIcAds,
              primaryButtonTrailing: Assets.imagesIcCoin,
              title: "Main Title",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("title + summary + customButton3", onTap: () async {
          await GuruPopup.instance.showStandardDialog(
              illustration: Image.asset(Assets.imagesIcIllustration),
              primaryButtonAction: "Action",
              primaryButtonInfo: "×100",
              primaryButtonLeading: Assets.imagesIcAds,
              primaryButtonTrailing: Assets.imagesIcCoin,
              primaryButtonSummary: "Button Summary",
              title: "Main Title",
              summary:
                  "This is a summary, the number of lines between 1-3 lines, according to the language situation adapted.");
        }),
        _divider,
        buildEntranceItem("Confirm Dialog + no title", onTap: () async {
          await GuruPopup.instance.showConfirmDialog(
              summary: "A message should be a short, complete sentence.",
              negativeButtonText: "Negative",
              positiveButtonText: "Positive");
        }),
        _divider,
        buildEntranceItem("Confirm Dialog", onTap: () async {
          await GuruPopup.instance.showConfirmDialog(
            title: 'title',
            summary: "A message should be a short, complete sentence.",
            negativeButtonText: "Negative",
            positiveButtonText: "Positive",
          );
        }),
        _divider,
        buildEntranceItem("Confirm Dialog + adBtn", onTap: () async {
          await GuruPopup.instance.showConfirmDialog(
              title: 'title',
              summary: "A message should be a short, complete sentence.",
              negativeButtonText: "Negative",
              positiveButtonText: "Positive",
              positiveButtonLeading: Assets.imagesIcAds);
        }),
        _divider,
        buildEntranceItem("Two Lines title + Confirm Dialog", onTap: () async {
          await GuruPopup.instance.showConfirmDialog(
            title:
                'This is title, This is title, This is title,This is title,This is title',
            summary:
                "A message should be a short, complete sentence.A message should be a short, complete sentence.A message should be a short, complete sentence.A message should be a short, complete sentence.A message should be a short, complete sentence.",
            negativeButtonText: "Negative",
            positiveButtonText: "Positive",
          );
        }),
        _divider,
        buildEntranceItem("Alert Dialog + no Title", onTap: () async {
          await GuruPopup.instance.showAlertDialog(
              summary: "A message should be a short, complete sentence.",
              negativeButtonText: "Negative",
              positiveButtonText: "Positive");
        }),
        _divider,
        buildEntranceItem("Alert Dialog", onTap: () async {
          await GuruPopup.instance.showAlertDialog(
              title: 'title',
              summary: "A message should be a short, complete sentence.",
              negativeButtonText: "Negative",
              positiveButtonText: "Positive");
        }),
        _divider,
        buildEntranceItem("two lines title + Alert Dialog", onTap: () async {
          await GuruPopup.instance.showAlertDialog(
              title: 'This is title, This is title, This is title',
              summary:
                  "A message should be a short, complete sentence.A message should be a short, complete sentence.",
              negativeButtonText: "Negative",
              positiveButtonText: "Positive");
        }),
        _divider,
        buildEntranceItem("title + summary", onTap: () async {
          await GuruPopup.instance.showSpotlightDialog(
              Image.asset('assets/images/demo.png'),
              primaryButtonAction: "PLAY NOW",
              title: "Classic Solitaire",
              summary: "The Most Addictive Classic Solitairecard vames!");
        }),
        _divider,
        buildEntranceItem("title + summary + customButton3", onTap: () async {
          final String title = S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings;
          final String summary = S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds;
          await GuruPopup.instance.showSpotlightDialog(
              Image.asset('assets/images/demo.png'),
              primaryButtonAction: S.of(context).settings,
              title: title,
              summary: summary,
              secondaryButtonText: S.of(context).settings);
        }),
        _divider,
        buildEntranceItem("title + summary + SecondaryButton", onTap: () async {
          await GuruPopup.instance.showSpotlightDialog(
              Image.asset('assets/images/demo.png'),
              primaryButtonAction: "Action",
              title: "Main Title",
              summary:
                  "This is a summary, the number of lines between 1-3 lines",
              secondaryButtonText: "Secondary Button");
        }),
        _divider,
        buildEntranceItem("summary + SecondaryButton", onTap: () async {
          await GuruPopup.instance.showSpotlightDialog(
              Image.asset('assets/images/demo.png'),
              primaryButtonAction: "Action",
              summary:
                  "This is a summary, the number of lines between 1-3 lines",
              secondaryButtonText: "Secondary Button");
        }),
        _divider,
        buildEntranceItem("summary", onTap: () async {
          await GuruPopup.instance.showSpotlightDialog(
              Image.asset('assets/images/demo.png'),
              primaryButtonAction: "Action",
              summary:
                  "This is a summary, the number of lines between 1-3 lines");
        }),
        _divider,
        buildEntranceItem("extend dialog", onTap: () async {
          final String title = S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings +
              S.of(context).settings;
          final String summary = S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds +
              S.of(context).removeBannerAndPopupAds;
          await GuruPopup.instance
              .showExtendDialog(Image.asset('assets/images/pic.png'),
                  title: title,
                  primaryButtonAction: "Action",
                  summary: summary,
                  contentBackground: const BoxDecoration(
                      gradient: LinearGradient(
                    colors: [
                      Color.fromRGBO(45, 24, 77, 0),
                      Color.fromRGBO(45, 24, 77, 1),
                      Color.fromRGBO(52, 28, 89, 1),
                      Color.fromRGBO(45, 24, 77, 1)
                    ],
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                  )));
        }),
        _divider,
        buildEntranceItem("paused dialog", onTap: () async {
          // await GuruPopup.instance.showPausedDialog(
          //     items: [
          //       SwitchSetting(
          //         enabledTitle: "Sound Effects",
          //         leading: "assets/images/ic_sound.png",
          //         settingData: UiSettings.instance.checkedTileBol,
          //         onChanged: ((value) {
          //           UiSettings.instance.checkedTileBol.set(value);
          //           // controller.changeValue(value);
          //         })
          //       )
          //     ]
          //     );
        }),
        _divider,
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GuruDemoPage(
      child: Scaffold(
        backgroundColor: const Color(0xFF121212),
        appBar: GuruAppBar(
          title: 'Guru Standard Dialog',
          backgroundColor: Colors.transparent,
          size: GuruAppBarSize.small,
          leadingType: LeadingType.back,
          actions: [
            IconButton(
              padding: const EdgeInsets.all(0),
              splashRadius:
                  GuruThemeDesignSpec.get().appBarDesignSpec.actionIconSize,
              icon: ImageIcon(
                const AssetImage("assets/images/Ic_Award.png"),
                size: GuruThemeDesignSpec.get().appBarDesignSpec.actionIconSize,
                color: Colors.white,
              ),
              onPressed: () {},
            )
          ],
        ),
        body: Stack(children: [
          SingleChildScrollView(
              child: Align(
                  alignment: Alignment.topCenter, child: buildBody(context))),
          ConsoleButton()
        ]),
      ),
    );
  }
}
