import 'dart:convert';
import 'dart:typed_data';

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 Function() onAddFilePressed;
  final bool isReview;
  final bool isPdf;
  final bool isFromNetwork;
  final bool allowAdButton;

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

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      physics: const NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      crossAxisCount: 4,
      crossAxisSpacing: 4.0,
      mainAxisSpacing: 8.0,
      children: List.generate(
        !allowAdButton
            ? pickedFiles.length
            : (isReview)
                ? pickedFiles.length
                : pickedFiles.length + 1,
        (index) {
          if (index == pickedFiles.length && !isReview && allowAdButton) {
            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,
          ));
        },
      ),
    );
  }
}

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({
    super.key,
    required this.image,
    this.onCrossPressedPrimary,
    this.onCrossPressedSecondary,
    this.index,
    this.isReview = false,
    this.isPdf = false,
  });

  bool isBase64String(String input) {
    // This regex checks if the string is a valid Base64 string
    final base64Regex = RegExp(r'^[A-Za-z0-9+/=]+$');

    // Check if the string matches the Base64 pattern
    return base64Regex.hasMatch(input) && (input.length % 4 == 0);
  }

  @override
  Widget build(BuildContext context) {
    Uint8List? bytes;
    if ((image.isFromNetwork == null || image.isFromNetwork == false) && isBase64String(image.filePath ?? "")) {
      bytes = (base64Decode(image.filePath!)) as Uint8List?;
    }
    return Stack(
      children: [
        SizedBox(
            height: 90,
            width: 90,
            child: Stack(
              children: [
                isPdf
                    ? Container(
                        height: 75,
                        width: 73,
                        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.filePath!
                            .buildNetworkImage(
                              fit: BoxFit.fill,
                              height: 75,
                              width: 73,
                            )
                            .paddingAll(8)
                        : isBase64String(image.filePath!)
                            ? Image.memory(
                                bytes!,
                                fit: BoxFit.fill,
                                height: 72,
                                width: 70,
                              ).paddingAll(8)
                            : image.filePath!
                                .buildFileImage(
                                  fit: BoxFit.fill,
                                  height: 72,
                                  width: 70,
                                )
                                .paddingAll(8),
                if (!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!);
                  })
                ]
              ],
            )),
      ],
    );
  }
}
