import 'package:fis_measure/view/player/control_board/control_board.dart';
import 'package:fis_measure/view/player/controller.dart';
import 'package:fis_measure/view/player/player.dart';
import 'package:flutter/material.dart';

import 'package:fis_vid/data_host/data_host.dart';

class VidPlayerMobilePage extends StatefulWidget {
  const VidPlayerMobilePage(this.url, {Key? key}) : super(key: key);

  final String url;

  @override
  State<StatefulWidget> createState() => _VidPlayerMobilePageState();
}

class _VidPlayerMobilePageState extends State<VidPlayerMobilePage> {
  late final _dataHost = VidDataHost(widget.url);
  late final _playerController = VidPlayerController(dataHost: _dataHost);

  @override
  Widget build(BuildContext context) {
    // _playerController.load().then((value) {
    //   _playerController.play();
    // });
    return FutureBuilder(
      future: _playerController.load(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (_playerController.canPlay) {
            _playerController.play();
            return buildPage(context);
          } else {
            return const Material(child: Center(child: Text("Load fila")));
          }
        } else {
          return const Material(
              child: Center(child: CircularProgressIndicator()));
        }
      },
    );
  }

  Widget buildPage(BuildContext context) {
    // const borderSide = BorderSide(color: Colors.grey);
    final size = MediaQuery.of(context).size;
    final w = size.width;
    final h = w / 16 * 9;
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            VidPlayer(
              _playerController,
              width: w,
              height: h,
            ),
            SizedBox(
              height: 160,
              child: _playerController.totalFramesCount > 1
                  ? VidPlayerControlBoard(_playerController)
                  : Container(),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _playerController.dispose();
    _dataHost.release();
    super.dispose();
  }
}
