import 'package:auto_size_text/auto_size_text.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/PatientERVirtualHistoryResponse.dart';
import 'package:diplomaticquarterapp/pages/feedback/feedback_home_page.dart';
import 'package:diplomaticquarterapp/services/livecare_services/livecare_provider.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.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/dialogs/confirm_dialog.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';

class LiveCareHistoryCard extends StatefulWidget {
  ErRequestHistoryList erRequestHistoryList;
  int isFirstOrLast;

  LiveCareHistoryCard({this.erRequestHistoryList, this.isFirstOrLast});

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

class _LiveCareHistoryCardState extends State<LiveCareHistoryCard> {
  AuthenticatedUser authUser = new AuthenticatedUser();
  AppSharedPreferences sharedPref = AppSharedPreferences();

  ProjectViewModel projectViewModel;

  @override
  void initState() {
    getAuthenticatedUser();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return Container(
      margin: EdgeInsets.only(left: 12, right: 12, top: widget.isFirstOrLast == 0 ? 12 : 6, bottom: widget.isFirstOrLast == 1 ? 12 : 6),
      decoration: BoxDecoration(
        color: CustomColors.accentColor,
        borderRadius: BorderRadius.all(
          Radius.circular(10.0),
        ),
        boxShadow: [
          BoxShadow(
            color: Color(0xff000000).withOpacity(.05),
            blurRadius: 27,
            offset: Offset(0, -3),
          ),
        ],
      ),
      child: Container(
        margin: EdgeInsets.only(left: projectViewModel.isArabic ? 0 : 0, right: projectViewModel.isArabic ? 0 : 0),
        padding: EdgeInsets.symmetric(vertical: 10, horizontal: 12),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: Colors.white, width: 1),
          borderRadius: BorderRadius.only(
            bottomRight: projectViewModel.isArabic ? Radius.circular(10) : Radius.circular(10.0),
            topRight: projectViewModel.isArabic ? Radius.circular(10) : Radius.circular(10.0),
            bottomLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(10),
            topLeft: projectViewModel.isArabic ? Radius.circular(10.0) : Radius.circular(10),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              widget.erRequestHistoryList.stringCallStatus ,
              style: TextStyle(
                fontSize: 10.0,
                fontWeight: FontWeight.w600,
                letterSpacing: -0.4,
                color: CustomColors.green,
                //color: widget.erRequestHistoryList.stringCallStatus == "Completed" ? CustomColors.green : CustomColors.orange,
              ),
            ),
            mHeight(12),
            Text(
              TranslationBase.of(context).requestedDate + ": " + DateUtil.getDayMonthYearHourMinuteDateFormatted(DateUtil.convertStringToDate(widget.erRequestHistoryList.arrivalTime)),
              style: TextStyle(
                fontSize: 16.0,
                fontWeight: FontWeight.w600,
                letterSpacing: -0.64,
              ),
            ),
            mHeight(4),
            Row(
              children: <Widget>[
                Text(
                  TranslationBase.of(context).callDuration + ": ",
                  style: TextStyle(fontSize: 10.0, fontWeight: FontWeight.w600, letterSpacing: -0.4, color: CustomColors.textColor),
                ),
                Text(
                  getCallTime(widget.erRequestHistoryList.callDuration),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 12.0,
                    letterSpacing: -0.4,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),mHeight(12),
            Container(
             width: double.infinity,
              child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () {
                        openInvoice();
                      },
                      child: Container(
                        padding: EdgeInsets.only(left: 12, right: 12, top: 6, bottom: 6),
                        decoration: containerRadius(CustomColors.devider, 100),
                        child: Row(
                          children: <Widget>[
                            SvgPicture.asset("assets/images/new/services/invoice_list.svg",width: 18,height: 18,),
                            mWidth(10),
                            Text(
                              TranslationBase.of(context).invoice,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 11.0,
                                letterSpacing: -0.46,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  mWidth(12),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () {
                        openComplaint();
                      },
                      child: Container(
                        padding: EdgeInsets.only(left: 12, right: 12, top: 7, bottom: 7),
                        decoration: containerRadius(CustomColors.devider, 100),
                        child: Row(
                          children: <Widget>[
                            SvgPicture.asset("assets/images/new/services/raise_comp.svg",width: 16,height: 16,),
                            mWidth(10),
                            Expanded(
                              child: AutoSizeText(
                                TranslationBase.of(context).complaint,
                                textAlign: TextAlign.center,
                                maxLines: 1,minFontSize: 8,
                                style: TextStyle(
                                  fontSize: 11.0,
                                  letterSpacing: -0.46,
                                  fontWeight: FontWeight.w600,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  mWidth(12),
                  Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () {
                        rateLiveCareAppo();
                      },
                      child: Container(
                        padding: EdgeInsets.only(left: 12, right: 12, top: 6, bottom: 6),
                        decoration: containerRadius(CustomColors.devider, 100),
                        child: Row(
                          children: <Widget>[
                            Icon( Icons.star,size: 18,),
                            mWidth(10),
                            Text(
                              TranslationBase.of(context).rate,
                              overflow: TextOverflow.clip,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                fontSize: 11.0,
                                letterSpacing: -0.46,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  rateLiveCareAppo() {
    if (widget.erRequestHistoryList.isAppointmentHaveRating) {
      AppToast.showErrorToast(message: TranslationBase.of(context).alreadyRated);
    } else {}
  }

  openInvoice() {
    showDialog(
      context: context,
      builder: (cxt) => ConfirmSendEmailDialog(
        email: projectViewModel.user.emailAddress,
        onTapSendEmail: () {
          sendInvoiceEmail(context);
        },
      ),
    );
  }

  openComplaint() {
    Navigator.push(context, FadePage(page: FeedbackHomePage()));
  }

  sendInvoiceEmail(context) {
    LiveCareService service = new LiveCareService();
    GifLoaderDialogUtils.showMyDialog(context);
    service.sendLiveCareInvoiceEmail(widget.erRequestHistoryList.appointmentNo.toString(), widget.erRequestHistoryList.projectID, authUser.emailAddress, context).then((res) {
      AppToast.showSuccessToast(message: TranslationBase(context).emailSentSuccessfully);
    }).catchError((err) {
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  getAuthenticatedUser() async {
    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      setState(() {
        authUser = data;
      });
    }
  }

  getCallTime(int number) {
    number = number.round();
    var hours = (number / 60 / 60).floor();
    var minutes = (number / 60).floor() - (hours * 60).floor();
    var seconds = number % 60;
    return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }
}
