import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/card_with_bg_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/user-guid/CusomRow.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import '../../../util/extenstions.dart';

import 'ShowTimer.dart';

class PatientCard extends StatelessWidget {
  final PatiantInformtion patientInfo;
  final Function onTap;
  final String patientType;
  final String arrivalType;
  final bool isInpatient;
  final bool isMyPatient;
  final bool isFromSearch;
  final bool isFromLiveCare;

  PatientCard(
      {Key key,
      this.patientInfo,
      this.onTap,
      this.patientType,
      this.arrivalType,
      this.isInpatient,
      this.isMyPatient = false,
      this.isFromSearch = false,
      this.isFromLiveCare = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    String nationalityName = patientInfo.nationalityName != null
        ? patientInfo.nationalityName.trim()
        : patientInfo.nationality != null
            ? patientInfo.nationality.trim()
            : patientInfo.nationalityId != null
                ? patientInfo.nationalityId
                : "";

    return Container(
        width: SizeConfig.screenWidth * 0.9,
        margin: EdgeInsets.all(6),
        padding: EdgeInsets.only(
            left: projectViewModel.isArabic ? 5 : 0,
            right: projectViewModel.isArabic ? 0 : 5,
            bottom: 0,
            top: 0),
        decoration: Helpers.getCardBoxDecoration(),
        child: CardWithBgWidget(
          padding: 0,
          marginLeft: (!isMyPatient && isInpatient) ||isFromLiveCare  ? 0 : 10,
          marginSymmetric: isFromSearch ? 10 : 0.0,
          hasBorder: false,
          bgColor: isFromLiveCare
              ? Colors.white
              : (isMyPatient && !isFromSearch)
                  ? AppGlobal.appGreenColor
                  : patientInfo.patientStatusType == 43
                      ? AppGlobal.appGreenColor
                      : isMyPatient
                          ? AppGlobal.appGreenColor
                          : isInpatient
                              ? Colors.white
                              : !isFromSearch
                                  ? Colors.red[800]
                                  : Colors.white,
          widget: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              //  padding: EdgeInsets.only(left: 10, right: 0, bottom: 0),
              child: InkWell(
                child: Column(
                  children: [
                    SizedBox(
                      height: 10,
                    ),
                    !(isInpatient && !isFromSearch)
                        ? Padding(
                            padding: EdgeInsets.only(left: 12.0),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                patientInfo.patientStatusType == 43
                                    ? Row(
                                        children: [
                                          PatientStatus(
                                            label: TranslationBase.of(context)
                                                .arrivedP,
                                            color: AppGlobal.appGreenColor,
                                          ),
                                          SizedBox(
                                            width: 8,
                                          ),
                                          SizedBox(
                                            height: 12,
                                            width: 1.5,
                                            child: Container(
                                              color: Colors.grey,
                                            ),
                                          ),
                                          SizedBox(
                                            width: 8,
                                          ),
                                          PatientStatus(
                                            label: patientInfo.status == 2
                                                ? 'Confirmed'
                                                : 'Booked',
                                            color: patientInfo.status == 2
                                                ? AppGlobal.appGreenColor
                                                : Colors.grey,
                                          ),
                                        ],
                                      )
                                    : patientInfo.patientStatusType == 42
                                        ? Row(
                                            children: [
                                              PatientStatus(
                                                label:
                                                    TranslationBase.of(context)
                                                        .notArrived,
                                                color: Colors.red[800],
                                              ),
                                              SizedBox(
                                                width: 8,
                                              ),
                                              SizedBox(
                                                height: 12,
                                                width: 1.5,
                                                child: Container(
                                                  color: Colors.grey,
                                                ),
                                              ),
                                              SizedBox(
                                                width: 8,
                                              ),
                                              PatientStatus(
                                                label: patientInfo.status == 2
                                                    ? 'Confirmed'
                                                    : 'Booked',
                                                color: patientInfo.status == 2
                                                    ? AppGlobal.appGreenColor
                                                    : Colors.grey,
                                              )
                                            ],
                                          )
                                        : !isFromSearch &&
                                                !isFromLiveCare &&
                                                patientInfo.patientStatusType ==
                                                    null
                                            ? Row(
                                                children: [
                                                  PatientStatus(
                                                    label: TranslationBase.of(
                                                            context)
                                                        .notArrived,
                                                    color: Colors.red[800],
                                                  ),
                                                  SizedBox(
                                                    width: 8,
                                                  ),
                                                  SizedBox(
                                                    height: 12,
                                                    width: 1.5,
                                                    child: Container(
                                                      color: Colors.grey,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    width: 8,
                                                  ),
                                                  PatientStatus(
                                                    label:
                                                        patientInfo.status == 2
                                                            ? 'Booked'
                                                            : 'Confirmed',
                                                    color:
                                                        patientInfo.status == 2
                                                            ? Colors.grey
                                                            : AppGlobal
                                                                .appGreenColor,
                                                  )
                                                ],
                                              )
                                            : SizedBox(),
                                this.arrivalType == '1'
                                    ? AppText(
                                        patientInfo.startTime != null
                                            ? patientInfo.startTime
                                            : patientInfo.startTimes,
                                        fontFamily: 'Poppins',
                                        fontWeight: FontWeight.w400,
                                      )
                                    : patientInfo.arrivedOn != null
                                        ? Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.end,
                                            children: [
                                              AppText(
                                                AppDateUtils
                                                    .getDayMonthYearDateFormatted(
                                                  AppDateUtils
                                                      .convertStringToDate(
                                                    patientInfo.arrivedOn,
                                                  ),
                                                  isMonthShort: true,
                                                ),
                                                fontFamily: 'Poppins',
                                                fontWeight: FontWeight.w400,
                                                fontSize: 15,
                                              ),
                                              AppText(
                                                "${AppDateUtils.getStartTime(patientInfo.startTime)}",
                                                fontFamily: 'Poppins',
                                                fontWeight: FontWeight.w400,
                                                fontSize: 15,
                                              ),
                                            ],
                                          )
                                        : (patientInfo.appointmentDate !=
                                                    null &&
                                                patientInfo
                                                    .appointmentDate.isNotEmpty)
                                            ? Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.end,
                                                children: [
                                                  AppText(
                                                    "${AppDateUtils.getDayMonthYearDateFormatted(AppDateUtils.convertStringToDate(
                                                          patientInfo
                                                              .appointmentDate,
                                                        ), isMonthShort: true)}",
                                                    fontFamily: 'Poppins',
                                                    fontWeight: FontWeight.w400,
                                                    fontSize: 15,
                                                  ),
                                                  AppText(
                                                    " ${AppDateUtils.getStartTime(patientInfo.startTime)}",
                                                    fontFamily: 'Poppins',
                                                    fontWeight: FontWeight.w400,
                                                    fontSize: 15,
                                                  ),
                                                ],
                                              )
                                            : SizedBox()
                              ],
                            ))
                        : SizedBox(),
                    if (isInpatient && isMyPatient && !isFromSearch)
                      Row(
                        children: [
                          SizedBox(
                            width: 12,
                          ),
                          PatientStatus(
                            label: 'My Patient',
                          ),
                        ],
                      ),
                    Padding(
                        padding: EdgeInsets.only(left: 12.0),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              flex: 2,
                              child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    AppText(
                                      isFromLiveCare
                                          ? Helpers.capitalize(
                                              patientInfo.fullName)
                                          : (Helpers.capitalize(
                                                  patientInfo.firstName) +
                                              " " +
                                              Helpers.capitalize(
                                                  patientInfo.lastName)),
                                      fontSize: 16,
                                      color: Color(0xFF2E303A),
                                      fontWeight: FontWeight.w600,
                                      fontFamily: 'Poppins',
                                      letterSpacing: -0.64,
                                      textOverflow: TextOverflow.ellipsis,
                                    ),
                                    if (patientInfo.gender == 1)
                                      Icon(
                                        DoctorApp.male_2,
                                        color: Colors.blue,
                                        size: 18,
                                      )
                                    else
                                      Icon(
                                        DoctorApp.female_1,
                                        color: Colors.pink,
                                        size: 18,
                                      ),
                                    if (isFromLiveCare)
                                      ShowTimer(
                                        patientInfo: patientInfo,
                                      ),
                                  ]),
                            ),
                            if(nationalityName.isNotEmpty)
                            Expanded(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Expanded(
                                    child: Container(
                                      padding: EdgeInsets.only(top: 8),
                                      alignment: Alignment.centerRight,
                                      child: AppText(
                                        nationalityName.truncate(14),
                                        fontWeight: FontWeight.w600,
                                        fontSize: 10,
                                        color: Color(0xFF2E303A),
                                        textOverflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                  ),
                                  patientInfo.nationality != null ||
                                          patientInfo.nationalityId != null
                                      ? ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(20.0),
                                          child: CachedNetworkImage(
                                            imageUrl: patientInfo
                                                        .nationalityFlagURL !=
                                                    null
                                                ? patientInfo.nationalityFlagURL
                                                : '',
                                            height: 16,
                                            width: 22,
                                            errorWidget:
                                                (context, url, error) =>
                                                    AppText(
                                              'No Image',
                                              fontSize: 10,
                                            ),
                                          ))
                                      : SizedBox()
                                ],
                              ),
                            )
                          ],
                        )),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.only(left: 12.0, top: 5),
                            child: Container(
                              width: 60,
                              height: 60,
                              child: Image.asset(
                                patientInfo.gender == 1
                                    ? 'assets/images/male_avatar.png'
                                    : 'assets/images/female_avatar.png',
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Expanded(
                              child: Row(
                            children: [
                              Expanded(
                                child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      // SizedBox(height: 10,),
                                      CustomRow(
                                        label: TranslationBase.of(context)
                                            .fileNumber,
                                        value: patientInfo.patientId.toString(),
                                      ),
                                      CustomRow(
                                        label: TranslationBase.of(context).age +
                                            " : ",
                                        value:
                                            "${AppDateUtils.getAgeByBirthday(patientInfo.dateofBirth, context, isServerFormat: !isFromLiveCare)}",
                                      ),
                                      if (isInpatient)
                                        CustomRow(
                                          label:
                                              patientInfo.admissionDate == null
                                                  ? ""
                                                  : TranslationBase.of(context)
                                                          .admissionDate +
                                                      " : ",
                                          value: patientInfo.admissionDate ==
                                                  null
                                              ? ""
                                              : "${AppDateUtils.getDayMonthYearDateFormatted(AppDateUtils.getDateTimeFromServerFormat(patientInfo.admissionDate.toString()), isMonthShort: true)}",
                                        ),
                                      if (patientInfo.admissionDate != null)
                                        CustomRow(
                                          label: TranslationBase.of(context)
                                                  .numOfDays +
                                              " : ",
                                          value:
                                              "${DateTime.now().difference(AppDateUtils.getDateTimeFromServerFormat(patientInfo.admissionDate)).inDays + 1}",
                                        ),
                                      if (patientInfo.admissionDate != null)
                                        CustomRow(
                                          label: TranslationBase.of(context)
                                                  .clinicName +
                                              " : ",
                                          value:
                                              "${patientInfo.clinicDescription}",
                                        ),
                                      if (patientInfo.admissionDate != null)
                                        CustomRow(
                                          label: TranslationBase.of(context)
                                                  .roomNo +
                                              " : ",
                                          value: "${patientInfo.roomId}",
                                        ),
                                      if (isFromLiveCare)
                                        Column(
                                          children: [
                                            CustomRow(
                                              label: TranslationBase.of(context)
                                                      .clinic +
                                                  " : ",
                                              value: patientInfo.clinicName,
                                            ),
                                          ],
                                        ),
                                    ]),
                              ),
                              Icon(
                                Icons.arrow_forward,
                                size: 24,
                              ),
                            ],
                          ))
                        ]),
                    isFromLiveCare
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Container(
                                  padding: EdgeInsets.all(4),
                                  child: SvgPicture.asset(
                                    'assets/images/svgs/profile_screen/livecare.svg',
                                    height: 20,
                                    width: 20,
                                    // color: Colors.grey.shade700,
                                  )),
                            ],
                          )
                        : !isInpatient && !isFromSearch
                            ? Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                    Container(
                                        padding: EdgeInsets.all(4),
                                        child: SvgPicture.asset(
                                          patientInfo.appointmentType ==
                                                      'Regular' &&
                                                  patientInfo.visitTypeId == 100
                                              ? 'assets/images/svgs/profile_screen/livecare.svg'
                                              : patientInfo.appointmentType ==
                                                      'Walkin'
                                                  ? 'assets/images/svgs/profile_screen/walkin.svg'
                                                  : 'assets/images/svgs/profile_screen/booked.svg',
                                          height: 25,
                                          width: 35,
                                        )),
                                  ])
                            : (isInpatient == true)
                                ? Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                        Container(
                                            padding: EdgeInsets.all(4),
                                            child: Image.asset(
                                              'assets/images/inpatient.png',
                                              height: 25,
                                              width: 35,
                                            )),
                                      ])
                                : SizedBox()
                  ],
                ),
                onTap: onTap,
              )),
        ));
  }
}

class PatientStatus extends StatelessWidget {
  PatientStatus({
    Key key,
    this.label,
    this.color,
  }) : super(key: key);
  final String label;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return AppText(
      label,
      color: color ?? AppGlobal.appGreenColor,
      fontWeight: FontWeight.w600,
      fontFamily: 'Poppins',
      fontSize: 10,
    );
  }
}
