import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/ask_doctor_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/widgets/DoctorView.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/medical/ask_doctor/request_type.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/widgets/others/app_expandable_notifier.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rating_bar/rating_bar.dart';

class AskDoctorPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BaseView<AskDoctorViewModel>(
      onModelReady: (model) => model.getMyDoctor(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        body: SingleChildScrollView(
          child: Container(
            margin: EdgeInsets.all(10),
            child: Column(
              children: [
                SizedBox(
                  height: 70,
                ),
                ...List.generate(
                  model.patientDoctorAppointmentListHospital.length,
                  (index) => AppExpandableNotifier(
                      title:
                          model.patientDoctorAppointmentListHospital[index].filterName,
                      bodyWidget: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children:
                            model.patientDoctorAppointmentListHospital[index].patientDoctorAppointmentList.map((doctor) {
                          return InkWell(
                            onTap: () {
                              model
                                  .getCallInfoHoursResult(
                                      doctorId: doctor.doctorID,
                                      projectId: doctor.projectID)
                                  .then((value) {
                                if (model.state != ViewState.ErrorLocal &&
                                    model.state != ViewState.Error) {
                                  //  Navigator.pop(context);
                                  Navigator.push(
                                    context,
                                    FadePage(
                                      page: RequestTypePage(doctorList: doctor,),
                                    ),
                                  );
                                } else {
                                  AppToast.showErrorToast(message: model.error);
                                }
                              });

                              ///.showProgressBar(
                              //                                   text: "Loading",
                              //                                   backgroundColor:
                              //                                   Colors.blue.withOpacity(0.6))
                            },
                            child: Card(
                              margin:
                                  EdgeInsets.fromLTRB(10.0, 16.0, 10.0, 8.0),
                              color: Colors.white,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10),
                              ),
                              child: Container(
                                decoration: BoxDecoration(),
                                padding: EdgeInsets.all(7.0),
                                width: MediaQuery.of(context).size.width,
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: <Widget>[
                                    Expanded(
                                      flex: 1,
                                      child: ClipRRect(
                                        borderRadius:
                                            BorderRadius.circular(100.0),
                                        child: Image.network(
                                            doctor.doctorImageURL,
                                            fit: BoxFit.fill,
                                            height: 60.0,
                                            width: 60.0),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 4,
                                      child: Container(
                                        width:
                                            MediaQuery.of(context).size.width *
                                                0.55,
                                        margin: EdgeInsets.fromLTRB(
                                            20.0, 10.0, 10.0, 0.0),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: <Widget>[
                                            if (doctor.doctorTitle != null)
                                              Text(
                                                  doctor.doctorTitle +
                                                      " " +
                                                      doctor.name,
                                                  style: TextStyle(
                                                      fontSize: 14.0,
                                                      color: Colors.grey[700],
                                                      letterSpacing: 1.0)),
                                            Container(
                                              margin: EdgeInsets.only(top: 3.0),
                                              child: Text(doctor.clinicName,
                                                  style: TextStyle(
                                                      fontSize: 12.0,
                                                      color: Colors.grey[600],
                                                      letterSpacing: 1.0)),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(top: 3.0),
                                              child: Text(doctor.projectName,
                                                  style: TextStyle(
                                                      fontSize: 12.0,
                                                      color: Colors.grey[600],
                                                      letterSpacing: 1.0)),
                                            ),
                                            if (doctor.speciality != null)
                                              Container(
                                                margin: EdgeInsets.only(
                                                    top: 3.0, bottom: 3.0),
                                                child: Text(
                                                    getDoctorSpeciality(
                                                            doctor.speciality)
                                                        .trim(),
                                                    style: TextStyle(
                                                        fontSize: 12.0,
                                                        color: Colors.grey[600],
                                                        letterSpacing: 1.0)),
                                              ),
                                            doctor.nearestFreeSlot != null
                                                ? Container(
                                                    margin: EdgeInsets.only(
                                                        top: 3.0, bottom: 3.0),
                                                    child: Text(
                                                        getDate(doctor
                                                            .nearestFreeSlot),
                                                        style: TextStyle(
                                                            fontSize: 14.0,
                                                            fontWeight:
                                                                FontWeight.bold,
                                                            color: Colors
                                                                .green[600],
                                                            letterSpacing:
                                                                1.0)),
                                                  )
                                                : Container(),
                                            Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              mainAxisSize: MainAxisSize.max,
                                              children: <Widget>[
                                                RatingBar.readOnly(
                                                  initialRating: doctor
                                                      .actualDoctorRate
                                                      .toDouble(),
                                                  size: 20.0,
                                                  filledColor:
                                                      Colors.yellow[700],
                                                  emptyColor: Colors.grey[500],
                                                  isHalfAllowed: true,
                                                  halfFilledIcon:
                                                      Icons.star_half,
                                                  filledIcon: Icons.star,
                                                  emptyIcon: Icons.star,
                                                ),
                                                Container(
                                                  child: Image.network(
                                                      doctor.nationalityFlagURL,
                                                      width: 25.0,
                                                      height: 25.0),
                                                ),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          );
                        }).toList(),
                      )),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  String getDoctorSpeciality(List<String> docSpecial) {
    String docSpeciality = "";
    docSpecial.forEach((v) {
      docSpeciality = docSpeciality + v + "\n";
    });
    return docSpeciality;
  }

  String getDate(String date) {
    DateTime dateObj = DateUtil.convertStringToDate(date);
    return DateUtil.getWeekDay(dateObj.weekday) +
        ", " +
        dateObj.day.toString() +
        " " +
        DateUtil.getMonth(dateObj.month) +
        " " +
        dateObj.year.toString() +
        " " +
        dateObj.hour.toString() +
        ":" +
        getMinute(dateObj);
  }

  String getMinute(DateTime dateObj) {
    if (dateObj.minute == 0) {
      return dateObj.minute.toString() + "0";
    } else {
      return dateObj.minute.toString();
    }
  }
}

