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/hmg_services/hmg_services_view_model.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/ui_models/covid_questionnare_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/covid19test/covid_review_screen.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/CustomSwitch.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';

import 'package:provider/provider.dart';

class Covid19Questionnaire extends StatefulWidget {
 final HospitalsModel selectedHospital;
   const Covid19Questionnaire({super.key, required this.selectedHospital});

  @override
  State<Covid19Questionnaire> createState() => _Covid19QuestionnaireState();
}

class _Covid19QuestionnaireState extends State<Covid19Questionnaire> {
  late HmgServicesViewModel hmgServicesViewModel;
  List<CovidQuestionnaireModel> qaList = [];


  @override
  void initState() {
    hmgServicesViewModel = Provider.of<HmgServicesViewModel>(context, listen: false);
    scheduleMicrotask(() {
      setState(() {
        qaList = hmgServicesViewModel.getQuestionsFromJson();
      });
    });
    super.initState();
  }

  void _toggleAnswer(int index, bool value) {
    setState(() {
      qaList[index].ans = value ? 1 : 0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return   CollapsingListView(
                  title: "COVID-19",
                bottomChild: 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: CustomButton(
                  text: LocaleKeys.next.tr(context: context),
                  onPressed: () {
                  moveToNextPage(context);
                  },
                  backgroundColor: AppColors.primaryRedColor,
                  borderColor: AppColors.primaryRedColor,
                  textColor: AppColors.whiteColor,
                  fontSize: 16.f,
                  isBold: true,
                  borderRadius: 12.r,
                  height: 56.h,
                )),
                  child: SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.all(24.w),
                      child: Column(
                        children: [
                          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: [
                                  "Please answer below questionnaire:".toText14(
                                    color: AppColors.textColor,
                                    weight: FontWeight.w600,
                                  ),
                                  SizedBox(height: 20.h),
                                  // Question list
                                  ListView.separated(
                                    shrinkWrap: true,
                                    physics: const NeverScrollableScrollPhysics(),
                                    itemCount: qaList.length,
                                    separatorBuilder: (context, index) => SizedBox(height: 16.h),
                                    itemBuilder: (context, index) {
                                      final question = qaList[index];
                                      final isAnswerYes = question.ans ==  1;

                                      return Row(
                                        children: [
                                          Expanded(
                                            child: (question.questionEn ?? '').toText14(
                                              color: AppColors.textColor,
                                              weight: FontWeight.w400,
                                            ),
                                          ),
                                          SizedBox(width: 12.w),
                                          CustomSwitch(
                                            value: isAnswerYes,
                                            onChanged: (value) => _toggleAnswer(index, value),
                                          ),
                                        ],
                                      );
                                    },
                                  ),
                                ],
                              ),
                            ),
                          ),
                          SizedBox(height: 16.h),
                          // Next button

                        ],
                      ),
                    ),

                  ),
              );


  }
  moveToNextPage(BuildContext context) async{
    LoaderBottomSheet.showLoader();
   await hmgServicesViewModel.getCovidProcedureList();
   await hmgServicesViewModel.getPaymentInfo(procedureID: hmgServicesViewModel.covidTestProcedureList[0].procedureId!);
   LoaderBottomSheet.hideLoader();
    Navigator.of(context)
        .push(
      CustomPageRoute(
        page: CovidReviewScreen(selectedHospital:  widget.selectedHospital, qaList:  qaList),
      ),
    );
  }
}
