import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:supertokens_flutter/http.dart' as http;

import '../../../main.dart';
import '../../../mih_apis/mih_api_calls.dart';
import '../../../mih_components/mih_inputs_and_buttons/mih_button.dart';
import '../../../mih_components/mih_inputs_and_buttons/mih_date_input.dart';
import '../../../mih_components/mih_inputs_and_buttons/mih_text_input.dart';
import '../../../mih_components/mih_inputs_and_buttons/mih_time_input.dart';
import '../../../mih_components/mih_layout/mih_window.dart';
import '../../../mih_components/mih_pop_up_messages/mih_error_message.dart';
import '../../../mih_components/mih_pop_up_messages/mih_success_message.dart';
import '../../../mih_components/mih_pop_up_messages/mih_warning_message.dart';
import '../../../mih_env/env.dart';
import '../../../mih_objects/app_user.dart';
import '../../../mih_objects/arguments.dart';
import '../../../mih_objects/business.dart';
import '../../../mih_objects/patients.dart';

class BuildPatientsList extends StatefulWidget {
  final List<Patient> patients;
  final AppUser signedInUser;
  final Business? business;
  final BusinessArguments arguments;

  const BuildPatientsList({
    super.key,
    required this.patients,
    required this.signedInUser,
    required this.business,
    required this.arguments,
  });

  @override
  State<BuildPatientsList> createState() => _BuildPatientsListState();
}

class _BuildPatientsListState extends State<BuildPatientsList> {
  TextEditingController dateController = TextEditingController();
  TextEditingController timeController = TextEditingController();
  TextEditingController idController = TextEditingController();
  TextEditingController fnameController = TextEditingController();
  TextEditingController lnameController = TextEditingController();
  TextEditingController accessStatusController = TextEditingController();
  final baseAPI = AppEnviroment.baseApiUrl;

  Future<void> addPatientAppointmentAPICall(int index) async {
    var response = await http.post(
      Uri.parse("$baseAPI/queue/insert/"),
      headers: <String, String>{
        "Content-Type": "application/json; charset=UTF-8"
      },
      body: jsonEncode(<String, dynamic>{
        "business_id": widget.business!.business_id,
        "app_id": widget.patients[index].app_id,
        "date": dateController.text,
        "time": timeController.text,
        "access": "pending",
      }),
    );
    if (response.statusCode == 201) {
      // Navigator.pushNamed(context, '/patient-manager/patient',
      //     arguments: widget.signedInUser);
      String message =
          "The appointment has been successfully booked!\n\nAn approval request as been sent to the patient.Once the access request has been approved, you will be able to access the patients profile. ou can check the status of your request in patient queue under the appointment.";
      //     "${fnameController.text} ${lnameController.text} patient profiole has been successfully added!\n";
      Navigator.pop(context);
      Navigator.pop(context);
      setState(() {
        dateController.text = "";
        timeController.text = "";
      });
      Navigator.of(context).pushNamed(
        '/patient-manager',
        arguments: BusinessArguments(
          widget.arguments.signedInUser,
          widget.arguments.businessUser,
          widget.arguments.business,
        ),
      );
      successPopUp(message);
      addAccessReviewNotificationAPICall(index);
    } else {
      internetConnectionPopUp();
    }
  }

  Future<void> addPatientAccessAPICall(int index) async {
    var response = await http.post(
      Uri.parse("$baseAPI/access-requests/insert/"),
      headers: <String, String>{
        "Content-Type": "application/json; charset=UTF-8"
      },
      body: jsonEncode(<String, dynamic>{
        "business_id": widget.business!.business_id,
        "app_id": widget.patients[index].app_id,
        "date": dateController.text,
        "time": timeController.text,
        "access": "pending",
      }),
    );
    if (response.statusCode == 201) {
      // Navigator.pushNamed(context, '/patient-manager/patient',
      //     arguments: widget.signedInUser);
      String message =
          "The appointment has been successfully booked!\n\nAn approval request as been sent to the patient.Once the access request has been approved, you will be able to access the patients profile. ou can check the status of your request in patient queue under the appointment.";
      //     "${fnameController.text} ${lnameController.text} patient profiole has been successfully added!\n";
      Navigator.pop(context);
      Navigator.pop(context);
      setState(() {
        dateController.text = "";
        timeController.text = "";
      });
      Navigator.of(context).pushNamed(
        '/patient-manager',
        arguments: BusinessArguments(
          widget.arguments.signedInUser,
          widget.arguments.businessUser,
          widget.arguments.business,
        ),
      );
      successPopUp(message);
      addAccessReviewNotificationAPICall(index);
    } else {
      internetConnectionPopUp();
    }
  }

