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

class AppFooter extends StatelessWidget {
  const AppFooter({super.key});

  Widget _buildStatusRow(String label, bool isConnected) {
    return Row(
      children: [
        AppText(
          "$label:",
          fontSize: SizeConfig.getWidthMultiplier()! * 1.5,
        ),
        AppText(
          " ${isConnected ? "Connected" : "Disconnected"}",
          fontSize: SizeConfig.getWidthMultiplier()! * 1.5,
          color: isConnected ? AppColors.greenColor : AppColors.redColor,
        ),
      ],
    );
  }

  Widget _buildPrayerColumn(String label, String prayerName, {bool isForArabic = false}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        AppText(
          label,
          fontSize: SizeConfig.getWidthMultiplier()! * 1.8,
          fontFamily: isForArabic ? AppStrings.fontNameGesTwo : AppStrings.fontNamePoppins,
        ),
        AppText(
          prayerName,
          fontSize: SizeConfig.getWidthMultiplier()! * 2,
          fontWeight: FontWeight.bold,
          color: AppColors.darkGreyTextColor,
          fontFamily: isForArabic ? AppStrings.fontNameGesTwo : AppStrings.fontNamePoppins,
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, ScreenConfigViewModel screenConfigVM, Widget? child) {
      bool isPortrait = screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
          screenConfigVM.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown;
      return Padding(
        padding: EdgeInsets.symmetric(
          horizontal: SizeConfig.getWidthMultiplier() * 4,
          vertical: SizeConfig.getHeightMultiplier() * 0.14,
        ),
        child: Row(
          children: [
            Expanded(
              flex: 3,
              child: customShadowSmoothContainer(
                height: SizeConfig.getHeightMultiplier() * 0.8,
                padding: EdgeInsets.symmetric(
                  horizontal: SizeConfig.getWidthMultiplier() * 3,
                  vertical: SizeConfig.getHeightMultiplier() * 0.1,
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(top: SizeConfig.getHeightMultiplier()! * 0.1),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              AppText(
                                AppStrings.poweredBy,
                                fontSize: SizeConfig.getWidthMultiplier()! * 1.5,
                                fontWeight: FontWeight.w400,
                                color: AppColors.darkGreyTextColor,
                              ),
                              AppText(
                                "v${screenConfigVM.currentScreenIP.replaceAll(".", "").replaceAll("0", "-")}(${AppConstants.currentBuildVersion})",
                                fontSize: SizeConfig.getWidthMultiplier()! * 1,
                                fontWeight: FontWeight.w400,
                                color: AppColors.darkGreyTextColor,
                              ),
                            ],
                          ),
                        ),
                        SizedBox(width: SizeConfig.getWidthMultiplier()!),
                        Padding(
                          padding: EdgeInsets.only(bottom: SizeConfig.getHeightMultiplier()! * 0.06),
                          child: Image.asset(
                            AppAssets.cloudLogo,
                            height: isPortrait ? SizeConfig.getHeightMultiplier()! * 0.6 : SizeConfig.getHeightMultiplier()! * 0.4,
                          ),
                        ),
                      ],
                    ),
                    Padding(
                      padding: isPortrait
                          ? EdgeInsets.only(
                              top: SizeConfig.getHeightMultiplier()! * 0.3,
                            )
                          : EdgeInsets.only(
                              top: SizeConfig.getHeightMultiplier()! * 0.15,
                            ),
                      child: isPortrait
                          ? Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                _buildStatusRow("Hub Status ", screenConfigVM.isHubConnected),
                                SizedBox(width: SizeConfig.getWidthMultiplier()! * 2),
                                _buildStatusRow("Network Status ", screenConfigVM.isInternetConnected),
                              ],
                            )
                          : Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _buildStatusRow("Hub Status ", screenConfigVM.isHubConnected),
                                SizedBox(height: SizeConfig.getHeightMultiplier()! * 0.05),
                                _buildStatusRow("Network Status ", screenConfigVM.isInternetConnected),
                              ],
                            ),
                    ),
                  ],
                ),
              ),
            ),
            if (screenConfigVM.globalConfigurationsModel.isPrayerTimeReq) ...[
              SizedBox(width: SizeConfig.getWidthMultiplier()! * 1.7),
              Directionality(
                textDirection: screenConfigVM.globalConfigurationsModel.textDirection,
                child: Expanded(
                  flex: isPortrait ? 2 : 3,
                  child: customShadowSmoothContainer(
                    height: SizeConfig.getHeightMultiplier() * 0.8,
                    padding: EdgeInsets.symmetric(
                      horizontal: isPortrait ? SizeConfig.getWidthMultiplier() * 3 : SizeConfig.getWidthMultiplier() * 1.5,
                      vertical: SizeConfig.getHeightMultiplier() * 0.1,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        AppText(
                          screenConfigVM.nextPrayerTime,
                          fontSize: SizeConfig.getWidthMultiplier()! * 3,
                          fontWeight: FontWeight.bold,
                          color: AppColors.newRedColor,
                          letterSpacing: 1.5,
                        ),
                        Row(
                          children: [
                            if (screenConfigVM.nextPrayerToShowEng.isNotEmpty) ...[
                              _buildPrayerColumn(screenConfigVM.globalConfigurationsModel.nextPrayerTextEng, "${screenConfigVM.nextPrayerToShowEng}   "),
                            ],
                            if (screenConfigVM.nextPrayerToShowEng.isNotEmpty && screenConfigVM.nextPrayerToShowArb.isNotEmpty) ...[
                              Padding(
                                padding: EdgeInsets.fromLTRB(
                                  SizeConfig.getWidthMultiplier()! * 0.1,
                                  SizeConfig.getHeightMultiplier()! * 0.18,
                                  SizeConfig.getWidthMultiplier()! * 0.1,
                                  0,
                                ),
                                child: AppText("|", fontSize: SizeConfig.getWidthMultiplier()! * 1.8, color: AppColors.darkGreyTextColor),
                              ),
                            ],
                            if (screenConfigVM.nextPrayerToShowArb.isNotEmpty) ...[
                              _buildPrayerColumn(screenConfigVM.globalConfigurationsModel.nextPrayerTextArb, screenConfigVM.nextPrayerToShowArb, isForArabic: true),
                            ],
                          ],
                        ),
                        if (isPortrait) ...[
                          SvgPicture.asset(
                            AppAssets.salahTimeIcon,
                            height: SizeConfig.getHeightMultiplier() * 0.5,
                          ),
                        ],
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ],
        ),
      );
    });
  }
}
