import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:example/route/router.dart';
import 'package:guru_utils/router/router.dart';
import 'package:guru_assistant/ui/ads_debug_page.dart' as Assistant;

import 'ads_debug_controller.dart';

class AdsDebugPage extends GetWidget<AdsDebugController> {
  const AdsDebugPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final controller = Get.find<AdsDebugController>();
    final model = Get.find<AdsDebugController>().model;

    // SettingItem _buildGdprGroup() {
    //   if (controller.forceGdprTypeSubject.value == 0) {
    //     return GroupSetting(
    //       title: "AdMob GDPR",
    //       items: [
    //         EntranceSetting(title: "Admob Consent Debug Geography", onTap: () async {
    //           final consentDebugGeographyIndex = controller.consentDebugGeographySubject.value;
    //           await GuruPopup.instance.showCustomDialog(child: RadioGroupWidget(consentDebugGeographyIndex, AdsDebugController.geographyName.map((t) => Text(
    //                 t,
    //                 style: const TextStyle(fontSize: 16, color: Colors.black),
    //               ))
    //           .toList(), onChanged: (idx) {
    //             controller.setForceGdprType(idx);
    //           }));
    //         }), 
    //         EntranceSetting(title: "Admob Consent Test Device Id", onTap: () async {
              
    //         }),
    //         EntranceSetting(title: "Modify Latest Shown Funding Choices Ts", summary: "", onTap: () async {
    //           final dt = controller.latestShownFundingChoicesTime.value;
    //           final selectedDate = await showDatePicker(
    //             context: Get.context!,
    //             initialDate: dt?.isAfter(DateTime(2022, 6, 10)) == true
    //                 ? dt ?? DateTime.now()
    //                 : DateTime.now(),
    //             firstDate: DateTime(2022, 6, 10),
    //             lastDate: DateTime.now().add(const Duration(days: 365)));

    //           final selectedTime = await showTimePicker(
    //             context: Get.context!,
    //             initialTime:
    //             dt != null ? TimeOfDay(hour: dt.hour, minute: dt.minute) : TimeOfDay.now());

    //           final newDateTime = DateTime(selectedDate?.year ?? dt.year, selectedDate?.month ?? dt.month,
    //               selectedDate?.day ?? dt.day, selectedTime?.hour ?? dt.hour, selectedTime?.minute ?? dt.minute);
              
    //           controller.setLatestShownFundingChoicesTimestamp(newDateTime);
    //         })
    //       ]
    //     );
    //   } else {
    //     return GroupSetting(
    //       title: "GURU GDPR",
    //       items: [
    //         // SwitchSetting(
    //         //   enabledTitle: "Force Show Guru Gdpr",
    //         //   settingData: AdsDebugSettings.instance.forceGdpr,
    //         //   onChanged: (value) {
    //         //     AdsDebugSettings.instance.forceGdpr.set(value);
    //         // }),
    //       ]
    //     );
    //   }
    // }

    return Assistant.AdsDebugPage(onEnterAdsTest: () {
      RouteCenter.instance.openPath(Routes.testAds.path());
    });
    // GuruSettingsPage(
    //   items: [
    //     GroupSetting(
    //       title: "MAX SUPPORT",
    //       items: [
    //         EntranceSetting(title: "MAX Mediation Debugger", onTap: () async {
    //           GuruApplovinFlutter.instance.openDebugger();
    //         }), 
    //         EntranceSetting(title: "GURU Mediation Debugger", onTap: () async {
    //           RouteCenter.instance.openPath(Routes.testAds.path());
    //         }),
    //         SwitchSetting(
    //           enabledTitle: "Fake Interstitial Ads",
    //           settingData: AdsDebugSettings.instance.fakeInterstitialAds,
    //           onChanged: (value) {
    //             AdsDebugSettings.instance.fakeInterstitialAds.set(value);
    //         }),
    //         SwitchSetting(
    //           enabledTitle: "Fake Rewarded Ads",
    //           settingData: AdsDebugSettings.instance.fakeRewardedAds,
    //           onChanged: (value) {
    //             AdsDebugSettings.instance.fakeRewardedAds.set(value);
    //         }),
    //       ]
    //     ),
    //     GroupSetting(
    //       title: "GDPR",
    //       items: [
    //         EntranceSetting(title: "Reset Gdpr", onTap: () async {
    //           controller.resetGdpr();
    //         }), 
    //         EntranceSetting(title: "GDPR TYPE", onTap: () async {
    //           final forceGdprTypeIndex = controller.forceGdprTypeSubject.value;
    //           await GuruPopup.instance.showCustomDialog(child: RadioGroupWidget(forceGdprTypeIndex, AdsDebugController.gdprType.map((t) => Text(
    //                 t,
    //                 style: const TextStyle(fontSize: 16, color: Colors.black),
    //               ))
    //           .toList(), onChanged: (i) {
    //             controller.setForceGdprType(i);
    //           }));
    //         })
    //       ]
    //     ),
    //     _buildGdprGroup(),
    //     GroupSetting(
    //       title: "PUBMATIC",
    //       items: [
    //         SwitchSetting(
    //           enabledTitle: "Force Pubmatic",
    //           settingData: AdsDebugSettings.instance.forcePubmatic,
    //           onChanged: (value) {
    //             AdsDebugSettings.instance.forcePubmatic.set(value);
    //         }),
    //         EntranceSetting(title: "Reset Pubmatic", onTap: () async {
    //           controller.resetPubmatic();
    //         })
    //       ]
    //     ),
    //     GroupSetting(
    //       title: "DATA",
    //       items: [
    //         EntranceSetting(title: "Ads Remote Config", onTap: () async {
    //           GuruPopup.instance.showKeyValueDialog(AdsManager.instance.adsConfig.dump().entries.toList());
    //         }),
    //         EntranceSetting(title: "Ads Properties", onTap: () async {

