import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';

class NetworkFilesContainerForVehiclePosting extends StatelessWidget {
  final List<VehiclePostingImages> networkFilesForVehiclePosting;
  final Function(String filePath)? onCrossPressedPrimary;
  final Function(int index, String filePath)? onCrossPressedSecondary;
  final int? index;
  final bool isReview;
  final Function() onAddFilePressed;

  const NetworkFilesContainerForVehiclePosting({
    Key? key,
    required this.networkFilesForVehiclePosting,
    this.onCrossPressedPrimary,
    this.onCrossPressedSecondary,
    this.index,
    required this.onAddFilePressed,
    this.isReview = 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(
        networkFilesForVehiclePosting.length,
        (index) {
          return Center(
              child: BuildNetworkContainer(
            fileUrl: networkFilesForVehiclePosting[index].imageUrl ?? "",
            onCrossPressedPrimary: onCrossPressedPrimary,
            onCrossPressedSecondary: onCrossPressedSecondary,
            index: index,
            isReview: isReview,
          ));
        },
      ),
    );
  }
}

class NetworkFilesContainerForDamagedParts extends StatelessWidget {
  final List<VehiclePostingDamageParts> networkFilesForDamagedParts;
  final Function(String filePath)? onCrossPressedPrimary;
  final Function(int index, String filePath)? onCrossPressedSecondary;
  final int? index;
  final bool isReview;
  final Function() onAddFilePressed;

  const NetworkFilesContainerForDamagedParts({
    Key? key,
    required this.networkFilesForDamagedParts,
    this.onCrossPressedPrimary,
    this.onCrossPressedSecondary,
    this.index,
    required this.onAddFilePressed,
    this.isReview = 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(
        networkFilesForDamagedParts.length,
        (index) {
          return Center(
              child: BuildNetworkContainer(
            fileUrl: networkFilesForDamagedParts[index].imageUrl ?? "",
            onCrossPressedPrimary: onCrossPressedPrimary,
            onCrossPressedSecondary: onCrossPressedSecondary,
            index: index,
            isReview: isReview,
          ));
        },
      ),
    );
  }
}

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

  const BuildNetworkContainer({
    Key? key,
    required this.fileUrl,
    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.network(
                        fileUrl,
                        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!, fileUrl);
                          return;
                        }
                        onCrossPressedPrimary!(fileUrl);
                      })
                    : const SizedBox()
              ],
            )),
      ],
    );
  }
}
