import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorRateDetails.dart';
import 'package:diplomaticquarterapp/models/header_model.dart';
import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/avatar/large_avatar.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:diplomaticquarterapp/widgets/my_rich_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:rating_bar/rating_bar.dart';

class DoctorHeader extends StatelessWidget {
  final HeaderModel headerModel;
  final VoidCallback onTap;
  final VoidCallback onRatingAndReviewTap;
  final bool showConfirmMessageDialog;
  final String buttonTitle;
  final String buttonIcon;
  final bool isNeedToShowButton;

  DoctorHeader(
      {Key key,
      @required this.headerModel,
      @required this.buttonTitle,
      @required this.onTap,
      this.isNeedToShowButton = true,
      this.buttonIcon,
      this.showConfirmMessageDialog = true,
      @required this.onRatingAndReviewTap})
      : super(key: key);

  ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    String _speciality = (headerModel?.speciality ?? []).length > 0 ? headerModel.speciality.first : "";
    projectViewModel = Provider.of(context);
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Padding(
            padding: EdgeInsets.only(left: 21, right: 21, bottom: 12),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                LargeAvatar(
                  name: headerModel.doctorName,
                  url: headerModel.doctorImageURL,
                  width: 51,
                  height: 51,
                ),
                SizedBox(width: 10),
                Expanded(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (_speciality.isNotEmpty)
                        Text(
                          _speciality,
                          style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.48, height: 18 / 12),
                        ),
                      headerModel.invoiceNo.isNotEmpty ? MyRichText(TranslationBase.of(context).invoiceNo + ":", headerModel.invoiceNo, projectViewModel.isArabic) : Container(),
                      MyRichText(TranslationBase.of(context).branch, headerModel.projectName, projectViewModel.isArabic),
                    ],
                  ),
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    if (headerModel.date != null)
                      Text(
                        DateUtil.getDayMonthYearDateFormatted(headerModel.date),
                        style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48, height: 18 / 12),
                      ),
                    if (headerModel.time != null)
                      Text(
                        headerModel.time ?? DateUtil.formatDateToTime(headerModel.date),
                        style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48, height: 18 / 12),
                      ),
                    if (headerModel?.nationalityFlagURL != null)
                      Padding(
                        padding: const EdgeInsets.only(top: 8.0),
                        child: Image.network(headerModel.nationalityFlagURL ?? "", height: 20),
                      ),
                  ],
                )
              ],
            ),
          ),
          if (headerModel.actualDoctorRate != null)
            SizedBox(
              height: 43,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () {
                      if ((headerModel?.totalReviews ?? 0) > 0) {
                        getDoctorRatingsDetails(context);
                      }
                    },
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(width: 21),
                        RatingBar.readOnly(
                          initialRating: headerModel.actualDoctorRate + 0.0,
                          size: 15.0,
                          filledColor: Color(0XFFD02127),
                          emptyColor: Color(0XFFD02127),
                          isHalfAllowed: true,
                          halfFilledIcon: Icons.star_half,
                          filledIcon: Icons.star,
                          emptyIcon: Icons.star_border,
                        ),
                        SizedBox(width: 6),
                        Text(
                          "${headerModel.totalReviews == null ? 0 : headerModel.totalReviews} ${TranslationBase.of(context).reviews}",
                          style: TextStyle(
                            fontSize: 12,
                            fontWeight: FontWeight.w600,
                            color: (headerModel?.totalReviews ?? 0) > 0 ? Colors.blue[600] : Color(0xff2B353E),
                            letterSpacing: -0.48,
                            height: 18 / 12,
                            decoration: (headerModel?.totalReviews ?? 0) > 0 ? TextDecoration.underline : null,
                          ),
                        ),
                      ],
                    ),
                  ),
                  isNeedToShowButton
                      ? InkWell(
                          onTap: () {
                            if (showConfirmMessageDialog)
                              showConfirmMessage(context, onTap, headerModel.email);
                            else
                              onTap();
                          },
                          child: Container(
                            padding: EdgeInsets.only(top: 10, bottom: 10, right: 21, left: 15),
                            decoration: BoxDecoration(
                                color: Color(0XFFD02127), borderRadius: projectViewModel.isArabic ? BorderRadius.only(topRight: Radius.circular(10)) : BorderRadius.only(topLeft: Radius.circular(10))),
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                SvgPicture.asset(buttonIcon ?? 'assets/images/new/email.svg', width: 19.0),
                                SizedBox(width: 6),
                                Text(
                                  buttonTitle == null ? TranslationBase.of(context).sendEmail : buttonTitle,
                                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Colors.white, letterSpacing: -0.64, height: 1),
                                ),
                              ],
                            ),
                          ),
                        )
                      : Container(),
                ],
              ),
            )
        ],
      ),
    );
  }

  void showConfirmMessage(BuildContext context, GestureTapCallback onTap, String email) {
    showDialog(
      context: context,
      builder: (cxt) => ConfirmSendEmailDialog(
        email: email,
        onTapSendEmail: () {
          onTap();
        },
      ),
    );
  }

  void getDoctorRatingsDetails(context) {
    GifLoaderDialogUtils.showMyDialog(context);
    List<DoctorRateDetails> doctorDetailsList = List();

    DoctorsListService service = new DoctorsListService();
    service.getDoctorsRatingDetails(headerModel.doctorId, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        doctorDetailsList.clear();
        res['DoctorRatingDetailsList'].forEach((v) {
          doctorDetailsList.add(new DoctorRateDetails.fromJson(v));
        });
        this.headerModel.decimalDoctorRate = res['DecimalDoctorRate'].toString();
        showRatingDialog(doctorDetailsList, context);
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  void showRatingDialog(List<DoctorRateDetails> doctorDetailsList, context) {
    showGeneralDialog(
        barrierColor: Colors.black.withOpacity(0.5),
        transitionBuilder: (context, a1, a2, widget) {
          final curvedValue = Curves.easeInOutBack.transform(a1.value) - 1.0;
          return Transform(
            transform: Matrix4.translationValues(0.0, curvedValue * 200, 0.0),
            child: Opacity(
              opacity: a1.value,
              child: Dialog(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 350.0,
                      color: Colors.white,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                              width: MediaQuery.of(context).size.width,
                              padding: EdgeInsets.all(20.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(TranslationBase.of(context).doctorRating, style: TextStyle(fontSize: 22.0, color: Colors.black, fontWeight: FontWeight.w600, letterSpacing: -0.64)),
                                  IconButton(
                                    icon: Icon(
                                      Icons.close,
                                      color: Colors.black,
                                    ),
                                    onPressed: () {
                                      Navigator.pop(context);
                                    },
                                  ),
                                ],
                              )),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Container(
                                  margin: EdgeInsets.symmetric(horizontal: 20.0),
                                  child: Text(this.headerModel.decimalDoctorRate != null ? this.headerModel.decimalDoctorRate : this.headerModel.actualDoctorRate.ceilToDouble().toString(),
                                      style: TextStyle(fontSize: 32.0, color: Colors.black, letterSpacing: -0.64, fontWeight: FontWeight.bold))),
                              Container(
                                margin: EdgeInsets.symmetric(horizontal: 20.0),
                                child: RatingBar.readOnly(
                                  initialRating: this.headerModel.decimalDoctorRate != null ? double.tryParse(this.headerModel.decimalDoctorRate) : this.headerModel.actualDoctorRate.toDouble(),
                                  size: 35.0,
                                  filledColor: Colors.yellow[700],
                                  emptyColor: Colors.grey[500],
                                  isHalfAllowed: true,
                                  halfFilledIcon: Icons.star_half,
                                  filledIcon: Icons.star,
                                  emptyIcon: Icons.star,
                                ),
                              ),
                            ],
                          ),
                          Container(
                              margin: EdgeInsets.symmetric(horizontal: 20.0),
                              transform: Matrix4.translationValues(0.0, -10.0, 0.0),
                              child: Text(this.headerModel.totalReviews.toString() + " " + TranslationBase.of(context).reviews,
                                  style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600))),
                          Container(
                            margin: EdgeInsets.only(top: 10.0),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                        width: 100.0,
                                        margin: EdgeInsets.only(top: 10.0, left: 15.0, right: 15.0),
                                        child: Text(TranslationBase.of(context).excellent, style: TextStyle(fontSize: 13.0, color: Colors.black, fontWeight: FontWeight.w600))),
                                    getRatingLine(doctorDetailsList[0].patientNumber, Colors.green[700]),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
                                  child: Text(getRatingWidth(doctorDetailsList[0].patientNumber).round().toString() + "%",
                                      style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600)),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                        width: 100.0,
                                        margin: EdgeInsets.only(top: 10.0, left: 15.0, right: 15.0),
                                        child: Text(TranslationBase.of(context).v_good, style: TextStyle(fontSize: 13.0, color: Colors.black, fontWeight: FontWeight.w600))),
                                    getRatingLine(doctorDetailsList[1].patientNumber, Color(0xffB7B723)),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
                                  child: Text(getRatingWidth(doctorDetailsList[1].patientNumber).round().toString() + "%",
                                      style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600)),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                        width: 100.0,
                                        margin: EdgeInsets.only(top: 10.0, left: 15.0, right: 15.0),
                                        child: Text(TranslationBase.of(context).good, style: TextStyle(fontSize: 13.0, color: Colors.black, fontWeight: FontWeight.w600))),
                                    getRatingLine(doctorDetailsList[2].patientNumber, Color(0xffEBA727)),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
                                  child: Text(getRatingWidth(doctorDetailsList[2].patientNumber).round().toString() + "%",
                                      style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600)),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                        width: 100.0,
                                        margin: EdgeInsets.only(top: 10.0, left: 15.0, right: 15.0),
                                        child: Text(TranslationBase.of(context).average, style: TextStyle(fontSize: 13.0, color: Colors.black, fontWeight: FontWeight.w600))),
                                    getRatingLine(doctorDetailsList[3].patientNumber, Color(0xffEB7227)),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
                                  child: Text(getRatingWidth(doctorDetailsList[3].patientNumber).round().toString() + "%",
                                      style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600)),
                                ),
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(bottom: 30.0),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                        width: 100.0,
                                        margin: EdgeInsets.only(top: 10.0, left: 15.0, right: 15.0),
                                        child: Text(TranslationBase.of(context).below_average, style: TextStyle(fontSize: 13.0, color: Colors.black, fontWeight: FontWeight.w600))),
                                    getRatingLine(doctorDetailsList[4].patientNumber, Color(0xffE20C0C)),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
                                  child: Text(getRatingWidth(doctorDetailsList[4].patientNumber).round().toString() + "%",
                                      style: TextStyle(fontSize: 14.0, color: Colors.black, fontWeight: FontWeight.w600)),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
        transitionDuration: Duration(milliseconds: 500),
        barrierDismissible: true,
        barrierLabel: '',
        context: context,
        pageBuilder: (context, animation1, animation2) {});
  }

  double getRatingWidth(int patientNumber) {
    var width = (patientNumber / this.headerModel.totalReviews) * 100;
    return width.roundToDouble();
  }

  Widget getRatingLine(int patientNumber, Color color) {
    return Container(
      margin: EdgeInsets.only(top: 10.0),
      child: Stack(children: [
        SizedBox(
          width: 135.0,
          height: 4.0,
          child: Container(
            color: Colors.grey[300],
          ),
        ),
        SizedBox(
          width: getRatingWidth(patientNumber) * 1.35,
          height: 4.0,
          child: Container(
            color: color,
          ),
        ),
      ]),
    );
  }
}
