import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:hmg_qline/views/common_widgets/app_texts_widget.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';
import 'package:marquee/marquee.dart';
import 'package:zo_animated_border/zo_animated_border.dart';

Widget getWeatherWidget(ScreenConfigViewModel screenConfigViewModel) {
  if (screenConfigViewModel.weathersWidgetModel.maxTemp == null || screenConfigViewModel.weathersWidgetModel.minTemp == null || screenConfigViewModel.weathersWidgetModel.iconPhrase == null) {
    return const SizedBox.shrink();
  }
  return Container(
    height: (screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
            screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown)
        ? SizeConfig.getHeightMultiplier() * 2
        : SizeConfig.getHeightMultiplier() * 0.9,
    padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
    margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
    decoration: AppColors.configWidgetDecoration,
    child: Directionality(
      textDirection: screenConfigViewModel.globalConfigurationsModel.textDirection,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              AppText(
                screenConfigViewModel.globalConfigurationsModel.weatherText,
                color: Colors.grey,
                fontSize: SizeConfig.getWidthMultiplier() * 2,
                fontFamily: screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
              ),
              AppText(
                "${screenConfigViewModel.globalConfigurationsModel.maxText}: ${screenConfigViewModel.weathersWidgetModel.maxTemp}°C , ${screenConfigViewModel.globalConfigurationsModel.minText}: ${screenConfigViewModel.weathersWidgetModel.minTemp}°C",
                fontSize: SizeConfig.getWidthMultiplier() * 3,
                fontHeight: 1,
                fontFamily: screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
              ),
            ],
          ),
          const SizedBox(width: 15),
          SvgPicture.asset(
            screenConfigViewModel.weathersWidgetModel.weatherIconPathLeft ?? AppAssets.cloudyLeftIcon,
            height: SizeConfig.getHeightMultiplier() * 0.5,
          ),
        ],
      ),
    ),
  );
}

Widget getPrayerWidget(ScreenConfigViewModel screenConfigViewModel) {
  if (screenConfigViewModel.nextPrayerTime.isEmpty) {
    return const SizedBox.shrink();
  }
  return SizedBox(
    child: Container(
      height: (screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
              screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown)
          ? SizeConfig.getHeightMultiplier() * 2
          : SizeConfig.getHeightMultiplier() * 0.9,
      padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
      decoration: AppColors.configWidgetDecoration,
      child: Directionality(
        textDirection: screenConfigViewModel.globalConfigurationsModel.textDirection,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                AppText(
                  screenConfigViewModel.globalConfigurationsModel.nextPrayerTextEng,
                  color: Colors.grey,
                  fontSize: SizeConfig.getWidthMultiplier() * 1.5,
                  fontFamily: screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
                ),
                AppText(
                  screenConfigViewModel.nextPrayerTime,
                  fontSize: SizeConfig.getWidthMultiplier() * 2.7,
                  fontHeight: 1,
                  fontFamily: screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
                ),
              ],
            ),
            const SizedBox(width: 15),
            SvgPicture.asset(
              AppAssets.mosqueIcon,
              height: SizeConfig.getWidthMultiplier() * 7,
            ),
          ],
        ),
      ),
    ),
  );
}

int getFlexForScreenTypes(ScreenConfigViewModel screenConfigVM) {
  int flex = 1;
  if (screenConfigVM.currentScreenTypeEnum == ScreenTypeEnum.roomLevelScreen) {
    if (screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
        screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown) {
      flex = 2;
    } else {
      flex = 3;
    }
  } else {
    if (screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
        screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown) {
      flex = 2;
    } else {
      flex = 1;
    }
  }
  return flex;
}

Widget counterNoText({required int counterNo, required bool isRoomNoRequired, required String roomText}) {
  return AppText(
    "$roomText $counterNo",
    fontFamily: AppStrings.fontNamePoppins,
    textAlign: TextAlign.center,
    fontWeight: FontWeight.bold,
    color: isRoomNoRequired ? AppColors.greyTextColor : Colors.transparent,
    fontSize: SizeConfig.getWidthMultiplier() * 8,
  );
}

