import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_qline/models/global_config_model.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:provider/provider.dart';
import 'package:hmg_qline/constants/app_constants.dart';

import 'package:hmg_qline/views/common_widgets/app_texts_widget.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';

class AppHeader extends StatelessWidget implements PreferredSizeWidget {
  const AppHeader({super.key});

  Widget networkStatusBar() {
    return Consumer(builder: (BuildContext context, ScreenConfigViewModel screenConfigVM, Widget? child) {
      return Container(
        color: Colors.grey.withOpacity(0.1),
        padding: EdgeInsets.symmetric(horizontal: SizeConfig.getWidthMultiplier() * 3),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                AppText(
                  "Network Status :",
                  fontSize: SizeConfig.getWidthMultiplier() * 2,
                ),
                AppText(
                  " ${screenConfigVM.isInternetConnected ? "Connected" : "Disconnected"}",
                  fontSize: SizeConfig.getWidthMultiplier() * 2.5,
                  color: screenConfigVM.isInternetConnected ? AppColors.greenColor : AppColors.redColor,
                ),
              ],
            ),
            Row(
              children: [
                Row(
                  children: [
                    AppText(
                      "Hub Status :",
                      fontSize: SizeConfig.getWidthMultiplier() * 2,
                    ),
                    AppText(
                      " ${screenConfigVM.isHubConnected ? "Connected" : "Disconnected"}",
                      fontSize: SizeConfig.getWidthMultiplier() * 2.5,
                      color: screenConfigVM.isHubConnected ? AppColors.greenColor : AppColors.redColor,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Selector<ScreenConfigViewModel, GlobalConfigurationsModel>(
      selector: (context, screenConfigViewModel) => screenConfigViewModel.globalConfigurationsModel,
      builder: (BuildContext context, GlobalConfigurationsModel globalConfigurationsModel, Widget? child) {
        return Column(
          children: [
            Container(
              alignment: Alignment.center,
              height: SizeConfig.getHeightMultiplier() * 0.6,
              padding: const EdgeInsets.symmetric(horizontal: 20),
              decoration: BoxDecoration(color: globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? AppColors.redColor : AppColors.greenColor),
              child: Directionality(
                textDirection: globalConfigurationsModel.textDirection,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    AppText(
                      globalConfigurationsModel.currentServeText ?? "",
                      color: Colors.white,
                      fontSize: SizeConfig.getHeightMultiplier() * 0.4,
                      fontFamily: globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
                    ),
                    SvgPicture.asset(
                      globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? AppAssets.hmgLogo : AppAssets.hmgLogoPharmacy,
                      height: SizeConfig.getHeightMultiplier() * 0.5,
                    ),
                  ],
                ),
              ),
            ),
            networkStatusBar(),
          ],
        );
      },
    );
  }

  @override
  Size get preferredSize => Size(double.maxFinite, SizeConfig.getHeightMultiplier());
}