  Future<void> addAccessReviewNotificationAPICall(int index) async {
    var response = await http.post(
      Uri.parse("$baseAPI/notifications/insert/"),
      headers: <String, String>{
        "Content-Type": "application/json; charset=UTF-8"
      },
      body: jsonEncode(<String, dynamic>{
        "app_id": widget.patients[index].app_id,
        "notification_type": "New Appointment Booked",
        "notification_message":
            "A new Appointment has been booked by ${widget.arguments.business!.Name} for the ${dateController.text} ${timeController.text}. Please approve the Access Review request.",
        "action_path": "/access-review",
      }),
    );
    if (response.statusCode == 201) {
      // Navigator.pushNamed(context, '/patient-manager/patient',
      //     arguments: widget.signedInUser);
    } else {
      internetConnectionPopUp();
    }
  }

  void internetConnectionPopUp() {
    showDialog(
      context: context,
      builder: (context) {
        return const MIHErrorMessage(errorType: "Internet Connection");
      },
    );
  }

  void successPopUp(String message) {
    showDialog(
      context: context,
      builder: (context) {
        return MIHSuccessMessage(
          successType: "Success",
          successMessage: message,
        );
      },
    );
  }

  void submitApointment(int index) {
    MIHApiCalls.addAppointmentAPICall(
      widget.arguments.business!.business_id,
      widget.patients[index].app_id,
      false,
      dateController.text,
      timeController.text,
      widget.arguments,
      context,
    );
    //addPatientAppointmentAPICall(index);
  }

  bool isAppointmentFieldsFilled() {
    if (dateController.text.isEmpty || timeController.text.isEmpty) {
      return false;
    } else {
      return true;
    }
  }

