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';

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.weatherIconPath ?? AppAssets.weatherIcon,
            height: SizeConfig.getHeightMultiplier() * 0.5,
          ),
        ],
      ),
    ),
  );
}

Widget getPrayerWidget(ScreenConfigViewModel screenConfigViewModel) {
  if (screenConfigViewModel.nextPrayerToShowWithTime.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.nextPrayerText,
                  color: Colors.grey,
                  fontSize: SizeConfig.getWidthMultiplier() * 1.5,
                  fontFamily: screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
                ),
                AppText(
                  screenConfigViewModel.nextPrayerToShowWithTime,
                  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 ? Colors.black : Colors.transparent,
    fontSize: SizeConfig.getWidthMultiplier() * 8,
  );
}

Widget noPatientInQueue({required String text, required String fontName, required String roomText, required bool isRoomNoRequired, required bool isForRoomLevel, required int counterNo}) {
  Widget noPatientText = Center(
    child: AppText(
      text,
      fontFamily: fontName,
      textAlign: TextAlign.center,
      fontSize: 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,
      ),
    ),
  );
}
