import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.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/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

import 'covid_19_questionnaire.dart';

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

@override
State<Covid19LandingPage> createState() => _Covid19LandingPageState();
}

class _Covid19LandingPageState extends State<Covid19LandingPage> {

  late HabibWalletViewModel habibWalletVM;
  int? _selectedBranchIndex;

  @override
  void initState() {
    habibWalletVM = Provider.of<HabibWalletViewModel>(context, listen: false);
    scheduleMicrotask(() {
      getProjectList();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return  Scaffold(
        backgroundColor: AppColors.bgScaffoldColor,
        body: Column(
      children: [
        Expanded(
          child: CollapsingListView(
            title: "COVID-19",
            child: Padding(
              padding: EdgeInsets.all(24.w),
              child: SingleChildScrollView(
                child: Container(
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                    color: AppColors.whiteColor,
                    borderRadius: 24.r,
                    hasShadow: false,
                  ),
                  child: Padding(
                    padding: EdgeInsets.all(20.h),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        "Get the results in Few Hours"
                            .toText18(
                              color: AppColors.textColor,
                              weight: FontWeight.w600,
                            ),
                        SizedBox(height: 16.h),
                        LocaleKeys.covid_info
                            .tr()
                            .toText14(
                              color: AppColors.greyTextColor,
                              weight: FontWeight.w400,
                              height: 1.6,
                            ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
            color: AppColors.whiteColor,
            // borderRadius: 24.r,
            hasShadow: true,
          customBorder: BorderRadius.only(topLeft:Radius.circular(24.r) , topRight:Radius.circular(24.r))
          ),
          child: SizedBox(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CustomButton(
                  text: "Select Location",
                  onPressed: () {
                    _showBranchBottomSheet(context);
                  },
                  backgroundColor: AppColors.primaryRedColor,
                  borderColor: AppColors.primaryRedColor,
                  textColor: AppColors.whiteColor,
                  fontSize: 16.f,
                  isBold: true,
                  borderRadius: 12.r,
                  padding: EdgeInsets.symmetric(horizontal: 10.w),
                  height: 50.h,
                  iconSize: 18.h,
                ).paddingSymmetrical(16.h, 24.w),
              ],
            ),
          ),
        ),
      ],
    ));
  }
  void _showBranchBottomSheet(BuildContext context, ) {
    // Set first branch as selected by default
    setState(() {
      _selectedBranchIndex = 0;
    });

    showCommonBottomSheet(

      context,
      title: "Select Branch",
        height: ResponsiveExtension.screenHeight * 0.651,
      child: StatefulBuilder(
        builder: (context, setBottomSheetState) {
          return Consumer<HabibWalletViewModel>(
            builder: (context, habibWalletVM, child) {

              final hospitals = habibWalletVM.advancePaymentHospitals;
              if (hospitals.isEmpty) {
                return const Center(
                  child: Padding(
                    padding: EdgeInsets.all(16.0),
                    child: Text('No branches available'),
                  ),
                );
              }

              return Column(
                children: [
                  Expanded(
                    child:Container(
                      margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 12.h, bottom: 24.h),
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: AppColors.whiteColor,
                        hasShadow: true,
                        customBorder: BorderRadius.only(
                          topLeft: Radius.circular(24.r),
                          topRight: Radius.circular(24.r),
                        ),
                      ), child: ListView.separated(
                      shrinkWrap: true,
                      physics: const BouncingScrollPhysics(),
                      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
                      itemBuilder: (context, index) {
                        final branch = hospitals[index];
                        final isSelected = _selectedBranchIndex == index;
                        return GestureDetector(
                          onTap: () {
                            setBottomSheetState(() {
                              _selectedBranchIndex = index;
                            });
                            setState(() {
                              _selectedBranchIndex = index;
                            });
                          },
                          child: Container(

                            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
                            child: Row(
                              children: [
                                // Radio button
                                Container(
                                  width: 20.w,
                                  height: 20.h,
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    border: Border.all(
                                      color: isSelected
                                          ? AppColors.primaryRedColor
                                          : AppColors.greyTextColor.withValues(alpha: 0.3),
                                      width: 2,
                                    ),
                                  ),
                                  child: isSelected
                                      ? Center(
                                          child: Container(
                                            width: 10.w,
                                            height: 10.h,
                                            decoration: BoxDecoration(
                                              shape: BoxShape.circle,
                                              color: AppColors.primaryRedColor,
                                            ),
                                          ),
                                        )
                                      : null,
                                ),
                                SizedBox(width: 12.w),
                                // Branch details
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [

                                      SizedBox(height: 4.h),
                                      (branch.desciption ?? 'Unknown').toText14(
                                        color: AppColors.textColor,
                                        weight: FontWeight.w600,
                                      ),
                                    ],
                                  ),
                                ),
                                // Location icon

                              ],
                            ),
                          ),
                        );
                      },
                      separatorBuilder: (context, index) => SizedBox(height: 12.h),
                      itemCount: hospitals.length,
                    )),
                  ),
                  // Next button
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 16.h),
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.whiteColor,
                      hasShadow: true,
                      customBorder: BorderRadius.only(
                        topLeft: Radius.circular(24.r),
                        topRight: Radius.circular(24.r),
                      ),
                    ),
                    child: SafeArea(
                      top: false,
                      child: CustomButton(
                        text: LocaleKeys.next.tr(context: context),
                        onPressed: (){

                          Navigator.of(context)
                              .push(
                            CustomPageRoute(
                              page: Covid19Questionnaire(selectedHospital: hospitals[_selectedBranchIndex!],),
                            ),
                          );

                        },
                        backgroundColor: _selectedBranchIndex != null
                            ? AppColors.primaryRedColor
                            : AppColors.greyTextColor.withValues(alpha: 0.3),
                        borderColor: _selectedBranchIndex != null
                            ? AppColors.primaryRedColor
                            : AppColors.greyTextColor.withValues(alpha: 0.3),
                        textColor: AppColors.whiteColor,
                        fontSize: 16.f,
                        isBold: true,
                        borderRadius: 12.r,
                        height: 56.h,
                      ),
                    ),
                  ),
                ],
              );
            },
          );
        },
      ),

    );
  }

  getProjectList() async{
    await habibWalletVM.getProjectsList();
  }
}


