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:flutter/material.dart';
import 'package:smart_progress_bar/smart_progress_bar.dart';

// ignore: must_be_immutable
class DentalComplaintCard extends StatefulWidget {
  final ListDentalChiefComplain listDentalChiefComplain;
  var languageID;

  DentalComplaintCard(
      {@required this.listDentalChiefComplain, this.languageID});

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

class _DentalComplaintCardState extends State<DentalComplaintCard> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: InkWell(
        onTap: () {
          getChiefComplaintsList();
        },
        child: Card(
          color: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
          ),
          child: Row(
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width * 0.85,
                padding: EdgeInsets.all(12.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)),
                    ),
                  ],
                ),
              ),
              Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    Icon(Icons.arrow_forward_ios,
                        size: 20.0, color: Colors.black54),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

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

    ClinicListService service = new ClinicListService();
    service
        .getChiefComplaintDoctorList(widget.listDentalChiefComplain.iD,
            widget.listDentalChiefComplain.projectID, context)
        .then((res) {
      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) {
      print(err);
    }).showProgressBar(
            text: "Loading", backgroundColor: Colors.blue.withOpacity(0.6));
  }

  Future navigateToSearchResults(
      context,
      List<DoctorList> docList,
      List<PatientDoctorAppointmentList>
          patientDoctorAppointmentListHospital) async {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => SearchResults(
                doctorsList: docList,
                patientDoctorAppointmentListHospital:
                    patientDoctorAppointmentListHospital)));
  }
}
