import 'package:flutter/material.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:hmg_qline/views/common_widgets/app_footer.dart';
import 'package:hmg_qline/views/common_widgets/app_general_widgets.dart';
import 'package:hmg_qline/views/common_widgets/app_header.dart';
import 'package:hmg_qline/views/common_widgets/app_scaffold.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';
import 'package:provider/provider.dart';

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

  Widget getBody({required BuildContext context}) {
    return Consumer<ScreenConfigViewModel>(
      builder: (BuildContext context, ScreenConfigViewModel screenConfigVM, Widget? child) {
        return Column(
          children: [
            if (screenConfigVM.globalConfigurationsModel.screenTypeEnum != ScreenTypeEnum.roomLevelScreen) ...[
              Expanded(
                flex: getFlexForScreenTypes(screenConfigVM),
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    children: [
                      if (screenConfigVM.globalConfigurationsModel.isWeatherReq) ...[
                        getWeatherWidget(screenConfigVM),
                      ],
                      const SizedBox(width: 20),
                      if (screenConfigVM.globalConfigurationsModel.isPrayerTimeReq) ...[
                        getPrayerWidget(screenConfigVM),
                      ],
                    ],
                  ),
                ),
              )
            ],
            const SizedBox(height: 12),
            Expanded(flex: 10, child: dataContentKiosk(context: context)),
            if (!screenConfigVM.globalConfigurationsModel.isWeatherReq && !screenConfigVM.globalConfigurationsModel.isPrayerTimeReq) ...[
              const SizedBox(height: 100),
            ],
          ],
        );
      },
    );
  }

  Widget kioskLanguageStateWidget(ScreenConfigViewModel screenConfigViewModel) {
    return GridView.builder(
      itemCount: 2,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          padding: const EdgeInsets.all(20),
          child: commonSelectionCardKiosk(
            screenConfigViewModel: screenConfigViewModel,
            title: index == 0 ? "English" : "عربي",
            icon: AppAssets.languageIcon,
            onTap: () {
              if (index == 0) {
                screenConfigViewModel.updateCurrentSelectedKioskLanguage(LanguageEnum.english);
                screenConfigViewModel.updateKioskScreenState(KioskScreenStateEnums.queueSelectionState);
              } else {
                screenConfigViewModel.updateCurrentSelectedKioskLanguage(LanguageEnum.arabic);
                screenConfigViewModel.updateKioskScreenState(KioskScreenStateEnums.queueSelectionState);
              }
            },
          ),
        );
      },
    );
  }

  Widget kioskQueueSelectionStateWidget(ScreenConfigViewModel screenConfigViewModel) {
    return GridView.builder(
        itemCount: 5,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: (screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitUp ||
                  screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum == ScreenOrientationEnum.portraitDown)
              ? 3
              : 2,
        ),
        itemBuilder: (BuildContext context, int index) {
          return Column(mainAxisSize: MainAxisSize.min, children: [
            commonSelectionCardKiosk(
                screenConfigViewModel: screenConfigViewModel,
                title: "New  Latest Certificate Generation ${index + 1}",
                icon: AppAssets.languageIcon,
                onTap: () {
                  screenConfigViewModel.updateKioskScreenState(KioskScreenStateEnums.languageState);
                }),
          ]);
        });
  }

  Widget kioskTicketNumberStateWidget(ScreenConfigViewModel screenConfigViewModel) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        commonSelectionCardKiosk(screenConfigViewModel: screenConfigViewModel, title: "English", icon: AppAssets.languageIcon, onTap: () {}),
        commonSelectionCardKiosk(screenConfigViewModel: screenConfigViewModel, title: "عربي", icon: AppAssets.languageIcon, onTap: () {}),
      ],
    );
  }

  Widget dataContentKiosk({required BuildContext context}) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Consumer(builder: (BuildContext context, ScreenConfigViewModel screenConfigViewModel, Widget? child) {
        return switch (screenConfigViewModel.kioskScreenStateEnum) {
          KioskScreenStateEnums.languageState => kioskLanguageStateWidget(screenConfigViewModel),
          KioskScreenStateEnums.queueSelectionState => kioskQueueSelectionStateWidget(screenConfigViewModel),
          KioskScreenStateEnums.ticketNoState => kioskTicketNumberStateWidget(screenConfigViewModel),
        };
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Selector<ScreenConfigViewModel, ScreenOrientationEnum>(
      selector: (context, screenConfigViewModel) => screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum,
      builder: (BuildContext context, ScreenOrientationEnum screenOrientationEnum, Widget? child) {
        return RotatedBox(
          quarterTurns: screenOrientationEnum.getTurnsByOrientation(),
          child: AppScaffold(
            appBar: const AppHeader(),
            body: getBody(context: context),
            bottomNavigationBar: const AppFooter(),
          ),
        );
      },
    );
  }
}
