import 'dart:io';

import 'package:chat/configs/app_colors.dart';
import 'package:chat/controllers/publish_controller.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:video_player/video_player.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class PublishPreviewPage extends StatelessWidget {
  const PublishPreviewPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ctrl = PublishController.to;
    return Scaffold(
      appBar: AppBar(
        title: Obx(() {
          return Text(
            '${(ctrl.publistFileIndex.value + 1)}/${ctrl.publishFileList.length}',
          );
        }),
        actions: [
          IconButton(
            onPressed: () => ctrl.removeFileByCurrentIndex(),
            icon: const Icon(Icons.delete),
          )
        ],
      ),
      body: Obx(() {
        final assetList = ctrl.publishFileList;
        return PhotoViewGallery.builder(
          onPageChanged: (index) => ctrl.publistFileIndex.value = index,
          pageController: PageController(
            initialPage: ctrl.publistFileIndex.value,
          ),
          itemCount: assetList.length,
          builder: (context, index) {
            final source = assetList[index];
            if (source.type == AssetType.image) {
              return PhotoViewGalleryPageOptions(
                imageProvider: AssetEntityImageProvider(source),
                minScale: PhotoViewComputedScale.contained,
                maxScale: PhotoViewComputedScale.covered * 2,
              );
            } else if (source.type == AssetType.video) {
              return PhotoViewGalleryPageOptions.customChild(
                disableGestures: true,
                child: FutureBuilder<File?>(
                  initialData: null,
                  future: source.originFile,
                  builder: (context, snapshot) {
                    if (snapshot.data == null) {
                      return const Center(child: CircularProgressIndicator());
                    }
                    return Chewie(
                      controller: ChewieController(
                        showOptions: false,
                        aspectRatio: source.orientatedSize.width /
                            source.orientatedSize.height,
                        autoInitialize: true,
                        autoPlay: true,
                        videoPlayerController: VideoPlayerController.file(
                          snapshot.data!,
                        ),
                      ),
                    );
                  },
                ),
              );
            } else {
              return PhotoViewGalleryPageOptions.customChild(
                child: const Center(
                  child: Text(
                    '格式不支持',
                    style: TextStyle(color: AppColors.white),
                  ),
                ),
              );
            }
          },
        );
      }),
    );
  }
}
