import 'dart:io';

import 'package:flutter/material.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';

class ImagePreviewDialog extends StatelessWidget {
  final String title;
  final String imageUrl;

  const ImagePreviewDialog({
    super.key,
    required this.title,
    required this.imageUrl,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Center(
        child: Container(
          decoration: BoxDecoration(
            color: AppColors.grayWhite,
            borderRadius: BorderRadius.circular(16),
          ),
          child: Padding(
            padding: const EdgeInsets.all(24.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      title,
                      style: AppTextStyles.bodyLargeMed,
                    ),
                    GestureDetector(
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                      child: Assets.images.icons.x.svg(),
                    ),
                  ],
                ),
                const SizedBox(height: 32),
                //rounded corner image
                ClipRRect(
                  borderRadius: BorderRadius.circular(16),
                  child: _buildImage(context),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildImage(context) {
    if (Uri.parse(imageUrl).isAbsolute) {
      return Image.network(
        imageUrl,
        width: MediaQuery.of(context).size.width - 80,
        fit: BoxFit.cover,
      );
    } else {
      return Image.file(
        File(imageUrl),
        width: MediaQuery.of(context).size.width - 80,
        fit: BoxFit.cover,
      );
    }
  }

  static void show(BuildContext context, String title, String imageUrl) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return ImagePreviewDialog(
          title: title,
          imageUrl: imageUrl,
        );
      },
    );
  }
}
