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/kw_button.dart';
import 'package:krow/features/shifts/domain/shift_entity.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:whatsapp_unilink/whatsapp_unilink.dart';

class ShiftManageWidget extends StatelessWidget {
  final List<ShiftManager> managers;

  const ShiftManageWidget({super.key, required this.managers});

  @override
  Widget build(BuildContext context) {
    if (managers.isEmpty) return const SizedBox.shrink();
    return Container(
      padding: const EdgeInsets.all(12.0),
      margin: const EdgeInsets.only(left: 16, right: 16, top: 8),
      decoration: KwBoxDecorations.primaryLight12,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'manage_contact_details'.tr().toUpperCase(),
            style: AppTextStyles.captionReg
                .copyWith(color: AppColors.blackCaptionText),
          ),
          const Gap(16),
          ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemCount: managers.length,
            itemBuilder: (context, index) {
              return _buildManager(managers[index]);
            },
            separatorBuilder: (context, index) {
              return const Padding(
                padding: EdgeInsets.symmetric(vertical: 8),
                child: Divider(
                  color: AppColors.grayTintStroke,
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _buildManager(ShiftManager manager) {
    return Row(
      children: [
        if(manager.imageUrl.isNotEmpty)
        CircleAvatar(
          radius: 24.0,
          backgroundColor: AppColors.grayWhite,
          backgroundImage: CachedNetworkImageProvider(
              manager.imageUrl), // Replace with your image asset
        ),
        const SizedBox(width: 16.0),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                manager.name,
                style: AppTextStyles.bodyMediumSmb,
              ),
              const SizedBox(height: 4.0),
              Text(
                manager.phoneNumber,
                style: AppTextStyles.bodySmallReg
                    .copyWith(color: AppColors.blackGray),
              ),
            ],
          ),
        ),
        const SizedBox(width: 8.0),
        Row(
          children: [
            KwButton.outlinedPrimary(
              rightIcon: Assets.images.icons.whatsapp,
              fit: KwButtonFit.circular,
              onPressed: () {
                var link = WhatsAppUnilink(
                  phoneNumber: manager.phoneNumber,
                  text: '',
                );
                launchUrlString(link.toString());
              },
            ),
            const SizedBox(width: 8.0),
            KwButton.outlinedPrimary(
              label: 'call'.tr(),
              rightIcon: Assets.images.icons.call,
              onPressed: () {
                launchUrlString('tel:${manager.phoneNumber}');
              },
            ).copyWith(
                color: AppColors.tintDarkGreen,
                textColors: AppColors.statusSuccess),
          ],
        )
      ],
    );
  }
}
