import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/index.dart';
import 'package:lottie/lottie.dart';
import 'package:mohem_flutter_app/api/dashboard_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/lottie_consts.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/main.dart';
import 'package:mohem_flutter_app/models/dashboard/menu_entries.dart';
import 'package:mohem_flutter_app/models/itg/advertisement.dart' as ads;
import 'package:mohem_flutter_app/provider/dashboard_provider_model.dart';
import 'package:mohem_flutter_app/ui/my_attendance/services_menu_list_screen.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/my_video_progress_indicator.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:video_player/video_player.dart';

class ITGAdsScreen extends StatefulWidget {
  const ITGAdsScreen({Key? key}) : super(key: key);

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

class _ITGAdsScreenState extends State<ITGAdsScreen> {
  late Future<VideoPlayerController> _futureController;
  VideoPlayerController? _controller;
  bool skip = false;
  bool isVideo = false;
  bool isImage = false;
  bool isAudio = false;
  String ext = '';

  bool isTextURL = false;

  // late File imageFile;
  late String imageUrl;
  ads.Advertisement? advertisementData;
  dynamic data;
  String? masterID;
  int videoDuration = 0;

  bool hasTimerEndedBool = false;

  final ValueNotifier<bool> hasTimerEnded = ValueNotifier(false);

  late CountdownTimerController timerController;

  void checkFileType() {
    if (advertisementData!.viewAttachFileColl!.first.contentType!.toLowerCase() == "text") {
      isTextURL = true;
    } else {
      String? rFile = advertisementData!.viewAttachFileColl!.first.base64String;
      try {
        rFile = advertisementData!.viewAttachFileColl!.where((element) => element.languageId == AppState().getLanguageID(context)).toList().first.base64String;
      } catch (ex) {}

      String? rFileExt = advertisementData!.viewAttachFileColl!.first.fileName;
      ext = "." + rFileExt!.split(".").last.toLowerCase();
      if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") {
        //  await processImage();
        imageUrl = rFile!;
        isImage = true;
      } else {
        if (ext == ".aac") {
          isAudio = true;
        }
        isVideo = true;
        _futureController = createVideoPlayer(rFile!);
      }
    }

    // advertisementData?.actionButtonsColl!.forEach((element) {
    advertisementData?.actionButtonsColl!.removeWhere((element1) => element1.actionButtonId == advertisementData?.skipButtonId);
    //});

    // setState(() {});
  }

  void changeTimer(bool isTimerEnd) {
    hasTimerEndedBool = isTimerEnd;
    hasTimerEnded.value = !hasTimerEnded.value;
  }

  void onTimerEnd() {
    changeTimer(true);
    timerController.disposeTimer();
    timerController.dispose();
  }

  //
  // Future processImage(String encodedBytes) async {
  //   try {
  //     Uint8List decodedBytes = base64Decode(encodedBytes.split("base64,").last);
  //     Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); // 1
  //     imageFile = Io.File("${appDocumentsDirectory.path}/addImage$ext");
  //     imageFile.writeAsBytesSync(decodedBytes);
  //   } catch (e) {
  //     logger.d(e);
  //   }
  // }

  Future<VideoPlayerController> createVideoPlayer(String encodedBytes) async {
    try {
      VideoPlayerController controller = VideoPlayerController.network(advertisementData!.viewAttachFileColl!.first.base64String!);
      await controller.initialize();
      await controller.play();
      await controller.setVolume(1.0);
      await controller.setLooping(false);
      return controller;
    } catch (e) {
      print(e);
      return VideoPlayerController.network("https://apimohemmweb.cloudsolutions.com.sa/ErmAttachment/compressedvideo.mp4");
    }
  }

