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

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

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

class _VideoPlayerScreenState extends State<MyHomePage> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;
  InAppWebViewController? webViewController;
  //double _scale = 1.0;
  String? url = 'http://192.168.65.111:8100/restream';
  String? urlData =
      'https://www.flutterdecode.com/play-video-from-url-in-flutter';

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

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
    _chewieController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // leading: IconButton(
        //   icon: const Icon(Icons.arrow_back),
        //   onPressed: () async {
        //     Navigator.pop(context, true);
        //   },
        // ),
        title: const Text(
          'Video Playing',
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: SizedBox(
        height: MediaQuery.of(context).size.height,
        child: InAppWebView(
          initialUrlRequest: URLRequest(
            url: Uri.parse("http://192.168.65.111:8100"),
          ),
          onWebViewCreated: (controller) {
            webViewController = controller;
          },
          initialOptions: InAppWebViewGroupOptions(
            crossPlatform: InAppWebViewOptions(),
            android: AndroidInAppWebViewOptions(
              supportMultipleWindows: true,
            ),
            ios: IOSInAppWebViewOptions(
              allowsInlineMediaPlayback: true,
            ),
          ),
        ),
      ),
    );
  }
}
// class VideoStream extends StatefulWidget {
//   const VideoStream({Key? key}) : super(key: key);

//   @override
//   State<VideoStream> createState() => _VideoStreamState();
// }

// class _VideoStreamState extends State<VideoStream> {
//   final WebSocket _socket = WebSocket('http://192.168.65.111:8100/restream');
//   bool _isConnected = false;
  
//   void connect(BuildContext context) async {
//     _socket.connect();
//     setState(() {
//       _isConnected = true;
//     });
//   }

//   void disconnect() {
//     _socket.disconnect();
//     setState(() {
//       _isConnected = false;
//     });
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: const Text("Live Video"),
//       ),
//       body: Padding(
//         padding: const EdgeInsets.all(20.0),
//         child: Center(
//           child: Column(
//             children: [
//               Row(
//                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
//                 children: [
//                   ElevatedButton(
//                     onPressed: () => connect(context),
//                     //style: buttonStyle,
//                     child: const Text("Connect"),
//                   ),
//                   ElevatedButton(
//                     onPressed: disconnect,
//                     //style: buttonStyle,
//                     child: const Text("Disconnect"),
//                   ),
//                 ],
//               ),
//               const SizedBox(
//                 height: 50.0,
//               ),
//               _isConnected
//                   ? StreamBuilder(
//                       stream: _socket.stream,
//                       builder: (context, snapshot) {
//                         if (!snapshot.hasData) {
//                           return const CircularProgressIndicator();
//                         }

//                         if (snapshot.connectionState == ConnectionState.done) {
//                           return const Center(
//                             child: Text("Connection Closed !"),
//                           );
//                         }
//                         //? Working for single frames
//                         return Image.memory(
//                           Uint8List.fromList(
//                             base64Decode(
//                               (snapshot.data.toString()),
//                             ),
//                           ),
//                           gaplessPlayback: true,
//                           excludeFromSemantics: true,
//                         );
//                       },
//                     )
//                   : const Text("Initiate Connection")
//             ],
//           ),
//         ),
//       ),
//     );
//   }
// }