import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.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/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/medical_file_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/my_appointments_page.dart';
import 'package:hmg_patient_app_new/presentation/insurance/widgets/patient_insurance_card.dart';
import 'package:hmg_patient_app_new/presentation/medical_file/widgets/medical_file_card.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/custom_tab_bar.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:hmg_patient_app_new/widgets/shimmer/movies_shimmer_widget.dart';
import 'package:hmg_patient_app_new/widgets/transitions/fade_page.dart';
import 'package:provider/provider.dart';

class MedicalFilePage extends StatefulWidget {
  MedicalFilePage({super.key});

  @override
  State<MedicalFilePage> createState() => _MedicalFilePageState();
}

class _MedicalFilePageState extends State<MedicalFilePage> {
  late InsuranceViewModel insuranceViewModel;

  int currentIndex = 0;

  @override
  void initState() {
    scheduleMicrotask(() {
      insuranceViewModel.initInsuranceProvider();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    insuranceViewModel = Provider.of<InsuranceViewModel>(context);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      appBar: AppBar(
        title: const Text('Appointments'),
        backgroundColor: AppColors.bgScaffoldColor,
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            LocaleKeys.medicalFile.tr(context: context).toText22(isBold: true).paddingSymmetrical(24.h, 0.0),
            SizedBox(height: 16.h),
            TextInputWidget(
              labelText: LocaleKeys.search.tr(context: context),
              hintText: "Type any record",
              controller: TextEditingController(),
              keyboardType: TextInputType.number,
              isEnable: true,
              prefix: null,
              autoFocus: false,
              isBorderAllowed: false,
              isAllowLeadingIcon: true,
              padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 8.h),
              leadingIcon: AppAssets.student_card,
            ).paddingSymmetrical(24.h, 0.0),
            SizedBox(height: 16.h),
            Container(
              width: double.infinity,
              decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                color: AppColors.whiteColor,
                borderRadius: 24,
              ),
              child: Padding(
                padding: EdgeInsets.all(16.h),
                child: Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Image.asset(
                          AppAssets.male_img,
                          width: 56.h,
                          height: 56.h,
                        ),
                        SizedBox(width: 8.h),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            "Haroon Amjad".toText18(isBold: true),
                            SizedBox(height: 4.h),
                            Row(
                              children: [
                                CustomButton(
                                  icon: AppAssets.file_icon,
                                  iconColor: AppColors.blackColor,
                                  iconSize: 12.h,
                                  text: "File no: 3628599",
                                  onPressed: () {},
                                  backgroundColor: AppColors.greyColor,
                                  borderColor: AppColors.greyColor,
                                  textColor: AppColors.blackColor,
                                  fontSize: 10,
                                  fontWeight: FontWeight.normal,
                                  borderRadius: 12,
                                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  height: 30.h,
                                ),
                                SizedBox(width: 4.h),
                                CustomButton(
                                  text: LocaleKeys.verified.tr(context: context),
                                  onPressed: () {},
                                  backgroundColor: AppColors.greyColor,
                                  borderColor: AppColors.greyColor,
                                  textColor: AppColors.blackColor,
                                  fontSize: 10,
                                  fontWeight: FontWeight.normal,
                                  borderRadius: 12,
                                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  height: 30.h,
                                ),
                              ],
                            ),
                          ],
                        )
                      ],
                    ),
                    SizedBox(height: 16.h),
                    Divider(color: AppColors.dividerColor, height: 1.h),
                    SizedBox(height: 16.h),
                    Row(
                      children: [
                        CustomButton(
                          text: "30 Years Old",
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.normal,
                          borderRadius: 12,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                        SizedBox(width: 4.h),
                        CustomButton(
                          icon: AppAssets.blood_icon,
                          iconColor: AppColors.primaryRedColor,
                          iconSize: 13.h,
                          text: "Blood: A+",
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.normal,
                          borderRadius: 12,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                        SizedBox(width: 4.h),
                        CustomButton(
                          icon: AppAssets.insurance_active_icon,
                          iconColor: AppColors.successColor,
                          iconSize: 13.h,
                          text: "Insurance Active",
                          onPressed: () {},
                          backgroundColor: AppColors.bgGreenColor.withOpacity(0.20),
                          borderColor: AppColors.bgGreenColor.withOpacity(0.0),
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.normal,
                          borderRadius: 12,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                      ],
                    ),
                    SizedBox(height: 8.h),
                  ],
                ),
              ),
            ).paddingSymmetrical(24.h, 0.0),
            SizedBox(height: 16.h),
            Consumer<MedicalFileViewModel>(builder: (context, medicalFileVM, child) {
              return Column(
                children: [
                  CustomTabBar(
                    activeTextColor: Color(0xffED1C2B),
                    activeBackgroundColor: Color(0xffED1C2B).withValues(alpha: .1),
                    tabs: [
                      CustomTabBarModel(AppAssets.myFilesBottom, LocaleKeys.general.tr(context: context).needTranslation),
                      CustomTabBarModel(AppAssets.insurance, LocaleKeys.insurance.tr(context: context)),
                      CustomTabBarModel(AppAssets.requests, LocaleKeys.request.tr(context: context).needTranslation),
                      CustomTabBarModel(AppAssets.more, "More".needTranslation),
                    ],
                    onTabChange: (index) {
                      print(index);
                      medicalFileVM.onTabChanged(index);
                    },
                  ).paddingSymmetrical(24.h, 0.0),
                  SizedBox(height: 24.h),
                  getSelectedTabData(medicalFileVM.selectedTabIndex),
                ],
              );
            }),
          ],
        ),
      ),
    );
  }

  Widget getSelectedTabData(int index) {
    switch (index) {
      case 0:
        //General Tab Data
        return Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                "Appointments & visits".needTranslation.toText18(isBold: true),
                Row(
                  children: [
                    LocaleKeys.viewAll.tr().toText12(color: AppColors.primaryRedColor, fontWeight: FontWeight.w500),
                    SizedBox(width: 2.h),
                    Icon(Icons.arrow_forward_ios, color: AppColors.primaryRedColor, size: 10.h),
                  ],
                ),
              ],
            ).paddingSymmetrical(24.h, 0.h).onPress(() {
              Navigator.of(context).push(
                FadePage(
                  page: MyAppointmentsPage(),
                ),
              );
            }),
          ],
        );
      case 1:
        //Insurance Tab Data
        return Column(
          children: [
            Consumer<InsuranceViewModel>(builder: (context, insuranceVM, child) {
              return insuranceVM.isInsuranceLoading
                  ? const MoviesShimmerWidget().paddingSymmetrical(24.h, 0.0)
                  : PatientInsuranceCard(
                      insuranceCardDetailsModel: insuranceVM.patientInsuranceList.first,
                      isInsuranceExpired: DateTime.now().isBefore(DateUtil.convertStringToDate(insuranceVM.patientInsuranceList.first.cardValidTo)));
            }),
            SizedBox(height: 10.h),
            GridView(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, crossAxisSpacing: 13, mainAxisSpacing: 13),
              physics: NeverScrollableScrollPhysics(),
              padding: EdgeInsets.only(top: 12),
              shrinkWrap: true,
              children: [
                MedicalFileCard(label: "Update Insurance", textColor: AppColors.blackColor, backgroundColor: AppColors.whiteColor, svgIcon: AppAssets.eye_result_icon),
                MedicalFileCard(label: "Insurance Approvals", textColor: AppColors.blackColor, backgroundColor: AppColors.whiteColor, svgIcon: AppAssets.eye_result_icon),
                MedicalFileCard(label: "My Invoices   List", textColor: AppColors.blackColor, backgroundColor: AppColors.whiteColor, svgIcon: AppAssets.eye_result_icon),
                MedicalFileCard(label: "Ancillary Orders List", textColor: AppColors.blackColor, backgroundColor: AppColors.whiteColor, svgIcon: AppAssets.eye_result_icon),
              ],
            ).paddingSymmetrical(24.h, 0.0),
            SizedBox(height: 16.h),
          ],
        );
      case 2:
        return Container();
      case 3:
        return Container();
      default:
        return Container();
    }
  }
}
