import 'dart:io';

import 'package:chat/configs/app_colors.dart';
import 'package:chat/utils/ui_tools.dart';
import 'package:flutter/material.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_video_elem.dart';
import 'package:video_player/video_player.dart';

class PreviewVideoWidget extends StatefulWidget {
  final V2TimVideoElem video;
  const PreviewVideoWidget(this.video, {Key? key}) : super(key: key);

  @override
  State<PreviewVideoWidget> createState() => _PreviewVideoWidgetState();
}

class _PreviewVideoWidgetState extends State<PreviewVideoWidget> {
  late VideoPlayerController _controller;
  bool isPause = false;

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

    var lv = widget.video.localVideoUrl;

    if (lv != null && lv.isNotEmpty && File(lv).existsSync()) {
      _controller =
          VideoPlayerController.file(File(widget.video.localVideoUrl!))
            ..initialize().then((value) {
              setState(() {
                _controller.play();
              });
            }).catchError((e) {
              UiTools.toast(e.toString());
            });
    } else {
      _controller = VideoPlayerController.network(widget.video.videoUrl!)
        ..initialize().then((value) {
          setState(() {
            _controller.play();
          });
        }).catchError((e) {
          UiTools.toast(e.toString());
        });
    }
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.mainBlack,
      appBar: AppBar(
        backgroundColor: AppColors.transparent,
        foregroundColor: AppColors.white,
      ),
      body: GestureDetector(
        onTap: () {
          setState(() {
            if (_controller.value.isInitialized) {
              if (_controller.value.isPlaying) {
                _controller.pause();
                isPause = true;
              } else {
                _controller.play();
                isPause = false;
              }
            }
          });
        },
        child: Stack(
          children: [
            Center(
              child: _controller.value.isInitialized
                  ? AspectRatio(
                      aspectRatio: _controller.value.aspectRatio,
                      child: VideoPlayer(_controller),
                    )
                  : _controller.value.hasError
                      ? const Text(
                          '视频加载失败',
                          style: TextStyle(
                            color: AppColors.white,
                          ),
                        )
                      : const CircularProgressIndicator(
                          color: AppColors.primary,
                        ),
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: VideoProgressIndicator(
                _controller,
                allowScrubbing: true,
              ),
            ),
            Align(
              alignment: Alignment.center,
              child: Visibility(
                visible: isPause,
                child: const Icon(
                  Icons.play_circle_outline,
                  size: 96,
                  color: AppColors.white,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