Widget intimationWidget({
  required String text,
  required String fontName,
  required String roomText,
  required bool isRoomNoRequired,
  required bool isForRoomLevel,
  required int counterNo,
  bool isForError = false,
}) {
  Widget noPatientText = Center(
    child: AppText(
      text,
      fontFamily: fontName,
      textAlign: TextAlign.center,
      fontSize: isForError ? SizeConfig.getWidthMultiplier() * 7 : SizeConfig.getWidthMultiplier() * 9,
    ),
  );
  if (isForRoomLevel) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        counterNoText(
          counterNo: counterNo,
          roomText: roomText,
          isRoomNoRequired: isRoomNoRequired,
        ),
        noPatientText,
        const SizedBox.shrink()
      ],
    );
  } else {
    return noPatientText;
  }
}

Widget commonSelectionCardKiosk({
  required LanguageEnum screenLanguageEnum,
  required TextDirection textDirection,
  required String title,
  required String icon,
  required VoidCallback onTap,
}) {
  return InkWell(
    onTap: onTap,
    child: SizedBox(
      child: Container(
        constraints: BoxConstraints(minWidth: SizeConfig.getWidthMultiplier() * 40),
        padding: const EdgeInsets.all(30),
        margin: const EdgeInsets.all(10),
        decoration: AppColors.configWidgetDecoration,
        child: Directionality(
          textDirection: textDirection,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SvgPicture.asset(icon, height: SizeConfig.getWidthMultiplier() * 12),
              const SizedBox(height: 15),
              AppText(
                title,
                fontSize: SizeConfig.getWidthMultiplier() * 6,
                fontHeight: 1,
                fontFamily: screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

Widget commonShadowButton({
  required LanguageEnum screenLanguageEnum,
  required TextDirection textDirection,
  required String title,
  required String icon,
  required double fontSize,
  required VoidCallback onTap,
}) {
  return InkWell(
    onTap: onTap,
    child: Container(
      constraints: BoxConstraints(minWidth: SizeConfig.getWidthMultiplier() * 40),
      padding: const EdgeInsets.all(30),
      margin: const EdgeInsets.all(10),
      decoration: AppColors.configWidgetDecoration,
      child: Directionality(
        textDirection: textDirection,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (icon.isNotEmpty) ...[
              SvgPicture.asset(icon, height: SizeConfig.getWidthMultiplier() * 12),
              const SizedBox(height: 15),
            ],
            AppText(
              title,
              fontSize: fontSize,
              fontFamily: screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
            ),
          ],
        ),
      ),
    ),
  );
}

Widget customShadowTextField({
  required TextEditingController controller,
  required String hintText,
  required Function(String) onSubmitted,
  TextDirection textDirection = TextDirection.ltr,
  TextInputType keyboardType = TextInputType.text,
  bool obscureText = false,
}) {
  return Container(
    margin: const EdgeInsets.all(10),
    padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
    decoration: AppColors.configWidgetDecoration,
    child: Directionality(
      textDirection: textDirection,
      child: TextField(
        controller: controller,
        keyboardType: keyboardType,
        obscureText: obscureText,
        decoration: InputDecoration(
          hintText: hintText,
          border: InputBorder.none,
        ),
        style: const TextStyle(fontSize: 16),
        onSubmitted: onSubmitted,
      ),
    ),
  );
}

Widget engArabicTextWithSeparatorWidget({
  required String englishText,
  required String arabicText,
  double? fontSize,
  Color? color,
  MainAxisAlignment? mainAxisAlignment,
}) {
  return Row(
    mainAxisAlignment: mainAxisAlignment ?? MainAxisAlignment.start,
    children: [
      AppText(
        arabicText,
        color: color ?? AppColors.darkGreyTextColor,
        fontSize: fontSize ?? SizeConfig.getWidthMultiplier() * 2.6,
        fontFamily: AppStrings.fontNameGesTwo,
        fontWeight: FontWeight.bold,
      ),
      AppText(
        " | ",
        fontSize: fontSize ?? SizeConfig.getWidthMultiplier() * 2.6,
        color: color ?? AppColors.darkGreyTextColor,
      ),
      AppText(
        englishText,
        fontSize: fontSize ?? SizeConfig.getWidthMultiplier() * 2.6,
        color: color ?? AppColors.darkGreyTextColor,
      ),
    ],
  );
}

Widget buildTextContainer(bool isForArabic) {
  return Container(
    padding: EdgeInsets.all(SizeConfig.getHeightMultiplier() * 0.2),
    child: Stack(
      children: [
        Positioned(
          top: 0,
          right: SizeConfig.getWidthMultiplier() * 2,
          child: SvgPicture.asset(
            AppAssets.textBgLeaf,
            height: SizeConfig.getHeightMultiplier() * 0.4,
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(top: 15),
          child: AppText(
            textAlign: TextAlign.center,
            AppStrings.thankYouMessageAr,
            fontSize: SizeConfig.getWidthMultiplier() * 3.6,
            color: AppColors.darkGreyTextColor,
            fontFamily: AppStrings.fontNameGesTwo,
          ),
        ),
      ],
    ),
  );
}

Widget showThankyouWidget() {
  return Expanded(
    flex: 4,
    child: Padding(
      padding: EdgeInsets.all(SizeConfig.getHeightMultiplier() * 0.2),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Stack(
            children: [
              Positioned(
                top: 0,
                right: SizeConfig.getWidthMultiplier() * 2,
                child: SvgPicture.asset(
                  AppAssets.textBgLeaf,
                  height: SizeConfig.getHeightMultiplier() * 0.4,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: SizeConfig.getHeightMultiplier() * 0.1, right: SizeConfig.getWidthMultiplier() * 1),
                child: AppText(
                  textAlign: TextAlign.center,
                  AppStrings.thankYouMessageAr,
                  fontSize: SizeConfig.getWidthMultiplier() * 3.6,
                  color: AppColors.darkGreyTextColor,
                  fontFamily: AppStrings.fontNameGesTwo,
                ),
              ),
            ],
          ),
          SizedBox(height: SizeConfig.getHeightMultiplier() * 0.3),
          Stack(
            children: [
              Positioned(
                top: 0,
                left: SizeConfig.getWidthMultiplier() * 3,
                child: SvgPicture.asset(
                  AppAssets.textBgLeaf,
                  height: SizeConfig.getHeightMultiplier() * 0.4,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: SizeConfig.getHeightMultiplier() * 0.1, left: SizeConfig.getWidthMultiplier() * 1.5),
                child: AppText(
                  textAlign: TextAlign.center,
                  AppStrings.thankYouMessageEn,
                  fontSize: SizeConfig.getWidthMultiplier() * 3.6,
                  color: AppColors.darkGreyTextColor,
                  fontFamily: AppStrings.fontNamePoppins,
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}

Widget customShadowSmoothContainer({
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  required Widget child,
  double? height,
  double? width,
  bool removeRightBorder = false,
}) {
  return Container(
    padding: padding,
    margin: margin,
    height: height,
    width: width,
    alignment: Alignment.center,
    decoration: BoxDecoration(
      color: AppColors.whiteColor,
      borderRadius: removeRightBorder
          ? BorderRadius.only(
              topLeft: Radius.circular(SizeConfig.widthMultiplier! * 1.2), // Apply radius to top-left
              bottomLeft: Radius.circular(SizeConfig.widthMultiplier! * 1.2), // Apply radius to bottom-left
              topRight: Radius.zero, // Keep top-right corner sharp
              bottomRight: Radius.zero, // Keep bottom-right corner sharp
            )
          : BorderRadius.circular(SizeConfig.widthMultiplier! * 1.2),
      boxShadow: [
        BoxShadow(
          color: AppColors.smoothContainerShadow,
          offset: const Offset(0, 4), // X: 0, Y: 4
          blurRadius: 14,
          spreadRadius: 0,
        ),
      ],
    ),
    child: child,
  );
}

Widget customShadowSmoothContainerWithBackground({
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  required Widget child,
  double? height,
  double? width,
  CallTypeEnum? callTypeEnum,
}) {
  callTypeEnum ??= CallTypeEnum.vitalSign;

  List<Color> borderGradientCombo = [];
  if (callTypeEnum == CallTypeEnum.vitalSign) {
    borderGradientCombo = [
      const Color(0xFFFFFFFF), // start
      const Color(0xFFF2F3F4),
      const Color(0xFFF2F3F4),
      const Color(0xFFA5ADB4),
      const Color(0xFFA5ADB4),
      const Color(0xFF78848F),
    ];
  } else {
    borderGradientCombo = [
      const Color(0xFFFFFFFF), // start
      const Color(0xFFAFEAD4), // start
      const Color(0xFFAFEAD4), // start
      const Color(0xFF42BB8D), // end
      const Color(0xFF42BB8D), // end
      const Color(0xFF42BB8D),
    ];
  }
  return ZoAnimatedGradientBorder(
    animationDuration: const Duration(seconds: 4),
    glowOpacity: 0.0,
    borderThickness: 8,
    gradientColor: borderGradientCombo,
    borderRadius: SizeConfig.getWidthMultiplier()! * 1.2,
    child: Container(
        padding: padding,
        margin: margin,
        height: height,
        width: width,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          gradient: const LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [
              Color(0xFFECF1F4), // #ECF1F4 (0deg)
              Color(0x71B1B7BC), // rgba(177, 183, 188, 0.445) (90deg)
              Color(0x89BEBDC7), // rgba(190, 195, 199, 0.5375) (135deg)
              Color(0xA1CBCFD3), // rgba(203, 207, 211, 0.63) (178.66deg)
              Color(0xFFE5E7E9), // #E5E7E9 (270deg)
              Color(0xFFF2F3F4), // #F2F3F4 (315deg)
              Color(0xFFECF1F4), // #ECF1F4 (360deg)
            ],
            stops: [0.0, 0.25, 0.375, 0.496, 0.75, 0.875, 1.0],
          ),
          color: AppColors.whiteColor,
          borderRadius: BorderRadius.circular(SizeConfig.getWidthMultiplier()! * 1.2),
          boxShadow: [
            BoxShadow(
              color: AppColors.smoothContainerShadow,
              offset: const Offset(0, 4), // X: 0, Y: 4
              blurRadius: 14,
              spreadRadius: 0,
            ),
          ],
        ),
        child: child),
  );
}

Widget rssFeedWidget({String? feed}) {
  String feedToShow = feed ?? AppStrings.dummyRssFeedText;
  return Expanded(
    child: Marquee(
      text: feedToShow,
      style: TextStyle(
        fontWeight: FontWeight.w400,
        fontSize: SizeConfig.getWidthMultiplier() * 2,
        fontFamily: AppStrings.fontNamePoppins,
      ),
      scrollAxis: Axis.horizontal,
      crossAxisAlignment: CrossAxisAlignment.center,
      blankSpace: 20.0,
      velocity: 100.0,
      pauseAfterRound: const Duration(seconds: 1),
      startPadding: 10.0,
      accelerationDuration: const Duration(seconds: 1),
      accelerationCurve: Curves.linear,
      decelerationDuration: const Duration(milliseconds: 500),
      decelerationCurve: Curves.easeOut,
    ),
  );
}

Widget callTypeContainer({
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  required Widget child,
  double? height,
  double? width,
  required Color color,
  required TextDirection textDirection,
  bool isDisableRightBorderRadius = true,
}) {
  final border = SizeConfig.getWidthMultiplier()! * 0.7;
  return Container(
    padding: padding,
    margin: margin,
    height: height,
    width: width,
    alignment: Alignment.center,
    decoration: BoxDecoration(
      color: color,
      borderRadius: isDisableRightBorderRadius
          ? textDirection == TextDirection.rtl
              ? BorderRadius.only(
                  topLeft: Radius.circular(border),
                  bottomLeft: Radius.circular(border),
                )
              : BorderRadius.only(
                  topRight: Radius.circular(border),
                  bottomRight: Radius.circular(border),
                )
          : BorderRadius.circular(border),
      boxShadow: [
        BoxShadow(
          color: AppColors.smoothContainerShadow,
          offset: const Offset(0, 4), // X: 0, Y: 4
          blurRadius: 14,
          spreadRadius: 0,
        ),
      ],
    ),
    child: child,
  );
}
