import 'dart:math';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hijri_gregorian_calendar/hijri_gregorian_calendar.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/widgets/appointment_calendar.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:provider/provider.dart';

class DoctorProfilePage extends StatelessWidget {
  DoctorProfilePage({super.key});

  late AppState appState;
  late BookAppointmentsViewModel bookAppointmentsViewModel;

  @override
  Widget build(BuildContext context) {
    bookAppointmentsViewModel = Provider.of<BookAppointmentsViewModel>(context, listen: false);
    appState = getIt.get<AppState>();
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: Column(
        children: [
          Expanded(
            child: CollapsingListView(
              title: "Doctor Profile".needTranslation,
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 24.h),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Image.network(
                              bookAppointmentsViewModel.doctorsProfileResponseModel.doctorImageURL!,
                              width: 63.h,
                              height: 63.h,
                              fit: BoxFit.fill,
                            ).circle(100),
                            SizedBox(width: 8.h),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                ("${bookAppointmentsViewModel.doctorsProfileResponseModel.doctorTitleForProfile} ${bookAppointmentsViewModel.doctorsProfileResponseModel.doctorName}")
                                    .toString()
                                    .toText28(isBold: true),
                                (bookAppointmentsViewModel.doctorsProfileResponseModel.specialty!.isNotEmpty ? bookAppointmentsViewModel.doctorsProfileResponseModel.specialty!.first : "")
                                    .toString()
                                    .toText18(weight: FontWeight.w500, color: AppColors.primaryRedColor),
                              ],
                            ),
                          ],
                        ),
                        Image.network(
                          bookAppointmentsViewModel.doctorsProfileResponseModel.nationalityFlagURL!,
                          width: 32.h,
                          height: 32.h,
                          fit: BoxFit.fill,
                        ),
                      ],
                    ),
                    SizedBox(height: 12.h),
                    Wrap(
                      direction: Axis.horizontal,
                      spacing: 3.h,
                      runSpacing: 4.h,
                      children: [
                        AppCustomChipWidget(
                          icon: AppAssets.rating_icon,
                          iconColor: AppColors.ratingColorYellow,
                          labelText: "Rating: ${bookAppointmentsViewModel.doctorsProfileResponseModel.decimalDoctorRate}".needTranslation,
                        ),
                        AppCustomChipWidget(
                          icon: AppAssets.rating_icon,
                          iconColor: AppColors.ratingColorYellow,
                          labelText: "Reviews: ${bookAppointmentsViewModel.doctorsProfileResponseModel.noOfPatientsRate}".needTranslation,
                        ),
                      ],
                    ),
                    SizedBox(height: 16.h),
                    Divider(color: AppColors.borderOnlyColor.withValues(alpha: 0.1), height: 1.h),
                    SizedBox(height: 16.h),
                    "Biography".toText14(weight: FontWeight.w600, color: AppColors.textColor),
                    bookAppointmentsViewModel.doctorsProfileResponseModel.doctorProfileInfo!.toText12(fontWeight: FontWeight.w600, color: AppColors.greyTextColor),
                  ],
                ).paddingSymmetrical(24.h, 0.h),
              ),
            ),
          ),
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.h,
              hasShadow: true,
            ),
            child: CustomButton(
              text: "View available appointments".needTranslation,
              onPressed: () async {
                LoaderBottomSheet.showLoader();
                bookAppointmentsViewModel.isLiveCareSchedule
                    ? await bookAppointmentsViewModel.getLiveCareDoctorFreeSlots(
                        isBookingForLiveCare: true,
                        onSuccess: (dynamic respData) async {
                          LoaderBottomSheet.hideLoader();
                          showCommonBottomSheetWithoutHeight(
                            title: "Pick a Date".needTranslation,
                            context,
                            child: AppointmentCalendar(),
                            isFullScreen: false,
                            isCloseButtonVisible: true,
                            callBackFunc: () {},
                          );
                        },
                        onError: (err) {
                          LoaderBottomSheet.hideLoader();
                          showCommonBottomSheetWithoutHeight(
                            context,
                            child: Utils.getErrorWidget(loadingText: err),
                            callBackFunc: () {},
                            isFullScreen: false,
                            isCloseButtonVisible: true,
                          );
                        })
                    : await bookAppointmentsViewModel.getDoctorFreeSlots(
                        isBookingForLiveCare: false,
                        onSuccess: (dynamic respData) async {
                          LoaderBottomSheet.hideLoader();
                          showCommonBottomSheetWithoutHeight(
                            title: "Pick a Date".needTranslation,
                            context,
                            child: AppointmentCalendar(),
                            isFullScreen: false,
                            isCloseButtonVisible: true,
                            callBackFunc: () {},
                          );
                        },
                        onError: (err) {
                          LoaderBottomSheet.hideLoader();
                          showCommonBottomSheetWithoutHeight(
                            context,
                            child: Utils.getErrorWidget(loadingText: err),
                            callBackFunc: () {},
                            isFullScreen: false,
                            isCloseButtonVisible: true,
                          );
                        });
              },
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              textColor: AppColors.whiteColor,
              fontSize: 16,
              fontWeight: FontWeight.w500,
              borderRadius: 12,
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              height: 50.h,
              icon: AppAssets.calendar,
              iconColor: AppColors.whiteColor,
              iconSize: 20.h,
            ).paddingSymmetrical(24.h, 24.h),
          ),
        ],
      ),
    );
  }
}
