import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/AppointmentDetails.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:rating_bar/rating_bar.dart';

class TodoListCard extends StatefulWidget {
  AppoitmentAllHistoryResultList appo;
  var languageID;
  final VoidCallback onListUpdated;

  TodoListCard({@required this.appo, this.onListUpdated});

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

class _TodoListCardState extends State<TodoListCard> {
  AppSharedPreferences sharedPref = AppSharedPreferences();

  @override
  void initState() {
//    widget.onListUpdated();
    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,
          padding: EdgeInsets.all(10.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Image.asset("assets/images/new-design/time_icon.png",
                      width: 20.0, height: 20.0),
                  Container(
                    margin: EdgeInsets.only(left: 10.0, right: 30.0),
                    child: Text(getDate(widget.appo.appointmentDate),
                        style: TextStyle(fontSize: 12.0)),
                  ),
                  widget.appo.isLiveCareAppointment
                      ? SvgPicture.asset(
                          "assets/images/new-design/liveCare_logo_icon.svg",
                          width: 20.0,
                          height: 20.0)
                      : Image.asset(
                          "assets/images/new-design/hospital_address_icon.png",
                          width: 20.0,
                          height: 20.0),
                  Container(
                    margin: EdgeInsets.only(left: 10.0, right: 10.0),
                    child: widget.appo.isLiveCareAppointment
                        ? Text(TranslationBase.of(context).upcomingLivecare,
                            style: TextStyle(fontSize: 12.0))
                        : Text(widget.appo.projectName,
                            style: TextStyle(fontSize: 12.0)),
                  ),
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 5.0),
                child: Divider(
                  color: Colors.grey[500],
                ),
              ),
              Flex(
                direction: Axis.horizontal,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Container(
                      height: MediaQuery.of(context).size.height * 0.1,
                      margin: EdgeInsets.only(top: 5.0),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(100.0),
                        child: Image.network(widget.appo.doctorImageURL,
                            fit: BoxFit.fill),
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: Container(
                      margin:
                          EdgeInsets.only(top: 20.0, left: 20.0, right: 20.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                              widget.appo.doctorTitle +
                                  " " +
                                  widget.appo.doctorNameObj,
                              style: TextStyle(
                                  fontSize: 14.0,
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold,
                                  letterSpacing: 1.0)),
                          Container(
                            margin: EdgeInsets.only(top: 3.0, bottom: 3.0),
                            child: Text(
                                getDoctorSpeciality(
                                        widget.appo.doctorSpeciality)
                                    .trim(),
                                style: TextStyle(
                                    fontSize: 12.0,
                                    color: Colors.grey[600],
                                    letterSpacing: 1.0)),
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            mainAxisSize: MainAxisSize.max,
                            children: <Widget>[
                              RatingBar.readOnly(
                                initialRating: 4.0,
                                size: 20.0,
                                filledColor: Colors.yellow[700],
                                emptyColor: Colors.grey[500],
                                isHalfAllowed: true,
                                halfFilledIcon: Icons.star_half,
                                filledIcon: Icons.star,
                                emptyIcon: Icons.star,
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      margin: EdgeInsets.only(top: 20.0),
                      child: Column(
                        children: <Widget>[
                          Image.asset(
                              getNextActionImage(widget.appo.nextAction),
                              width: 50.0,
                              height: 50.0),
                          Container(
                            margin: EdgeInsets.only(top: 5.0),
                            child: Text(
                                getNextActionText(widget.appo.nextAction),
                                textAlign: TextAlign.center,
                                style: TextStyle(fontSize: 12.0)),
                          )
                        ],
                      ),
                    ),
                  )
                ],
              ),
              Divider(
                color: Colors.grey[500],
              ),
              Flex(
                direction: Axis.horizontal,
                children: <Widget>[
                  Expanded(
                    flex: 2,
                    child: Container(
                      child: Text(
                          getNextActionDescription(widget.appo.nextAction),
                          style: TextStyle(
                              fontSize: 12.0, color: Colors.grey[700])),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        navigateToAppointmentDetails(context);
                      },
                      child: Container(
                        child: Text(TranslationBase.of(context).upcomingDetails,
                            textAlign: TextAlign.end,
                            style: TextStyle(
                                fontSize: 12.0,
                                color: new Color(0xFF40ACC9),
                                decoration: TextDecoration.underline)),
                      ),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  String getNextActionImage(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return "assets/images/new-design/confirm_button.png";
        break;

      case 15:
        return widget.languageID == 'ar'
            ? "assets/images/new-design/pay_online_button_arabic_disabled.png"
            : "assets/images/new-design/pay_online_button_disabled.png";
        break;

      case 20:
        return widget.languageID == 'ar'
            ? "assets/images/new-design/pay_online_button_arabic.png"
            : "assets/images/new-design/pay_online_button.png";
        break;

      case 30:
        return "assets/images/new-design/qr_code_button.png";
        break;

      case 40:
        return "assets/images/new-design/video_call_instruction.png";
        break;

      case 50:
        return "assets/images/new-design/liveCare_logo_icon.png";
        break;

      default:
        return "";
    }
  }

  String getNextActionText(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return TranslationBase.of(context).confirm;
        break;

      case 15:
        return TranslationBase.of(context).pendingPayment;
        break;

      case 20:
        return TranslationBase.of(context).payNow;
        break;

      case 30:
        return TranslationBase.of(context).viewQR;
        break;

      case 40:
        return TranslationBase.of(context).instruction;
        break;

      case 50:
        return TranslationBase.of(context).livecare;
        break;

      default:
        return "";
    }
  }

  String getNextActionDescription(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return TranslationBase.of(context).upcomingConfirm;
        break;

      case 15:
        return TranslationBase.of(context).upcomingPaymentPending;
        break;

      case 20:
        return TranslationBase.of(context).upcomingPaymentNow;
        break;

      case 30:
        return TranslationBase.of(context).upcomingQR;
        break;

      case 40:
        return TranslationBase.of(context).upcomingVirtual;
        break;

      case 50:
        return TranslationBase.of(context).upcomingLivecare;
        break;

      default:
        return "";
    }
  }

  getLanguageID() async {
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    setState(() {
      widget.languageID = languageID;
    });
  }

  String getDate(String date) {
    DateTime dateObj = DateUtil.convertStringToDate(date);
    return DateUtil.getWeekDay(dateObj.weekday) +
        ", " +
        dateObj.day.toString() +
        " " +
        DateUtil.getMonth(dateObj.month) +
        " " +
        dateObj.year.toString() +
        " " +
        dateObj.hour.toString() +
        ":" +
        getMinute(dateObj);
  }

  String getMinute(DateTime dateObj) {
    if (dateObj.minute == 0) {
      return dateObj.minute.toString() + "0";
    } else {
      return dateObj.minute.toString();
    }
  }

  String getDoctorSpeciality(List<String> docSpecial) {
    String docSpeciality = "";
    docSpecial.forEach((v) {
      docSpeciality = docSpeciality + v + "\n";
    });
    return docSpeciality;
  }

  Future navigateToAppointmentDetails(context) async {
    Navigator.push(
        context, FadePage(page: AppointmentDetails()));
  }
}
