import 'package:decorated_flutter/decorated_flutter.dart';
import 'package:demo_widgets/demo_widgets.dart';
import 'package:flutter/material.dart';
import 'package:tencent_live_fluttify/tencent_live_fluttify.dart';

class PlayerScreen extends StatefulWidget {
  @override
  _PlayerScreenState createState() => _PlayerScreenState();
}

class _PlayerScreenState extends State<PlayerScreen> {
  bool _loading;
  LivePlayer _player;

  @override
  void initState() {
    super.initState();
    _loading = true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Player')),
      body: DecoratedColumn(
        children: [
          Flexible(
            child: Stack(
              children: [
                CloudVideo(
                  onCloudVideoCreated: (controller) async {
                    _player = await LivePlayer.create();
                    await _player.setPlayerView(controller);
                    await _player.setRenderMode(RenderMode.FULL_SCREEN);
                    await _player.startPlay(
                      'https://media.w3.org/2010/05/sintel/trailer.mp4',
                      type: PlayType.HLS,
                    );
                    await _player.setOnEventListener(onEventPlayBegin: () {
                      setState(() {
                        _loading = false;
                      });
                    });
                  },
                ),
                if (_loading) Center(child: CircularProgressIndicator()),
              ],
            ),
          ),
          Flexible(
            child: SyntaxView(
              code: kCode,
              syntax: Syntax.DART,
              withLinesCount: true,
            ),
          ),
        ],
      ),
    );
  }

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

const kCode = '''
CloudVideo(
  onCloudVideoCreated: (controller) async {
    final player = await LivePlayer.create();
    await player.setPlayerView(controller.playerView);
    await player.setRenderMode(RenderMode.FULL_SCREEN);
    await player.startPlay(
      'https://media.w3.org/2010/05/sintel/trailer.mp4',
      type: PlayType.HLS,
    );
    await player.setOnEventListener(onEventPlayBegin: () {
      setState(() {
        _loading = false;
      });
    });
  },
)
''';
