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/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/widgets/avatar/large_avatar.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemBuilder: (context, index) => Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            width: double.infinity,
            margin: EdgeInsets.only(left: 8, right: 8, top: 3),
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(color: Colors.white, width: 2),
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                )),
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: LargeAvatar(
                      name: reportList[index].doctorName,
                      url: reportList[index].doctorImageURL,
                    ),
                  ),
                ),
                Expanded(
                  flex: 4,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            SizedBox(
                              height: 12,
                            ),
                            Texts(reportList[index].projectName),
                            Container(
                              //height: MediaQuery.of(context).size.height * 0.07,
                              width: MediaQuery.of(context).size.width * 0.35,
                              child: Texts(reportList[index].clinicDescription),
                            ),
                            Texts(TranslationBase.of(context).invoiceNo + ': ${reportList[index].invoiceNo}'),
                            SizedBox(height: 12),
                          ],
                        ),
                        reportList[index].status == 2
                            ? Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left: 15.0, right: 15.0),
                                  child: InkWell(
                                    onTap: () {
                                      showConfirmMessage(reportList[index]);
                                      // sendReportEmail(reportList[index]);
                                    },
                                    child: Icon(
                                      Icons.email,
                                      color: Theme.of(context).primaryColor,
                                      size: 30.0,
                                    ),
                                  ),
                                ),
                              )
                            : Container(),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
        itemCount: reportList.length,
      ),
    );
  }

  void showConfirmMessage(Reports report) {
    showDialog(
      context: AppGlobal.context,
      child: 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);
    });
  }
}
