import 'dart:convert';
import 'dart:io' as Io;
import 'dart:io';
import 'dart:typed_data';

import 'package:easy_localization/easy_localization.dart';
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/file_process.dart';
import 'package:mohem_flutter_app/classes/lottie_consts.dart';
import 'package:mohem_flutter_app/classes/utils.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/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/main.dart';
import 'package:mohem_flutter_app/models/itg/advertisement.dart' as ads;
import 'package:mohem_flutter_app/models/marathon/tutorial_notification_model.dart';
import 'package:mohem_flutter_app/ui/chat/common.dart';
import 'package:mohem_flutter_app/ui/marathon/marathon_provider.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
import 'package:video_player/video_player.dart';

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

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

class _MarathonTutorialViewerScreenState extends State<MarathonTutorialViewerScreen> {
  late Future<VideoPlayerController> _futureController;
  VideoPlayerController? _controller;
  bool skip = false;

  bool isVideo = false;
  bool isImage = false;
  bool isAudio = false;
  bool isPdf = false;

  String link = "";

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

  ValueNotifier<int> videoLength = ValueNotifier(0);

  void checkFileTypes(String link) {
    ext = "." + link.split(".").last.toLowerCase();
    if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") {
      isImage = true;
    } else if (ext == ".pdf") {
      isPdf = true;
    } else {
      if (ext == ".aac") {
        isAudio = true;
      }
      isVideo = true;
      _futureController = createVideoPlayer(link);
    }

    setState(() {});
  }

  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 link) async {
    try {
      VideoPlayerController controller = VideoPlayerController.networkUrl(Uri.parse(link));
      await controller.initialize();
      await controller.play();
      await controller.setVolume(1.0);
      await controller.setLooping(false);
      controller.addListener(() {
        controller.position.then((value) {
          videoLength.value = value!.inMilliseconds;
          // if(controller.value.isCompleted) {
          //   videoLength.value = 0;
          // }
        });
      });
      return controller;
    } catch (e) {
      print(e);
      return VideoPlayerController.network("https://apimohemmweb.cloudsolutions.com.sa/ErmAttachment/compressedvideo.mp4");
    }
  }

  bool showControls = false;

  void showVideoControls() {
    showControls = !showControls;
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
  }

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

  TutorialNotificationModel? tutorial;

  @override
  Widget build(BuildContext context) {
    if (tutorial == null) {
      tutorial ??= context.read<MarathonProvider>().tutorial;
      link = tutorial!.filePath!;
      checkFileTypes(link);
    }

    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBarWidget(context, title: tutorial!.fileName!, showHomeButton: false),
      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!),
                                    AnimatedContainer(
                                      duration: const Duration(milliseconds: 250),
                                      color: Colors.black.withOpacity(showControls ? .4 : .0),
                                      child: AnimatedOpacity(
                                        opacity: showControls ? 1 : 0,
                                        duration: const Duration(milliseconds: 250),
                                        child: Column(
                                          children: [
                                            Icon(_controller!.value.isPlaying ? Icons.pause : Icons.play_arrow, color: Colors.white)
                                                .onPress(() {
                                                  if (!showControls) {
                                                    showVideoControls();
                                                    return;
                                                  }

                                                  if (_controller!.value.isPlaying) {
                                                    _controller!.pause();
                                                  }
                                                  // else if (_controller!.value.isCompleted) {
                                                  //   videoLength.value = 0;
                                                  //   _controller!.play();
                                                  // }
                                                  else {
                                                    _controller!.play();
                                                  }
                                                  setState(() {});
                                                })
                                                .center
                                                .expanded,
                                            ValueListenableBuilder<int>(
                                                valueListenable: videoLength,
                                                builder: (context, val, child) {
                                                  return SeekBar(
                                                    duration: _controller!.value.duration,
                                                    position: Duration(milliseconds: val),
                                                    bufferedPosition: Duration(),
                                                    onChanged: (duration) {
                                                      _controller!.seekTo(duration);
                                                    },
                                                  );
                                                }),
                                          ],
                                        ),
                                      ),
                                    ).onPress(() {
                                      showVideoControls();
                                    }),
                                  ],
                                ),
                              ),
                      ),
                      30.height,
                    ],
                  );
                } else {
                  return const Center(
                    child: CircularProgressIndicator(
                      color: Colors.white,
                    ),
                  );
                }
              },
            ),
          if (isImage) Image.network(link).center,
          if (isPdf)
            DefaultButton(LocaleKeys.open.tr(), () async {
              try {
                Utils.showLoading(context);
                await FileProcess.downloadFileFromUrl(link, tutorial!.fileName!).then((path) {
                  Utils.hideLoading(context);
                  FileProcess.openFile(path, isFullPath: true);
                });
              } catch (ex) {
                Utils.hideLoading(context);
                Utils.handleException(ex, context, null);
              }
            }).paddingAll(21).center
        ],
      ),
    );
  }
}
