import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class PhotoGalleryView extends StatelessWidget {
  final List<String> imageUrls;
  final int remainingPhotos;

  const PhotoGalleryView({
    super.key,
    required this.imageUrls,
    required this.remainingPhotos,
  });

  Widget _buildSmartImage(String imageUrl) {
    bool isFile = !imageUrl.startsWith('http');
    return ClipRRect(
      borderRadius: BorderRadius.circular(12.0),
      child: isFile
          ? Image.file(
              File(imageUrl),
              fit: BoxFit.cover,
            )
          : Image.network(
              imageUrl,
              fit: BoxFit.cover,
            ),
    );
  }

  Widget _buildLastImageOverlay(String imageUrl) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(12.0),
      child: Stack(
        fit: StackFit.expand,
        children: [
          _buildSmartImage(imageUrl),
          Container(
            // ignore: deprecated_member_use
            color: Colors.white.withOpacity(0.7),
            child: Center(
              child: Text(
                '+$remainingPhotos',
                style: const TextStyle(
                  color: Colors.black,
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (imageUrls.isEmpty) {
      return const Center(child: Text("هنوز عکسی وجود ندارد."));
    }

    final displayUrls = List<String>.from(imageUrls);
    while (displayUrls.length < 6) {
      displayUrls.add('https://via.placeholder.com/200');
    }


    return StaggeredGrid.count(
      crossAxisCount: 4,
      mainAxisSpacing: 8,
      crossAxisSpacing: 7,
      children: [
        if (imageUrls.isNotEmpty)
          StaggeredGridTile.count(
            crossAxisCellCount: 3,
            mainAxisCellCount: 2,
            child: _buildSmartImage(displayUrls[0]),
          ),
        if (imageUrls.length > 1)
          StaggeredGridTile.count(
            crossAxisCellCount: 1,
            mainAxisCellCount: 1,
            child: _buildSmartImage(displayUrls[1]),
          ),
        if (imageUrls.length > 2)
          StaggeredGridTile.count(
            crossAxisCellCount: 1,
            mainAxisCellCount: 1,
            child: _buildSmartImage(displayUrls[2]),
          ),
        if (imageUrls.length > 3)
          StaggeredGridTile.count(
            crossAxisCellCount: 2,
            mainAxisCellCount: 1,
            child: _buildSmartImage(displayUrls[3]),
          ),
        if (imageUrls.length > 4)
          StaggeredGridTile.count(
            crossAxisCellCount: 1,
            mainAxisCellCount: 1,
            child: _buildSmartImage(displayUrls[4]),
          ),
        if (imageUrls.length > 5)
          StaggeredGridTile.count(
            crossAxisCellCount: 1,
            mainAxisCellCount: 1,
            child: _buildLastImageOverlay(displayUrls[5]),
          ),
      ],
    );
  }
}