import 'package:diplomaticquarterapp/config/shared_pref_kay.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/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_dialog.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:smart_progress_bar/smart_progress_bar.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();

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

  @override
  Widget build(BuildContext 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,
            children: <Widget>[
              Text("Requested date:",
                  style:
                      TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold)),
              Container(
                margin: EdgeInsets.only(top: 10.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(widget.erRequestHistoryList.sArrivalTime,
                        style: TextStyle(fontSize: 14.0)),
                    Text(
                        "Call Duration\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: () {
                          print("Invoice");
                          openInvoice();
                        },
                        child: Container(
                          child: Row(
                            children: <Widget>[
                              Icon(Icons.content_paste, color: Colors.blue),
                              Container(
                                margin: EdgeInsets.only(left: 10.0),
                                child: Text("Invoice",
                                    textAlign: TextAlign.center),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          print("Complaints");
                          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("Complaints",
                                    textAlign: TextAlign.center),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          print("Rate Dr & Appointment");
                        },
                        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: 10.0),
                                child: Text("Rate Dr & Appointment",
                                    overflow: TextOverflow.clip,
                                    textAlign: TextAlign.center),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  openInvoice() {
    ConfirmDialog dialog = new ConfirmDialog(
        context: context,
        confirmMessage: "Send a copy of this invoice to the email: " +
            authUser.emailAddress,
        okText: TranslationBase.of(context).confirm,
        cancelText: TranslationBase.of(context).cancel_nocaps,
        okFunction: () => {sendInvoiceEmail(context)},
        cancelFunction: () => {});
    dialog.showAlertDialog(context);
  }

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

  sendInvoiceEmail(context) {
    ConfirmDialog.closeAlertDialog(context);
    LiveCareService service = new LiveCareService();
    service
        .sendLiveCareInvoiceEmail(
            widget.erRequestHistoryList.appointmentNo.toString(),
            widget.erRequestHistoryList.projectID,
            authUser.emailAddress,
            context)
        .then((res) {
      AppToast.showSuccessToast(message: "LiveCare invoice sent successfully");
    }).catchError((err) {
      AppToast.showErrorToast(message: err);
      print(err);
    }).showProgressBar(
            text: "Loading", backgroundColor: Colors.blue.withOpacity(0.6));
  }

  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')}';
  }
}
