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/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/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:provider/provider.dart';

class LiveCareHistoryCard extends StatefulWidget {
  ErRequestHistoryList erRequestHistoryList;

  LiveCareHistoryCard({this.erRequestHistoryList});

  @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.all(10.0),
      child: Card(
        margin: EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 8.0),
        color: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
        child: Container(
          width: MediaQuery.of(context).size.width,
          // height: MediaQuery.of(context).size.height * 0.22,
          padding: EdgeInsets.all(10.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(TranslationBase.of(context).requestedDate, style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold)),
              Container(
                margin: EdgeInsets.only(top: 0.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(DateUtil.getDayMonthYearHourMinuteDateFormatted(DateUtil.convertStringToDate(widget.erRequestHistoryList.arrivalTime)), style: TextStyle(fontSize: 14.0)),
                    Text(TranslationBase.of(context).callDuration + "\n" + getCallTime(widget.erRequestHistoryList.callDuration), textAlign: TextAlign.center, style: TextStyle(fontSize: 14.0, color: Colors.grey[600])),
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.all(7.0),
                decoration: BoxDecoration(
                  shape: BoxShape.rectangle,
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  color: Colors.green,
                ),
                margin: EdgeInsets.only(top: 5.0, bottom: 5.0),
                child: Text(widget.erRequestHistoryList.stringCallStatus, style: TextStyle(fontSize: 14.0, color: Colors.white)),
              ),
              Divider(
                color: Colors.grey[500],
              ),
              Container(
                margin: EdgeInsets.all(5.0),
                child: Flex(
                  direction: Axis.horizontal,
                  children: <Widget>[
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          openInvoice();
                        },
                        child: Container(
                          child: Row(
                            children: <Widget>[
                              Icon(Icons.content_paste, color: Colors.red),
                              Container(
                                margin: EdgeInsets.only(left: 10.0),
                                child: Text(TranslationBase.of(context).invoice, textAlign: TextAlign.center, style: TextStyle(fontSize: 12.0)),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          openComplaint();
                        },
                        child: Container(
                          child: Row(
                            children: <Widget>[
                              Icon(Icons.add, size: 24.0, color: Colors.red),
                              Container(
                                margin: EdgeInsets.only(left: 10.0),
                                child: Text(TranslationBase.of(context).complaint, textAlign: TextAlign.center, style: TextStyle(fontSize: 12.0)),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          rateLiveCareAppo();
                        },
                        child: Container(
                          child: Row(
                            children: <Widget>[
                              Icon(Icons.star, size: 24.0, color: Colors.yellow[700]),
                              Container(
                                width: MediaQuery.of(context).size.width * 0.2,
                                margin: EdgeInsets.only(left: 9.0),
                                child: Text(TranslationBase.of(context).rateDoctorAppo, overflow: TextOverflow.clip, textAlign: TextAlign.center, style: TextStyle(fontSize: 11.0)),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }


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

    }
  }

  openInvoice() {
    showDialog(
      context: context,
      child: 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')}';
  }
}
