import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/ui_models/hmg_services_component_model.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:url_launcher/url_launcher.dart';

class ServiceGridViewItem extends StatelessWidget {
  final HmgServicesComponentModel hmgServiceComponentModel;
  final int index;
  final bool isHomePage;
  final bool isLocked;
  final bool isHealthToolIcon;
  final Function? onTap;

  const ServiceGridViewItem(
      this.hmgServiceComponentModel, this.index, this.isHomePage,
      {super.key, this.isLocked = false, required this.isHealthToolIcon, this.onTap});

  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: () => hmgServiceComponentModel.route != null ? getIt.get<NavigationService>().pushPageRoute(hmgServiceComponentModel.route!) : hmgServiceComponentModel.onTap!(),
        // onTap: () => hmgServiceComponentModel.isExternalLink
        //     ? _openLink(hmgServiceComponentModel.route!)
        //     : getIt
        //         .get<NavigationService>()
        //         .pushPageRoute(hmgServiceComponentModel.route!),
        child: Column(
          mainAxisSize: MainAxisSize.max,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 48.h,
              width: 48.h,
              decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                color: hmgServiceComponentModel.bgColor,
                borderRadius: 12.r,
                hasShadow: false,
              ),
              child: Padding(
                padding: EdgeInsets.all(12.h),
                child: Utils.buildSvgWithAssets(
                  icon: hmgServiceComponentModel.icon,
                  iconColor: isHealthToolIcon ? null : AppColors.whiteColor,
                  fit: BoxFit.contain,
                    applyThemeColor: false
                ),
              ),
            ),
            SizedBox(height: 4.h),
            hmgServiceComponentModel.title.toText12(
              isBold: true,
              color: AppColors.textColor,
              maxLine: 2,

            ),
          ],
        ));
  }

  Future<void> _openLink(String link) async {
    final Uri url = Uri.parse(link);

    if (await canLaunchUrl(url)) {
      await launchUrl(url, mode: LaunchMode.externalApplication);
    } else {
      throw "Could not launch $url";
    }
  }
}
