// lib/views/home/main/widgets/swot_item_card.dart

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/home_page_content/swot.dart';
import 'package:didvan/services/app_initalizer.dart';
import 'package:didvan/services/network/request.dart';
import 'package:didvan/views/home/main/widgets/bookmark.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:url_launcher/url_launcher_string.dart';

class SwotItemCard extends StatefulWidget {
  final SwotItem item;

  const SwotItemCard({super.key, required this.item, this.onBookmarkChangedInList});
  final void Function(int postId, bool isBookmarked)? onBookmarkChangedInList;

  @override
  State<SwotItemCard> createState() => _SwotItemCardState();
}

class _SwotItemCardState extends State<SwotItemCard> {
  String _getCategoryName(String category) {
    switch (category) {
      case 'MACRO_TRENDS':
        return 'کلان روند';
      case 'INDUSTRY_ENVIRONMENT':
        return 'محیط صنعت';
      case 'CONCEPTS':
        return 'مفاهیم';
      case 'MACRO_ENVIRONMENT':
        return 'محیط کلان';
      case 'INVESTMENTS':
        return 'سرمایه گذاری';
      default:
        return category;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        AppInitializer.openWebLink(
          context,
          'http://opportunity-threat.didvan.com/posts/${widget.item.id}/?accessToken=${RequestService.token}',
          mode: LaunchMode.inAppWebView,
        );
      },
      child: Container(
        height: 500 ,
        width: kIsWeb ? 350 : 250,
        margin: const EdgeInsets.only(right: 0),
        padding: const EdgeInsets.all(0),
        decoration: BoxDecoration(
          color: Theme.of(context).cardColor,
          borderRadius: BorderRadius.circular(9),
        ),
        child: Stack(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ClipRRect(
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(12),
                    topRight: Radius.circular(12),
                  ),
                  child: CachedNetworkImage(
                    errorWidget: (context, url, error) {
                      if (kDebugMode) {
                        print('image fetch complete with Error: $error');
                      }
                      return Container(
                        height: 150,
                        width: kIsWeb ? 350 : 300,
                        decoration: BoxDecoration(
                          color:
                              Theme.of(context).colorScheme.disabledBackground,
                        ),
                        child: const Icon(Icons.image_not_supported_outlined),
                      );
                    },
                    errorListener: (value) {},
                    fit: BoxFit.cover,
                    imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
                    httpHeaders: {
                      'Authorization': 'Bearer ${RequestService.token}'
                    },
                    width: kIsWeb ? 350 : 300,
                    height: 150,
                    imageUrl: widget.item.imageUrl,
                    placeholder: (context, _) => ShimmerPlaceholder(
                      width: kIsWeb ? 350 : 300,
                      height: 150,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 0),
                  child: Text(
                    widget.item.title,
                    style: Theme.of(context).textTheme.titleMedium?.copyWith(
                          fontWeight: FontWeight.bold,
                        ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          widget.item.type == "THREAT"
                              ? SvgPicture.asset(
                                  "lib/assets/images/features/Badge.svg")
                              : SvgPicture.asset(
                                  "lib/assets/images/features/Badge-Green.svg"),
                          const SizedBox(
                            width: 5,
                          ),
                          Text(
                            widget.item.type == "THREAT" ? "تهدید" : "فرصت",
                          )
                        ],
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      Row(
                        children: [
                          SvgPicture.asset(
                              "lib/assets/images/features/ant-design_dot-chart-outlined.svg"),
                          const SizedBox(
                            width: 5,
                          ),
                          Text(
                            'عدد ${widget.item.type == "THREAT" ? "تهدید" : "فرصت"}: ${((widget.item.x1 ?? 0.0) * (widget.item.y1 ?? 0.0)).toStringAsFixed(1)}',
                            style: Theme.of(context).textTheme.bodyMedium,
                          )
                        ],
                      ),
                      const SizedBox(height: 10),
                      Row(
                        children: [
                          const Icon(DidvanIcons.puzzle_light,size: 17,),
                          const SizedBox(width: 5,),
                          Text(
                            _getCategoryName(widget.item.category),
                            style: Theme.of(context).textTheme.bodyMedium,
                          ),
                        ],
                      ),
                      const SizedBox(height: 30),
                    ],
                  ),
                ),
              ],
            ),
            Positioned(
              bottom: 3,
              left: 0,
              child: BookmarkIcon(postId: widget.item.id),
            )
          ],
        ),
      ),
    );
  }
}