import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/header_screen.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:taafee_mobile/common/widgets/toast.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/card/business_logic_layer/card_controller.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';

import '../../../../core/local_storage/local_storage.dart';

// ignore: must_be_immutable
class ImagesGalleryView extends StatelessWidget {
  ImagesGalleryView({super.key});
  final Key galleryViewKey = const Key('gallery');
  var image = Get.arguments;
  List<String>? imagesUrls;
  File? imageFile;
  ImageProvider<Object>? imageProvider;
  final CardController cardController = Get.find<CardController>();
  @override
  Widget build(BuildContext context) {
    if (image is List<String>) {
      imagesUrls = image;
    } else if (image is File) {
      imageFile = image;
    } else if (image is ImageProvider<Object>) {
      imageProvider = image;
    }
    return Scaffold(
      backgroundColor: Colors.black,
      body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              HeaderScreen(
                'card_images'.tr,
                textColor: Colors.white,
                iconColor: Colors.white,
              ),
              SizedBox(
                width: 20,
                height: 20,
                child: SvgPicture.asset(
                  "assets/icons/option.svg",
                  width: Responsive.isTablet() ? 8 : 4,
                  colorFilter:
                      const ColorFilter.mode(Colors.white, BlendMode.srcIn),
                ).onTap(() {
                  Get.bottomSheet(
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.grey[850],
                      ),
                      child: SingleChildScrollView(
                        child: Column(
                          children: [
                            Obx(() {
                              return Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  const Icon(
                                    Icons.vrpano,
                                    color: Colors.white,
                                    size: 24,
                                  ),
                                  const SizedBox(
                                    width: 20,
                                  ),
                                  MediumTextWidget(
                                    'save_to_gallery'.tr,
                                    color: Colors.white,
                                    fontSize: Responsive.isTablet() ? 20 : 16,
                                  ),
                                  const SizedBox(
                                    width: 20,
                                  ),
                                  if (cardController.saveCardState.loading)
                                    const SizedBox(
                                      width: 20,
                                      height: 20,
                                      child: CircularProgressIndicator(
                                        color: Colors.white,
                                      ),
                                    ),
                                ],
                              ).paddingOnly(left: 8, right: 8).onTap(() {
                                if (!cardController.saveCardState.loading &&
                                    imagesUrls != null) {
                                  cardController.saveNetworkImage(
                                      imagesUrls![cardController.imageIndex],
                                      onError: (e) {
                                    Toast.showToast('failed_to_save'.tr);
                                  }, onSuccess: (value) {
                                    RoutingManager.back();
                                    Toast.showToast(
                                        'photo_has_been_saved_successfully'.tr);
                                  });
                                }
                              });
                            })
                                .paddingSymmetric(horizontal: 8)
                                .paddingOnly(top: 18, bottom: 18),
                          ],
                        ),
                      ),
                    ),
                  );
                }),
              ),
            ],
          ).paddingOnly(left: 20, bottom: 20, right: 20, top: 20),
          SizedBox(
            height: Get.height * 0.8,
            child: PhotoViewGallery.builder(
              onPageChanged: (index) {
                cardController.changeImageIndex(index);
              },
              pageController: cardController.pageController.value,
              itemCount: (imagesUrls != null) ? imagesUrls!.length : 1,
              builder: (context, index) {
                return (imagesUrls != null)
                    ? PhotoViewGalleryPageOptions(
                        imageProvider: CachedNetworkImageProvider(
                          imagesUrls![index],
                          headers: {
                            "Authorization":
                                LocalStorage().getChatToken() ?? "",
                          },
                        ),
                      )
                    : ((imageFile != null)
                        ? PhotoViewGalleryPageOptions(
                            imageProvider: FileImage(imageFile!),
                          )
                        : PhotoViewGalleryPageOptions(
                            imageProvider: imageProvider,
                          ));
              },
            ),
          ),
          const SizedBox(
            height: 20,
          ),
          if (imagesUrls != null)
            SmoothPageIndicator(
              count: imagesUrls!.length,
              controller: cardController.pageController.value,
              effect: ExpandingDotsEffect(
                dotColor: Colors.grey,
                dotHeight: 10,
                dotWidth: 10,
                spacing: 5,
                expansionFactor: 2,
                activeDotColor: AppColors.primeColor,
              ),
            ),
        ],
      ).makeSafeArea(),
    );
  }
}
