import 'package:flutter/material.dart';
import 'package:food_health/api/dto/food_scan_dto.dart';
import 'package:food_health/config/theme/custom_colors.dart';
import 'package:food_health/router/config/route_paths.dart';
import 'package:food_health/widgets/common/async_image.dart';
import 'package:food_health/widgets/ui_kit/empty/index.dart';
import 'package:go_router/go_router.dart';
import 'package:remixicon/remixicon.dart';

class RecordListCard extends StatelessWidget {
  final bool loading;
  final List<FoodScanDto> records;
  final Function() onRefresh;

  const RecordListCard({
    super.key,
    required this.records,
    required this.loading,
    required this.onRefresh,
  });

  @override
  Widget build(BuildContext context) {
    if (loading) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    return Visibility(
      visible: records.isNotEmpty,
      replacement: Empty(
        child: ElevatedButton(
          onPressed: onRefresh,
          child: Text("Refresh"),
        ),
      ),
      child: ListView.separated(
        padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
        itemBuilder: (context, index) {
          var item = records[index];
          return InkWell(
            onTap: () {
              context.push(RoutePaths.detail, extra: item);
            },
            child: Container(
              padding: EdgeInsets.all(15),
              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.surface,
                borderRadius: BorderRadius.circular(8),
                boxShadow: [
                  BoxShadow(
                    color: Theme.of(context).colorScheme.shadow,
                    blurRadius: 4,
                    offset: Offset(0, 2),
                  ),
                ],
              ),
              child: Row(
                spacing: 15,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      SizedBox(
                        width: 80,
                        height: 80,
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(8),
                          child: AsyncImage(
                            url: item.imageUrl!,
                          ),
                        ),
                      ),
                      Positioned(
                        right: -5,
                        top: -5,
                        child: StatusWidget(
                          type: item.foodType!,
                        ),
                      ),
                    ],
                  ),
                  Expanded(
                    child: Column(
                      spacing: 10,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          item.foodName ?? "",
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: Theme.of(context).textTheme.titleSmall,
                        ),
                        Text(
                          item.foodDesc ?? "",
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: Theme.of(context).textTheme.labelMedium,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
        separatorBuilder: (context, index) {
          return SizedBox(height: 15);
        },
        itemCount: records.length,
      ),
    );
  }
}

class StatusWidget extends StatelessWidget {
  final int type;

  const StatusWidget({super.key, required this.type});

  @override
  Widget build(BuildContext context) {
    IconData? iconData;
    Color? color;
    switch (type) {
      case 1:
        iconData = RemixIcons.shield_check_line;
        color = Theme.of(context).colorScheme.success;
        break;
      case 2:
        iconData = RemixIcons.error_warning_fill;
        color = Theme.of(context).colorScheme.warning;
        break;
      case 3:
        iconData = RemixIcons.close_circle_line;
        color = Theme.of(context).colorScheme.danger;
        break;
    }
    if (iconData == null) {
      return SizedBox();
    } else {
      return Container(
        padding: EdgeInsets.all(5),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: color,
        ),
        child: Icon(
          iconData,
          color: Colors.white,
          size: 15,
        ),
      );
    }
  }
}
