import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/views/conversation/preview/video_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';

class ShowVideoMessage extends StatelessWidget {
  final V2TimMessage message;
  const ShowVideoMessage(this.message, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Get.to(
          PreviewVideoWidget(message.videoElem!),
          transition: Transition.zoom,
        );
      },
      child: Container(
        constraints: BoxConstraints(
          maxWidth: Get.width * 0.382,
          maxHeight: 192,
          minHeight: 96,
          minWidth: 96,
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: Stack(
            children: [
              _showVideSnapshot(),
              const Positioned.fill(
                child: Center(
                  child: Icon(
                    Icons.play_circle_outline,
                    size: 64,
                    color: AppColors.white,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _showVideSnapshot() {
    String ss = message.videoElem!.localSnapshotUrl!;
    String? su = message.videoElem!.snapshotUrl;

    if (ss.isNotEmpty && File(ss).existsSync()) {
      return Image.file(
        File(ss),
        fit: BoxFit.cover,
      );
    } else if (su != null && su.isNotEmpty) {
      return CachedNetworkImage(
        imageUrl: su,
        fit: BoxFit.cover,
      );
    } else {
      return Container();
    }
  }
}
