import 'dart:io';

import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';

class PickedFilesContainer extends StatelessWidget {
  final List<ImageModel> pickedFiles;
  final Function(String filePath)? onCrossPressedPrimary;
  final Function(int index, String filePath)? onCrossPressedSecondary;
  final int? index;
  final bool isReview;
  final bool isPdf;
  final bool isFromNetwork;
  final Function() onAddFilePressed;

  const PickedFilesContainer({
    Key? key,
    required this.pickedFiles,
    this.onCrossPressedPrimary,
    this.onCrossPressedSecondary,
    this.index,
    required this.onAddFilePressed,
    this.isReview = false,
    this.isPdf = false,
    this.isFromNetwork = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      crossAxisCount: 4,
      crossAxisSpacing: 4.0,
      mainAxisSpacing: 8.0,
      children: List.generate(
        isReview ? pickedFiles.length : pickedFiles.length + 1,
        (index) {
          if (index == pickedFiles.length && !isReview) {
            return Container(
              decoration: BoxDecoration(color: MyColors.greyButtonColor, border: Border.all(width: 2, color: MyColors.greyAddBorderColor)),
              margin: const EdgeInsets.all(8),
              alignment: Alignment.center,
              child: Container(
                height: 24,
                width: 24,
                decoration: const BoxDecoration(shape: BoxShape.circle, color: MyColors.darkTextColor),
                child: const Icon(Icons.add, color: MyColors.white),
              ),
            ).onPress(onAddFilePressed);
          }
          return Center(
              child: BuildFilesContainer(
            image: pickedFiles[index],
            onCrossPressedPrimary: onCrossPressedPrimary,
            onCrossPressedSecondary: onCrossPressedSecondary,
            index: index,
            isReview: isReview,
            isPdf: isPdf,
          ));
        },
      ),
    );
    // return Wrap(
    //   children: pickedImages
    //       .map((file) => BuildImageContainer(
    //             file: file,
    //             onCrossPressedPrimary: onCrossPressedPrimary,
    //             onCrossPressedSecondary: onCrossPressedSecondary,
    //             index: index,
    //             isReview: isReview,
    //           ))
    //       .toList(),
    // );
  }
}

class BuildFilesContainer extends StatelessWidget {
  final ImageModel image;
  final Function(String filePath)? onCrossPressedPrimary;
  final Function(int index, String filePath)? onCrossPressedSecondary;
  final int? index;
  final bool isReview;
  final bool isPdf;

  const BuildFilesContainer({
    Key? key,
    required this.image,
    this.onCrossPressedPrimary,
    this.onCrossPressedSecondary,
    this.index,
    this.isReview = false,
    this.isPdf = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        SizedBox(
            height: 90,
            width: 90,
            child: Stack(
              children: [
                isPdf
                    ? Container(
                        height: 72,
                        width: 70,
                        margin: const EdgeInsets.all(8),
                        decoration: BoxDecoration(
                            border: Border.all(
                          width: 2,
                          color: MyColors.darkPrimaryColor,
                        )),
                        child: const Icon(Icons.picture_as_pdf).paddingAll(8),
                      )
                    : image.isFromNetwork!
                        ? Image.network(
                            image.filePath!,
                            fit: BoxFit.fill,
                            height: 72,
                            width: 70,
                          ).paddingAll(8)
                        : Image.file(
                            File(image.filePath!),
                            fit: BoxFit.fill,
                            height: 72,
                            width: 70,
                          ).paddingAll(8),
                !isReview
                    ? Align(
                        alignment: Alignment.topRight,
                        child: MyAssets.closeWithOrangeBg.buildSvg(
                          fit: BoxFit.fill,
                          height: 30,
                          width: 30,
                        ),
                      ).onPress(() {
                        if (onCrossPressedPrimary == null) {
                          onCrossPressedSecondary!(index!, image.filePath!);
                          return;
                        }
                        onCrossPressedPrimary!(image.filePath!);
                      })
                    : const SizedBox()
              ],
            )),
      ],
    );
  }
}
