import 'package:flutter/material.dart';
import 'package:storemonitor/Screens/Constants.dart';
import 'package:video_player/video_player.dart';

class VideoApp extends StatefulWidget {
  final String message;
  final String id;
  final String comment;

  const VideoApp({Key? key,
      required this.message,
      required this.id,
      required this.comment}): super(key: key);
      
  @override
   _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  late VideoPlayerController _controller;
  var videoUrl = '';
  var isdata = '';
  var id = '';

  @override
  void initState() {
    super.initState();
    isdata = widget.message;

    //List<String> parts = isdata.split('/');

// // Find the part that contains the file ID
//     String? fileId;
//     for (String part in parts) {
//       if (part.startsWith('d/')) {
//         fileId = part.substring(2);
//         print('if Condition:  $fileId');
//         break;
//       }
//     }

//     if (fileId != null) {
//       print('File ID: $fileId');
//     } else {
//       print('File ID not found in the link');
//     }
    String googleDriveFileUrl = isdata;

// Split the URL by '/'
    List<String> urlParts = googleDriveFileUrl.split('/');
    int indexOfD = urlParts.indexOf('d');

// The file ID is the part after 'd'
    String fileId = urlParts.elementAt(indexOfD + 1);   
//print('Testing : $urlParts[5]');
// The fileId is the first part after the last '/'
    //String fileId = urlParts.last;
    print('File ID: $fileId');
    videoUrl = 'https://drive.google.com/uc?export=view&id=$fileId';

    print('VideoLink : $videoUrl');
    _controller = VideoPlayerController.networkUrl(Uri.parse(videoUrl))
      ..initialize().then((_) {
        setState(() {
          _controller.seekTo(const Duration(microseconds: 1));
          _controller.play();
          true;
        });
      });
  }
  // void _initializeVideoPlayer() async {
  //   videoUrl =
  //       'https://drive.google.com/uc?export=view&id=1QVUJ34BKRilfchA_0ppmM3gFIPvYIl7i';
  //   try {
  //     await _controller.initialize();
  //   } catch (e) {
  //     print('Video initialization error: $e');
  //     // Retry initialization after a delay (e.g., 1 second)
  //     Future.delayed(Duration(seconds: 1), () {
  //       _initializeVideoPlayer();
  //     });
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Video Playing ',
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: Center(
        child: _controller.value.isInitialized
            ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
            : Container(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _controller.value.isPlaying
                ? _controller.pause()
                : _controller.play();
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }

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