    //         })
    //       ]
    //     ),
    //     GroupSetting(
    //       title: "IDS",
    //       items: [
    //         EntranceSetting(title: "Banner AdUnitId", summary: profile.bannerId.id, onTap: () async {
    //           Clipboard.setData(ClipboardData(text: profile.bannerId.id));
    //           Log.d("saveTokenToClipboard:${profile.bannerId.id}");
    //           GuruPopup.instance.showCommonToast("Copied Banner AdUnitId to your clipboard");
    //         }),
    //         EntranceSetting(title: "Interstitial AdUnitId", summary: profile.interstitialId.id, onTap: () async {
    //           Clipboard.setData(ClipboardData(text: profile.interstitialId.id));
    //           Log.d("saveTokenToClipboard:${profile.interstitialId.id}");
    //           GuruPopup.instance.showCommonToast("Copied Interstitial AdUnitId to your clipboard");
    //         }),
    //         EntranceSetting(title: "Rewarded AdUnitId", summary: profile.rewardsId.id, onTap: () async {
    //           Clipboard.setData(ClipboardData(text: profile.rewardsId.id));
    //           Log.d("saveTokenToClipboard:${profile.rewardsId.id}");
    //           GuruPopup.instance.showCommonToast("Copied Rewarded AdUnitId to your clipboard");
    //         })
    //       ]
    //     )
    //   ]
    // );
  }
}





class RadioGroupWidget extends StatefulWidget {
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final CrossAxisAlignment crossAxisAlignment;

  final TextDirection? textDirection;
  final Axis direction;

  final int initialIndex;
  final List<Widget> items;
  final void Function(int)? onChanged;

  RadioGroupWidget(this.initialIndex, this.items,
      {this.mainAxisAlignment = MainAxisAlignment.start,
      this.mainAxisSize = MainAxisSize.min,
      this.crossAxisAlignment = CrossAxisAlignment.start,
      this.direction = Axis.vertical,
      this.textDirection,
      this.onChanged});

  @override
  State<StatefulWidget> createState() {
    return RadioGroupState();
  }
}

class RadioGroupState extends State<RadioGroupWidget> {
  int groupValue = 0;

  final List<Widget> _items = [];

  void _refresh() {
    _items.clear();

    for (int index = 0; index < widget.items.length; ++index) {
      _items.add(Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Radio<int>(
            value: index,
            groupValue: groupValue,
            onChanged: (v) {
              setState(() {
                groupValue = v ?? 0;
              });
              widget.onChanged?.call(v ?? 0);
            },
          ),
          InkWell(
            onTap: () {
              setState(() {
                groupValue = index;
              });
              widget.onChanged?.call(index);
            },
            child: widget.items[index],
          )
        ],
      ));
    }
  }

  @override
  void initState() {
    super.initState();
    groupValue = widget.initialIndex;
  }

  @override
  void didUpdateWidget(covariant RadioGroupWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    _refresh();
    if (widget.direction == Axis.vertical) {
      return Column(
        mainAxisAlignment: widget.mainAxisAlignment,
        mainAxisSize: widget.mainAxisSize,
        crossAxisAlignment: widget.crossAxisAlignment,
        children: _items,
      );
    } else {
      return Row(
        mainAxisAlignment: widget.mainAxisAlignment,
        mainAxisSize: widget.mainAxisSize,
        crossAxisAlignment: widget.crossAxisAlignment,
        children: _items,
      );
    }
  }
}