import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/image_preview_dialog.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_popup_menu.dart';

class UploadImageCard extends StatelessWidget {
  final String title;
  final String? message;
  final VoidCallback onTap;
  final EdgeInsets padding;
  final Color? statusColor;
  final String? imageUrl;
  final bool inUploading;
  final bool hasError;
  final VoidCallback? onSelectImage;
  final VoidCallback? onDeleteTap;
  final Widget child;
  final String? localImagePath;

  const UploadImageCard({
    super.key,
    required this.title,
    required this.onTap,
    this.onSelectImage,
    this.onDeleteTap,
    this.message,
    this.padding = EdgeInsets.zero,
    this.statusColor,
    this.imageUrl,
    this.inUploading = false,
    this.hasError = false,
    this.child = const SizedBox.shrink(),
    this.localImagePath,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: const BoxConstraints(minHeight: 48),
      margin: padding,
      padding: const EdgeInsets.all(4),
      decoration: KwBoxDecorations.primaryLight8,
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              _buildImageSection(),
              const Gap(8),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: AppTextStyles.bodyMediumMed.copyWith(
                        color: AppColors.blackBlack,
                      ),
                    ),
                    if (message != null) const Gap(4),
                    if (message != null)
                      Text(
                        message!,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: AppTextStyles.bodyTinyReg.copyWith(
                          color: statusColor ?? AppColors.blackGray,
                        ),
                      ),
                  ],
                ),
              ),
              const Gap(16),
              _actionSection(context),
            ],
          ),
          child,
        ],
      ),
    );
  }

  Widget _buildImageSection() {
    if (inUploading) {
      return _buildImageLoadingSpinner();
    }

    if (imageUrl != null || localImagePath != null) {
      return Container(
        width: 48,
        height: 48,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6),
          border: Border.all(
            color: statusColor ?? AppColors.tintDarkGreen,
            width: 1,
          ),
          image: DecorationImage(
            image: localImagePath != null
                ? FileImage(File(localImagePath!))
                : CachedNetworkImageProvider(imageUrl ?? ''),
            fit: BoxFit.cover,
          ),
        ),
      );
    }

    return (hasError
            ? Assets.images.icons.imageErrorPlaceholder
            : Assets.images.icons.imagePlaceholder)
        .svg();
  }

  Container _buildImageLoadingSpinner() {
    return Container(
      width: 48,
      height: 48,
      decoration: KwBoxDecorations.primaryLight8,
      child: const Center(
        child: SizedBox(
          height: 24,
          width: 24,
          child: CircularProgressIndicator(
            color: AppColors.statusSuccess,
          ),
        ),
      ),
    );
  }

  Widget _actionSection(context) {
    if (imageUrl != null || localImagePath != null) {
      return _buildImageMenu(context);
    }

    return _buildButton(
      'upload'.tr(),
      onSelectImage,
      Assets.images.userProfile.menu.gallery,
    );
  }

  Container _buildButton(
    String? title,
    VoidCallback? onTap,
    SvgGenImage icon, {
    KwButtonFit? fit,
  }) {
    return Container(
      margin: const EdgeInsets.only(right: 4),
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: AppColors.grayTintStroke, width: 1),
      ),
      child: KwButton.secondary(
        label: title,
        fit: fit,
        onPressed: () {
          onTap?.call();
        },
        height: 30,
        leftIcon: icon,
      ),
    );
  }

  Widget _buildImageMenu(context) {
    return Row(
      children: [
        _buildButton(
          'preview'.tr(),
          () {
            ImagePreviewDialog.show(
              context,
              title,
              localImagePath ?? imageUrl ?? '',
            );
          },
          Assets.images.icons.eye,
        ),
        KwPopupMenu(
          horizontalMargin: 24,
          menuItems: [
            KwPopupMenuItem(
              title: 're-upload'.tr(),
              icon: Assets.images.icons.downloadCloud.svg(),
              onTap: () {
                onSelectImage?.call();
              },
            ),
            KwPopupMenuItem(
              title: 'delete'.tr(),
              textStyle: AppTextStyles.bodyMediumReg.copyWith(
                color: AppColors.statusError,
              ),
              icon: Assets.images.icons.trash.svg(),
              onTap: () {
                onDeleteTap?.call();
              },
            ),
          ],
        ),
      ],
    );
  }
}
