import 'package:auto_size_text/auto_size_text.dart';
import 'package:hmg_patient_app/config/size_config.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/models/Appointments/OBGyneProcedureListResponse.dart';
import 'package:hmg_patient_app/pages/BookAppointment/widgets/DoctorView.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ResultByDoctor extends StatefulWidget {
  List<DoctorList> doctorsList = [];
  List<DoctorList>? patientDoctorAppointmentListHospital;
  final bool isLiveCareAppointment;
  final bool isObGyneAppointment;
  final bool isDoctorNameSearch;
  final bool isDoctorSearchResult;
  final bool showNearestAppointment;
  final bool nearestAppointmentDoctors;
  final OBGyneProcedureListResponse? obGyneProcedureListResponse;
  final Function(bool)? refreshDoctorList;

  ResultByDoctor({
    required this.doctorsList,
    required this.patientDoctorAppointmentListHospital,
    required this.isLiveCareAppointment,
    required this.isObGyneAppointment,
    required this.isDoctorNameSearch,
    required this.isDoctorSearchResult,
    this.showNearestAppointment = false,
    this.nearestAppointmentDoctors = false,
    this.obGyneProcedureListResponse,
    this.refreshDoctorList
  });

  @override
  State<ResultByDoctor> createState() => _ResultByDoctorState();
}

class _ResultByDoctorState extends State<ResultByDoctor> {

  bool nearestAppo = false;

  @override
  void initState() {
    nearestAppo = widget.nearestAppointmentDoctors;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [

        Visibility(
          visible: widget.showNearestAppointment,
          child: Padding(
            padding: const EdgeInsets.only(left: 6, right: 6,),
            child: Row(
              children: <Widget>[
                Checkbox(
                  activeColor: CustomColors.accentColor,
                  value: nearestAppo,
                  onChanged: (bool? value) {
                    setState(() {
                     nearestAppo = value ?? false;
                    });
                    widget.refreshDoctorList?.call(nearestAppo);
                  },
                ),
                AutoSizeText(
                  TranslationBase.of(context).nearestAppo.trim(),
                  maxLines: 1,
                  minFontSize: 10,
                  style: TextStyle(
                    fontSize: SizeConfig.textMultiplier! * 1.4,
                    fontWeight: FontWeight.w600,
                    letterSpacing: -0.39,
                    height: 0.8,
                  ),
                ),
                // Text(TranslationBase.of(context).nearestAppo, style: TextStyle(fontSize: 14.0, letterSpacing: -0.56)),
              ],
            ),
          ),
        ),
        widget.patientDoctorAppointmentListHospital?.isNotEmpty == true
            ? Expanded(
              child: ListView.separated(
                addAutomaticKeepAlives: true,
                physics: BouncingScrollPhysics(),
                separatorBuilder: (context, index) {
                  return Material(
                    color: Color(0xFFf5f5f5),
                    child: SizedBox(
                      height: 12,
                    ),
                  );
                },
                itemBuilder: (context, index) {
                  final doctor = widget.patientDoctorAppointmentListHospital![index];

                  return Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 12.0),
                    child: DoctorView(
                        doctor: doctor,
                        isLiveCareAppointment: widget.isLiveCareAppointment,
                        isObGyneAppointment: widget.isObGyneAppointment,
                        isDoctorNameSearch: widget.isDoctorNameSearch,
                        obGyneProcedureListResponse: widget.obGyneProcedureListResponse,
                        isShowDate: false,
                        onTap: () {
                          context.read<ProjectViewModel>().analytics.appointment.book_appointment_select_doctor(appointment_type: 'regular', doctor: doctor);
                        }),
                  );
                },
                itemCount: widget.patientDoctorAppointmentListHospital?.length ?? 0,
              ),
            )
            : getNoDataWidget(context),
      ],
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
