import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/reports/Reports.dart';
import 'package:diplomaticquarterapp/core/service/medical/reports_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/locator.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/uitl/utils_new.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/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:rating_bar/rating_bar.dart';

class ReportListWidget extends StatelessWidget {
  final List<Reports> reportList;
  final String emailAddress;

  ReportListWidget({@required this.reportList, this.emailAddress});

  ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return reportList.length > 0
        ? ListView.separated(
            physics: BouncingScrollPhysics(),
            padding: EdgeInsets.only(bottom: 14, top: 14, left: 21, right: 21),
            separatorBuilder: (context, index) => SizedBox(height: 14),
            itemBuilder: (context, index) {
              Reports report = reportList[index];
              return Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(
                      Radius.circular(10.0),
                    ),
                    boxShadow: [
                      BoxShadow(
                        color: Color(0xff000000).withOpacity(.05),
                        //spreadRadius: 5,
                        blurRadius: 27,
                        offset: Offset(0, -3),
                      ),
                    ],
                    color: Colors.white),
                child: Padding(
                  padding: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (report.doctorName != null)
                        Text(
                          report.doctorName,
                          style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.64, height: 25 / 16),
                        ),
                      if (report.doctorName != null) SizedBox(height: 6),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          LargeAvatar(
                            name: report.doctorName,
                            url: report.doctorImageURL,
                            width: 48,
                            height: 48,
                          ),
                          SizedBox(width: 11),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                if (report.projectName != null) MyRichText(TranslationBase.of(context).clinic + ":", report.projectName, projectViewModel.isArabic),
                                if (report.clinicDescription != null) MyRichText(TranslationBase.of(context).hospital + ":", report.clinicDescription, projectViewModel.isArabic),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  mainAxisSize: MainAxisSize.max,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    RatingBar.readOnly(
                                      initialRating: report.actualDoctorRate.toDouble(),
                                      size: 16.0,
                                      filledColor: Color(0XFFD02127),
                                      emptyColor: Color(0XFFD02127),
                                      isHalfAllowed: true,
                                      halfFilledIcon: Icons.star_half,
                                      filledIcon: Icons.star,
                                      emptyIcon: Icons.star_border,
                                    ),
                                    if (reportList[index].status == 2)
                                      IconButton(
                                          icon: Icon(Icons.email),
                                          color: Color(0xff28323A),
                                          constraints: BoxConstraints(),
                                          padding: EdgeInsets.zero,
                                          onPressed: () {
                                            showConfirmMessage(reportList[index]);
                                          })
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              );
            },
            itemCount: reportList.length,
          )
        : getNoDataWidget(context);
  }

  void showConfirmMessage(Reports report) {
    showDialog(
      context: AppGlobal.context,
      builder: (cxt) => ConfirmSendEmailDialog(
        email: emailAddress,
        onTapSendEmail: () {
          sendReportEmail(report);
        },
      ),
    );
  }

  sendReportEmail(Reports report) {
    GifLoaderDialogUtils.showMyDialog(AppGlobal.context);
    ReportsService _reportsService = locator<ReportsService>();
    _reportsService
        .sendEmailForMedicalReport(report.projectName, report.clinicDescription, report.doctorName, DateUtil.convertDateToString(report.requestDate), report.invoiceNo.toString(), report.projectID,
            DateUtil.convertDateToString(report.requestDate), report.setupId)
        .then((value) {
      GifLoaderDialogUtils.hideDialog(AppGlobal.context);
      AppToast.showSuccessToast(message: TranslationBase.of(AppGlobal.context).emailSentSuccessfully);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(AppGlobal.context);
      print(err);
    });
  }
}