  void initTimer() {
    Future.delayed(const Duration(seconds: 5), () {
      skip = true;
      // setState(() {});
    });
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    data = ModalRoute.of(context)!.settings.arguments;
    advertisementData ??= data["advertisement"] as ads.Advertisement;
    masterID ??= data["masterId"];
    if (advertisementData != null) {
      checkFileType();
      videoDuration = advertisementData?.durationInSeconds ?? 0;
      timerController = CountdownTimerController(endTime: DateTime.now().millisecondsSinceEpoch + 1000 * videoDuration, onEnd: onTimerEnd);
    }
    return Scaffold(
      backgroundColor: isTextURL ? Colors.white : Colors.black,
      appBar: isTextURL
          ? AppBar(
              centerTitle: true,
              automaticallyImplyLeading: false,
              backgroundColor: Colors.white,
              title: (AppState().isArabic(context) ? advertisementData!.advertisementTitleAr! : advertisementData!.advertisementTitle!).toText24(color: MyColors.darkTextColor, isBold: true),

              //advertisementData!.viewAttachFileColl!.first.base64String!.toText24(color: MyColors.darkTextColor, isBold: true),
            )
          : null,
      body: Stack(
        children: [
          if (isVideo)
            FutureBuilder(
              future: _futureController,
              builder: (BuildContext context, AsyncSnapshot<Object?> snapshot) {
                if (snapshot.connectionState == ConnectionState.done && snapshot.data != null) {
                  _controller = snapshot.data as VideoPlayerController;
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Center(
                        child: isAudio
                            ? Lottie.asset(MyLottieConsts.audioPlaybackLottie)
                            : AspectRatio(
                                aspectRatio: _controller!.value.aspectRatio,
                                child: Stack(
                                  alignment: Alignment.bottomCenter,
                                  children: [
                                    VideoPlayer(_controller!),
                                    if (advertisementData?.isOptional ?? false)
                                      Positioned(
                                        top: 4,
                                        right: 8,
                                        child: Container(
                                          padding: const EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
                                          decoration: BoxDecoration(color: Colors.white.withOpacity(0.5), borderRadius: BorderRadius.circular(30)),
                                          child: Row(
                                            mainAxisSize: MainAxisSize.min,
                                            children: [
                                              Text(AppState().isArabic(context) ? "يتخطى" : "Skip"),
                                              Icon(Icons.skip_next),
                                            ],
                                          ),
                                        ).onPress(() {
                                          Navigator.pop(context);
                                          DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Skip").then((value) {
                                            logger.d(value);
                                          });
                                        }),
                                      ),
                                  ],
                                ),
                              ),
                      ),
                      30.height,
                      CountdownTimer(
                        controller: timerController,
                        endTime: DateTime.now().millisecondsSinceEpoch + 1000 * videoDuration,
                        endWidget: "00:00:00".toText16(color: Colors.white, isBold: true),
                        textStyle: const TextStyle(color: Colors.white, fontSize: 16, letterSpacing: -0.48, fontWeight: FontWeight.bold),
                      ),
                      50.height,
                      // if (advertisementData?.isOptional ?? false)
                      //   DefaultButton(AppState().isArabic(context) ? "يتخطى" : "Skip", () async {
                      //     Navigator.pop(context);
                      //     DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Skip").then((value) {
                      //       logger.d(value);
                      //     });
                      //   }).paddingOnly(left: 60.0, right: 60.0, top: 8, bottom: 8),
                      ValueListenableBuilder<bool>(
                        valueListenable: hasTimerEnded,
                        builder: (context, val, child) {
                          // if (hasTimerEndedBool) {
                          return ListView.builder(
                            padding: EdgeInsets.zero,
                            itemCount: advertisementData?.actionButtonsColl!.length,
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            itemBuilder: (context, index) {
                              String? btnText = AppState().isArabic(context) ? advertisementData?.actionButtonsColl![index].btnTextAr : advertisementData?.actionButtonsColl![index].btnTextEn;

                              // bool isAcknowledge = advertisementData?.actionButtonsColl![index].actionButtonId == 11;
                              bool isAcknowledge = advertisementData?.actionButtonsColl![index].actionButtonId == 17;
                              if (isAcknowledge) {
                                return Stack(
                                  alignment: Alignment.center,
                                  children: [
                                    MyVideoProgressIndicator(
                                      _controller!,
                                      allowScrubbing: false,
                                      //padding: EdgeInsets.only(left: 60, right: 60),
                                    ),
                                    DefaultButton(
                                      btnText!,
                                      hasTimerEndedBool
                                          ? () async {
                                              Navigator.pop(context);
                                              DashboardApiClient()
                                                  .setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, advertisementData?.actionButtonsColl![index].actionValue)
                                                  .then((value) {
                                                logger.d(value);
                                              });
                                            }
                                          : null,
                                      disabledColor: Colors.transparent,
                                      textColor: !hasTimerEndedBool ? Colors.white.withOpacity(.5) : Colors.white,
                                    ).paddingOnly(left: 60.0, right: 60.0, top: 8, bottom: 8),
                                  ],
                                );
                              }
                              return DefaultButton(
                                btnText!,
                                () async {
                                  Navigator.pop(context);
                                  DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, advertisementData?.actionButtonsColl![index].actionValue).then((value) {
                                    logger.d(value);
                                  });
                                },
                              ).paddingOnly(left: 60.0, right: 60.0, top: 8, bottom: 8);
                            },
                            // gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                            //   crossAxisCount: 1,
                            //   childAspectRatio: (7.0),
                            // ),
                          );
                          //   Row(
                          //   mainAxisAlignment: MainAxisAlignment.center,
                          //   children: [
                          //     Container(padding: const EdgeInsets.all(16), decoration: Utils.containerRadius(MyColors.white, 10), child: const Icon(Icons.thumb_up, color: MyColors.gradiantEndColor))
                          //         .onPress(() {
                          //       try {
                          //         Navigator.pop(context);
                          //         DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Like").then((value) {
                          //           logger.d(value);
                          //         });
                          //       } catch (ex) {
                          //         logger.wtf(ex);
                          //         Utils.handleException(ex, context, null);
                          //       }
                          //     }),
                          //     20.width,
                          //     Container(
                          //             padding: const EdgeInsets.all(16), decoration: Utils.containerRadius(MyColors.white, 10), child: const Icon(Icons.thumb_down, color: MyColors.gradiantEndColor))
                          //         .onPress(() {
                          //       try {
                          //         Navigator.pop(context);
                          //         DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Dislike").then((value) {
                          //           logger.d(value);
                          //         });
                          //       } catch (ex) {
                          //         logger.wtf(ex);
                          //         Utils.handleException(ex, context, null);
                          //       }
                          //     }),
                          //   ],
                          // );
                          // } else {
                          //   return Container();
                          // }
                        },
                      ),
                      20.height,
                      // if (advertisementData?.isOptional ?? false)
                      // GridView.builder(
                      //   padding: EdgeInsets.zero,
                      //   itemCount: advertisementData?.actionButtonsColl!.length,
                      //   shrinkWrap: true,
                      //   physics: const NeverScrollableScrollPhysics(),
                      //   itemBuilder: (context, index) {
                      //     String? btnText = AppState().isArabic(context) ? advertisementData?.actionButtonsColl![index].btnTextAr : advertisementData?.actionButtonsColl![index].btnTextEn;
                      //     return DefaultButton(btnText!, () async {
                      //       Navigator.pop(context);
                      //       DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, advertisementData?.actionButtonsColl![index].actionValue).then((value) {
                      //         logger.d(value);
                      //       });
                      //     }).paddingAll(8);
                      //   },
                      //   gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      //     crossAxisCount: 2,
                      //     childAspectRatio: (4.0),
                      //   ),
                      // )
                      // DefaultButton(AppState().isArabic(context) ? advertisementData?.skipButtonTextAr ?? "Skip" : advertisementData?.skipButtonTextEn ?? "Skip", () async {
                      //   Navigator.pop(context);
                      //   DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Skip").then((value) {
                      //     logger.d(value);
                      //   });
                      // }).paddingOnly(left: 100, right: 100)
                    ],
                  );
                } else {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }
              },
            ),
          if (isImage)
            SingleChildScrollView(
              padding: const EdgeInsets.only(top: 60, left: 12, right: 12),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.network(imageUrl),
                  50.height,
                  Container(padding: const EdgeInsets.all(16), decoration: Utils.containerRadius(MyColors.white, 10), child: const Icon(Icons.thumb_up, color: MyColors.gradiantEndColor)).onPress(
                    () {
                      try {
                        Navigator.pop(context);
                        DashboardApiClient().setAdvertisementViewed(masterID!, advertisementData!.advertisementId!, "Like").then((value) {
                          logger.d(value);
                        });
                      } catch (ex) {
                        logger.wtf(ex);
                        Utils.handleException(ex, context, null);
                      }
                    },
                  ),
                ],
              ),
            ),
          if (isTextURL)
            SingleChildScrollView(
              padding: const EdgeInsets.all(21),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  (AppState().isArabic(context) ? advertisementData!.viewAttachFileColl![0].base64String! : advertisementData!.viewAttachFileColl![1].base64String!)
                      .toText16(color: MyColors.darkTextColor),
                  24.height,
                  ListView.separated(
                    itemCount: advertisementData!.actionButtonsColl?.length ?? 0,
                    separatorBuilder: (cxt, index) => 16.height,
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    padding: EdgeInsets.zero,
                    itemBuilder: (cxt, index) => DefaultButton(
                      AppState().isArabic(context) ? advertisementData!.actionButtonsColl![index].btnTextAr : advertisementData!.actionButtonsColl![index].btnTextEn,
                      () async {
                        if (advertisementData!.actionButtonsColl![index].actionValue.toLowerCase() == "skip") {
                          Navigator.pop(context);
                        } else {
                          if (advertisementData!.actionButtonsColl![index].actionType!.toLowerCase() == 'redirect' && advertisementData!.actionButtonsColl![index].actionValue!.toLowerCase() ==
                              'external') {
                            Uri uri = Uri.parse(advertisementData!.actionButtonsColl![index].url!);
                            await launchUrl(uri, mode: LaunchMode.externalApplication).catchError((err) {
                              // print(err);
                              Utils.showToast('Could not launch');
                            });
                            Navigator.pop(context);
                          } else if (advertisementData!.actionButtonsColl![index].actionType!.toLowerCase() == 'redirect' && advertisementData!.actionButtonsColl![index].actionValue!.toLowerCase() ==
                              'internal') {

                              await Navigator.pushNamed(context, advertisementData!.actionButtonsColl![index].url!);

                          } else if(advertisementData!.actionButtonsColl![index].actionType!.toLowerCase() == 'eit' && advertisementData!.actionButtonsColl![index].actionValue!.toLowerCase() ==
                              'internal'){
                            var pro = Provider.of<DashboardProviderModel>(context, listen: false);
                            List<GetMenuEntriesList> _menuList =
                                pro.getMenuEntriesList?.where((element) => element.parentMenuName == advertisementData!.actionButtonsColl![index].url && (element.menuEntryType == "FUNCTION" || element.menuEntryType == "MENU")).toList() ?? [];
                            Navigator.pushNamed(context, AppRoutes.servicesMenuListScreen, arguments: ServicesMenuListScreenParams(advertisementData!.advertisementTitle!,  _menuList));

                          }
                          else{
                            Uri uri = Uri.parse(advertisementData!.actionButtonsColl![index].iconOrImage);
                            // if (await canLaunchUrl(uri)) {
                            await launchUrl(uri, mode: LaunchMode.externalApplication).catchError((err) {
                              // print(err);
                              Utils.showToast('Could not launch');
                            });
                            Navigator.pop(context);
                            // } else {
                            //   Utils.showToast('Could not launch');
                            // }
                          }
                        }
                      },
                    ),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }
}
