import 'package:diplomaticquarterapp/models/Appointments/DentalChiefComplaintsModel.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/SearchResults.dart';
import 'package:diplomaticquarterapp/services/clinic_services/get_clinic_service.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class DentalComplaintCard extends StatefulWidget {
  final ListDentalChiefComplain listDentalChiefComplain;
  VoidCallback logAnalytics;
  var languageID;
  Function onSelectedMethod;
  bool isDoctorNameSearch;

  DentalComplaintCard({@required this.listDentalChiefComplain, this.languageID, this.onSelectedMethod, this.isDoctorNameSearch = false});

  @override
  _DentalComplaintCardState createState() => _DentalComplaintCardState();
}

class _DentalComplaintCardState extends State<DentalComplaintCard> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: InkWell(
        onTap: () {
          if (widget.logAnalytics != null) widget.logAnalytics();
          // if(widget.isDoctorNameSearch) {
          //   widget.onSelectedMethod();
          // } else {
          getChiefComplaintsList();
          // }
        },
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.all(14.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                child: Text(
                  widget.listDentalChiefComplain.name,
                  style: TextStyle(
                    fontSize: 16.0,
                    color: Colors.black,
                    letterSpacing: -0.64,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  getChiefComplaintsList() {
    List<DoctorList> doctorsList = [];
    List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital = List();

    GifLoaderDialogUtils.showMyDialog(context);
    ClinicListService service = new ClinicListService();
    service.getChiefComplaintDoctorList(widget.listDentalChiefComplain.iD, widget.listDentalChiefComplain.projectID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        print(res['List_DentalDoctorChiefComplaintMapping']);
        setState(() {
          doctorsList.clear();
          res['List_DentalDoctorChiefComplaintMapping'].forEach((v) {
            doctorsList.add(new DoctorList.fromJson(v));
          });

          doctorsList.forEach((element) {
            List<PatientDoctorAppointmentList> doctorByHospital = _patientDoctorAppointmentListHospital
                .where(
                  (elementClinic) => elementClinic.filterName == element.projectName,
                )
                .toList();

            if (doctorByHospital.length != 0) {
              _patientDoctorAppointmentListHospital[_patientDoctorAppointmentListHospital.indexOf(doctorByHospital[0])].patientDoctorAppointmentList.add(element);
            } else {
              _patientDoctorAppointmentListHospital
                  .add(PatientDoctorAppointmentList(filterName: element.projectName, distanceInKMs: element.projectDistanceInKiloMeters.toString(), patientDoctorAppointment: element));
            }
          });
          navigateToSearchResults(context, doctorsList, _patientDoctorAppointmentListHospital);
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  Future navigateToSearchResults(context, List<DoctorList> docList, List<PatientDoctorAppointmentList> patientDoctorAppointmentListHospital) async {
    Navigator.push(context, FadePage(page: SearchResults(doctorsList: docList, patientDoctorAppointmentListHospital: patientDoctorAppointmentListHospital, isLiveCareAppointment: false)));
  }
}
