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/features/insurance/insurance_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/insurance/widgets/patient_insurance_card.dart';
import 'package:hmg_patient_app_new/presentation/lab/search_lab_report.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/shimmer/movies_shimmer_widget.dart';
import 'package:provider/provider.dart';

import 'widgets/insurance_history.dart';

class InsuranceHomePage extends StatefulWidget {
  const InsuranceHomePage({super.key});

  @override
  State<InsuranceHomePage> createState() => _InsuranceHomePageState();
}

class _InsuranceHomePageState extends State<InsuranceHomePage> {
  late InsuranceViewModel insuranceViewModel;

  @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: LocaleKeys.insurance.tr(context: context).toText18(),
        backgroundColor: AppColors.bgScaffoldColor,
      ),
      body: SingleChildScrollView(
        child: Consumer<InsuranceViewModel>(builder: (context, insuranceVM, child) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  "${LocaleKeys.insurance.tr(context: context)} ${LocaleKeys.updateInsurance.tr(context: context)}".toText24(isBold: true),
                  CustomButton(
                    icon: AppAssets.insurance_history_icon,
                    iconColor: AppColors.primaryRedColor,
                    iconSize: 21.h,
                    text: LocaleKeys.history.tr(context: context),
                    onPressed: () {
                      insuranceVM.setIsInsuranceHistoryLoading(true);
                      insuranceVM.getPatientInsuranceCardHistory();
                      showCommonBottomSheet(context,
                          child: InsuranceHistory(), callBackFunc: () {}, title: "", height: ResponsiveExtension.screenHeight * 0.65, isCloseButtonVisible: false, isFullScreen: false);
                    },
                    backgroundColor: AppColors.primaryRedColor.withOpacity(0.1),
                    borderColor: AppColors.primaryRedColor.withOpacity(0.0),
                    textColor: AppColors.primaryRedColor,
                    fontSize: 14,
                    fontWeight: FontWeight.w600,
                    borderRadius: 12,
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 40.h,
                  ),
                ],
              ).paddingSymmetrical(24.h, 24.h),
              insuranceVM.isInsuranceLoading
                  ? const MoviesShimmerWidget().paddingSymmetrical(24.h, 0)
                  : PatientInsuranceCard(
                      insuranceCardDetailsModel: insuranceVM.patientInsuranceList.first,
                      isInsuranceExpired: DateTime.now().isAfter(DateUtil.convertStringToDate(insuranceVM.patientInsuranceList.first.cardValidTo))),
            ],
          );
        }),
      ),
    );
  }
}
