import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/constants/message_constant.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';

class PreviewImageWidget extends StatelessWidget {
  final IMG_PREVIEW_TYPE type;

  final String path;

  final String original;

  const PreviewImageWidget({
    Key? key,
    required this.type,
    required this.path,
    required this.original,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: AppColors.black,
      child: SafeArea(
        child: Stack(
          children: [
            Positioned.fill(
              child: PhotoViewGallery.builder(
                pageController: PageController(initialPage: 0),
                itemCount: 1,
                builder: (context, index) {
                  if (type == IMG_PREVIEW_TYPE.local) {
                    return PhotoViewGalleryPageOptions(
                      imageProvider: FileImage(File(path)),
                      minScale: PhotoViewComputedScale.contained,
                      maxScale: PhotoViewComputedScale.covered * 2,
                    );
                  } else {
                    if (path.split('?').first.isImageFileName) {
                      return PhotoViewGalleryPageOptions(
                        imageProvider: CachedNetworkImageProvider(path),
                        minScale: PhotoViewComputedScale.contained,
                        maxScale: PhotoViewComputedScale.covered * 2,
                      );
                    } else {
                      return PhotoViewGalleryPageOptions.customChild(
                        child: const Center(
                          child: Text(
                            '格式不支持',
                            style: TextStyle(
                              color: AppColors.white,
                            ),
                          ),
                        ),
                      );
                    }
                  }
                },
              ),
            ),
            const SafeArea(
              child: Padding(
                padding: EdgeInsets.all(4.0),
                child: BackButton(
                  color: AppColors.white,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