  void appointmentPopUp(int index) {
    var firstLetterFName = widget.patients[index].first_name[0];
    var firstLetterLName = widget.patients[index].last_name[0];
    var fnameStar = '*' * 8;
    var lnameStar = '*' * 8;

    setState(() {
      idController.text = widget.patients[index].id_no;
      fnameController.text = firstLetterFName + fnameStar;
      lnameController.text = firstLetterLName + lnameStar;
    });
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => MIHWindow(
        fullscreen: false,
        windowTitle: "Patient Appointment",
        windowTools: const [],
        onWindowTapClose: () {
          Navigator.pop(context);
        },
        windowBody: [
          MIHTextField(
            controller: idController,
            hintText: "ID No.",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTextField(
            controller: fnameController,
            hintText: "First Name",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTextField(
            controller: lnameController,
            hintText: "Surname",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHDateField(
            controller: dateController,
            lableText: "Date",
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTimeField(
            controller: timeController,
            lableText: "Time",
            required: true,
          ),
          const SizedBox(height: 30.0),
          SizedBox(
            width: 300,
            height: 50,
            child: MIHButton(
              buttonText: "Book",
              buttonColor:
                  MzanziInnovationHub.of(context)!.theme.secondaryColor(),
              textColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
              onTap: () {
                //print("here1");
                bool filled = isAppointmentFieldsFilled();
                //print("fields filled: $filled");
                if (filled) {
                  //print("here2");
                  submitApointment(index);
                  //print("here3");
                } else {
                  showDialog(
                    context: context,
                    builder: (context) {
                      return const MIHErrorMessage(errorType: "Input Error");
                    },
                  );
                }
              },
            ),
          )
        ],
      ),
    );
  }

  void noAccessWarning() {
    showDialog(
      context: context,
      builder: (context) {
        return const MIHWarningMessage(warningType: "No Access");
      },
    );
  }

  Future<bool> hasAccessToProfile(int index) async {
    var hasAccess = false;
    await MIHApiCalls.checkBusinessAccessToPatient(
            widget.business!.business_id, widget.patients[index].app_id)
        .then((results) {
      if (results.isEmpty) {
        setState(() {
          hasAccess = false;
        });
      } else if (results[0].status == "approved") {
        setState(() {
          hasAccess = true;
        });
      } else {
        setState(() {
          hasAccess = false;
        });
      }
    });
    return hasAccess;
  }

  Future<String> getAccessStatusOfProfile(int index) async {
    var accessStatus = "";
    await MIHApiCalls.checkBusinessAccessToPatient(
            widget.business!.business_id, widget.patients[index].app_id)
        .then((results) {
      if (results.isEmpty) {
        setState(() {
          accessStatus = "";
        });
      } else {
        setState(() {
          accessStatus = results[0].status;
        });
      }
    });
    return accessStatus;
  }

  void patientProfileChoicePopUp(int index) async {
    var hasAccess = false;
    String accessStatus = "";
    await hasAccessToProfile(index).then((result) {
      setState(() {
        hasAccess = result;
      });
    });
    await getAccessStatusOfProfile(index).then((result) {
      setState(() {
        accessStatus = result;
      });
    });
    // print(accessStatus);
    // print(hasAccess);
    var firstLetterFName = widget.patients[index].first_name[0];
    var firstLetterLName = widget.patients[index].last_name[0];
    var fnameStar = '*' * 8;
    var lnameStar = '*' * 8;
    if (accessStatus == "") {
      accessStatus = "No Access";
    }
    setState(() {
      idController.text = widget.patients[index].id_no;
      fnameController.text = firstLetterFName + fnameStar;
      lnameController.text = firstLetterLName + lnameStar;
      accessStatusController.text = accessStatus.toUpperCase();
    });
    //print(accessStatus);
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => MIHWindow(
        fullscreen: false,
        windowTitle: "Patient Profile",
        windowTools: const [],
        onWindowTapClose: () {
          Navigator.pop(context);
        },
        windowBody: [
          MIHTextField(
            controller: idController,
            hintText: "ID No.",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTextField(
            controller: fnameController,
            hintText: "First Name",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTextField(
            controller: lnameController,
            hintText: "Surname",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 10.0),
          MIHTextField(
            controller: accessStatusController,
            hintText: "Access Status",
            editable: false,
            required: true,
          ),
          const SizedBox(height: 20.0),
          Visibility(
            visible: !hasAccess,
            child: Column(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  "Important Notice: Requesting Patient Profile Access",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: MzanziInnovationHub.of(context)!.theme.errorColor(),
                  ),
                ),
                Text(
                  "You are about to request access to a patient's profile. Please be aware of the following important points:",
                  style: TextStyle(
                    fontWeight: FontWeight.normal,
                    color: MzanziInnovationHub.of(context)!.theme.errorColor(),
                  ),
                ),
                SizedBox(
                  width: 600,
                  child: Text(
                    "1. Permanent Access: Once the patient accepts your access request, it will become permanent.",
                    style: TextStyle(
                      fontWeight: FontWeight.normal,
                      color:
                          MzanziInnovationHub.of(context)!.theme.errorColor(),
                    ),
                  ),
                ),
                SizedBox(
                  width: 600,
                  child: Text(
                    "2. Shared Information: Any updates you make to the patient's profile will be visible to others who have access to the profile.",
                    style: TextStyle(
                      fontWeight: FontWeight.normal,
                      color:
                          MzanziInnovationHub.of(context)!.theme.errorColor(),
                    ),
                  ),
                ),
                SizedBox(
                  width: 600,
                  child: Text(
                    "3. Irreversible Access: Once granted, you cannot revoke your access to the patient's profile.",
                    style: TextStyle(
                      fontWeight: FontWeight.normal,
                      color:
                          MzanziInnovationHub.of(context)!.theme.errorColor(),
                    ),
                  ),
                ),
                Text(
                  "By pressing the \"Request Access\" button you accept the above terms.",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: MzanziInnovationHub.of(context)!.theme.errorColor(),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 20.0),
          Wrap(runSpacing: 10, spacing: 10, children: [
            Visibility(
              visible: hasAccess,
              child: SizedBox(
                width: 300,
                height: 50,
                child: MIHButton(
                  buttonText: "Book Appointment",
                  buttonColor:
                      MzanziInnovationHub.of(context)!.theme.secondaryColor(),
                  textColor:
                      MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  onTap: () {
                    if (hasAccess) {
                      appointmentPopUp(index);
                    } else {
                      noAccessWarning();
                    }
                  },
                ),
              ),
            ),
            Visibility(
              visible: hasAccess,
              child: SizedBox(
                width: 300,
                height: 50,
                child: MIHButton(
                  buttonText: "View Patient Profile",
                  buttonColor:
                      MzanziInnovationHub.of(context)!.theme.successColor(),
                  textColor:
                      MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  onTap: () {
                    if (hasAccess) {
                      Navigator.of(context)
                          .pushNamed('/patient-manager/patient',
                              arguments: PatientViewArguments(
                                widget.signedInUser,
                                widget.patients[index],
                                widget.arguments.businessUser,
                                widget.business,
                                "business",
                              ));
                    } else {
                      noAccessWarning();
                    }
                  },
                ),
              ),
            ),
            Visibility(
              visible: !hasAccess && accessStatus == "No Access",
              child: SizedBox(
                width: 300,
                height: 50,
                child: MIHButton(
                  buttonText: "Request Access",
                  buttonColor:
                      MzanziInnovationHub.of(context)!.theme.successColor(),
                  textColor:
                      MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  onTap: () {
                    //print("Send access Request...");
                    // MIHApiCalls.addPatientAccessAPICall(
                    //   widget.business!.business_id,
                    //   widget.patients[index].app_id,
                    //   "patient",
                    //   widget.business!.Name,
                    //   widget.arguments,
                    //   context,
                    // );
                  },
                ),
              ),
            ),
            Visibility(
              visible: !hasAccess && accessStatus == "declined",
              child: SizedBox(
                width: 300,
                height: 50,
                child: MIHButton(
                  buttonText: "Re-apply",
                  buttonColor:
                      MzanziInnovationHub.of(context)!.theme.successColor(),
                  textColor:
                      MzanziInnovationHub.of(context)!.theme.primaryColor(),
                  onTap: () {
                    print("Send rewaply access Request...");
                    // MIHApiCalls.reapplyPatientAccessAPICall(
                    //   widget.business!.business_id,
                    //   widget.patients[index].app_id,
                    //   widget.arguments,
                    //   context,
                    // );
                  },
                ),
              ),
            ),
            Visibility(
              visible: !hasAccess && accessStatus == "pending",
              child: const SizedBox(
                width: 500,
                //height: 50,
                child: Text(
                    "Patient has not approved access to their profile. Once access has been approved you can book and appointment or view their profile."),
              ),
            ),
          ])
        ],
      ),
    );
  }

  Widget isMainMember(int index) {
    //var matchRE = RegExp(r'^[a-z]+$');
    var firstLetterFName = widget.patients[index].first_name[0];
    var firstLetterLName = widget.patients[index].last_name[0];
    var fnameStar = '*' * 8;
    var lnameStar = '*' * 8;

    if (widget.patients[index].medical_aid_main_member == "Yes") {
      return Row(
        mainAxisSize: MainAxisSize.max,
        children: [
          Text(
            "$firstLetterFName$fnameStar $firstLetterLName$lnameStar",
            style: TextStyle(
              color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Icon(
            Icons.star_border_rounded,
            color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
          ),
        ],
      );
    } else {
      return Text(
        "$firstLetterFName$fnameStar $firstLetterLName$lnameStar",
        style: TextStyle(
          color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
        ),
      );
    }
  }

  Widget hasMedicalAid(int index) {
    var medAidNoStar = '*' * 8;
    if (widget.patients[index].medical_aid == "Yes") {
      return ListTile(
        title: isMainMember(index),
        subtitle: Text(
          "ID No.: ${widget.patients[index].id_no}\nMedical Aid No.: $medAidNoStar",
          style: TextStyle(
            color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
          ),
        ),
        onTap: () {
          patientProfileChoicePopUp(index);
          // setState(() {
          //   appointmentPopUp(index);
          //   // Add popup to add patienmt to queue
          //   // Navigator.of(context).pushNamed('/patient-manager/patient',
          //   //     arguments: PatientViewArguments(
          //   //         widget.signedInUser, widget.patients[index], "business"));
          // });
        },
        trailing: Icon(
          Icons.arrow_forward,
          color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
        ),
      );
    } else {
      return ListTile(
        title: isMainMember(index),
        subtitle: Text(
          "ID No.: ${widget.patients[index].id_no}\nMedical Aid No.: $medAidNoStar",
          style: TextStyle(
            color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
          ),
        ),
        onTap: () {
          patientProfileChoicePopUp(index);
          // setState(() {
          //   appointmentPopUp(index);
          //   // Navigator.of(context).pushNamed('/patient-manager/patient',
          //   //     arguments: PatientViewArguments(
          //   //         widget.signedInUser, widget.patients[index], "business"));
          // });
        },
        trailing: Icon(
          Icons.add,
          color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
        ),
      );
    }
  }

  @override
  void dispose() {
    dateController.dispose();
    timeController.dispose();
    idController.dispose();
    fnameController.dispose();
    lnameController.dispose();
    accessStatusController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      separatorBuilder: (BuildContext context, index) {
        return Divider(
          color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
        );
      },
      itemCount: widget.patients.length,
      itemBuilder: (context, index) {
        //final patient = widget.patients[index].id_no.contains(widget.searchString);
        //print(index);
        return hasMedicalAid(index);
      },
    );
  }
}
