import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.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/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/patient_appointment_history_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/widgets/doctor_row_appointment_rating.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/loader/bottomsheet_loader.dart';
import 'package:provider/provider.dart';

class AppointmentRatingWidget extends StatefulWidget {
  const AppointmentRatingWidget({super.key, required this.patientAppointmentHistoryResponseModel});

  final PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;

  @override
  State<AppointmentRatingWidget> createState() => _AppointmentRatingWidgetState();
}

class _AppointmentRatingWidgetState extends State<AppointmentRatingWidget> {
  late MyAppointmentsViewModel myAppointmentsViewModel;
  late BookAppointmentsViewModel bookAppointmentsViewModel;

  int rating = 0;
  String notes = "";

  @override
  Widget build(BuildContext context) {
    myAppointmentsViewModel = Provider.of<MyAppointmentsViewModel>(context, listen: false);
    bookAppointmentsViewModel = Provider.of<BookAppointmentsViewModel>(context, listen: false);
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.6,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Scrollable main content
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(top: 0.0, left: 0, right: 0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  // Doctor row
                  Container(
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: AppColors.whiteColor,
                        borderRadius: 24.r,
                        hasShadow: false,
                      ),
                      child: BuildDoctorRowAppointmentRating(
                        patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel,
                      )),

                  SizedBox(height: 16),

                  // Rating box
                  Container(
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.whiteColor,
                      borderRadius: 24.r,
                      hasShadow: false,
                    ),
                    width: double.infinity,
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          LocaleKeys.rateTheDoctor.tr(context: context).toText16(isBold: true),
                          SizedBox(height: 12),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              ...List.generate(
                                5,
                                (index) => AnimatedSwitcher(
                                  duration: Duration(milliseconds: 1000),
                                  switchInCurve: Curves.elasticOut,
                                  switchOutCurve: Curves.elasticIn,
                                  transitionBuilder: (Widget child, Animation<double> animation) {
                                    return ScaleTransition(child: child, scale: animation);
                                  },
                                  child: Container(
                                    key: ValueKey<int>(rating),
                                    child: IconButton(
                                        iconSize: 45.0,
                                        onPressed: () {
                                          setState(() {
                                            rating = index + 1;
                                          });
                                        },
                                        color: rating >= (index + 1) ? Color.fromRGBO(255, 186, 0, 1.0) : Colors.grey[400],
                                        icon: Icon(rating >= (index + 1) ? Icons.star : Icons.star)),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),

                  SizedBox(height: 12),

                  // Note text field
                  Container(
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: AppColors.whiteColor,
                        borderRadius: 24.r,
                        hasShadow: false,
                      ),
                      child: Padding(
                          padding: EdgeInsets.all(16.0),
                          child: TextField(
                            maxLines: 4,
                            decoration: InputDecoration.collapsed(
                                hintText: LocaleKeys.notes.tr(context: context),
                                hintStyle: TextStyle(fontSize: 16.f, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.64, height: 23 / 16)),
                            onChanged: (value) {
                              setState(() {
                                notes = value;
                              });
                            },
                          ))),
                ],
              ),
            ),
          ),
          // Bottom action buttons pinned to bottom
          SafeArea(
            top: false,
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 12.0),
              child: Row(
                children: [
                  Expanded(
                    child: CustomButton(
                      text: LocaleKeys.later.tr(context: context),
                      backgroundColor: Color(0xffFEE9EA),
                      borderColor: Color(0xffFEE9EA),
                      textColor: Color(0xffED1C2B),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                  ),
                  SizedBox(width: 10),
                  Expanded(
                    child: CustomButton(
                      text: LocaleKeys.submit.tr(context: context),
                      isDisabled: rating == 0,
                      onPressed: rating == 0 ? null : () async {
                        // Set up clinic rating and show clinic rating view
                        // appointmentRatingViewModel!.setTitle(LocaleKeys.rateDoctor.tr(context: context),);
                        // appointmentRatingViewModel!.setSubTitle(LocaleKeys.howWasYourLastVisitWithDoctor.tr(context: context),);
                        // appointmentRatingViewModel!.setClinicOrDoctor(true);

                        LoaderBottomSheet.showLoader();
                        await myAppointmentsViewModel.submitDoctorRating(docRate: rating, docNote: notes, patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel);
                        // await  appointmentRatingViewModel!.submitClinicRating(clinicRate: rating, clinicNote: note);
                        LoaderBottomSheet.hideLoader();
                        Navigator.pop(context);
                        setState(() {});
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
