import 'package:cached_network_image/cached_network_image.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/extenstions_utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.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/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.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,
      required this.onTap,
      this.patientType = '',
      this.arrivalType = '',
      this.isInpatient = false,
      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: Utils.getCardBoxDecoration(),
        child: CardWithBgWidget(
          padding: 0,
          marginLeft: (!isMyPatient && isInpatient) || isFromLiveCare ? 0 : 0,
          marginSymmetric: isFromSearch ? 0 : 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,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              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
                                        ? Container(
                                            padding: EdgeInsets.only(right: 9),
                                            child: AppText(
                                              "${AppDateUtils.getStartTime(patientInfo!.startTime!)}",
                                              fontFamily: 'Poppins',
                                              fontWeight: FontWeight.w600,
                                              fontSize: 11,
                                              letterSpacing: -0.64,
                                            ),
                                          )
                                        : (patientInfo!.appointmentDate != null && patientInfo!.appointmentDate!.isNotEmpty)
                                            ? Container(
                                                padding: EdgeInsets.only(right: 9),
                                                child: AppText(
                                                  " ${AppDateUtils.getStartTime(patientInfo!.startTime!)}",
                                                  fontFamily: 'Poppins',
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 11,
                                                  letterSpacing: -0.64,
                                                ),
                                              )
                                            : 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 ? Utils.capitalize(patientInfo!.fullName) : (Utils.capitalize(patientInfo!.firstName) + " " + Utils.capitalize(patientInfo!.lastName)),
                                  fontSize: 16,
                                  color: Color(0xFF2E303A),
                                  fontWeight: FontWeight.w600,
                                  fontFamily: 'Poppins',
                                  letterSpacing: -0.64,
                                  textOverflow: TextOverflow.ellipsis,
                                ),
                                if (patientInfo!.gender == 1)
                                  Container(
                                    padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
                                    child: SvgPicture.asset("assets/images/svgs/male.svg"),
                                  )
                                else
                                  Container(
                                    padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
                                    child: SvgPicture.asset("assets/images/svgs/female.svg"),
                                  ),
                                if (isFromLiveCare)
                                  ShowTimer(
                                    patientInfo: patientInfo!,
                                  ),
                              ]),
                            ),
                            if (nationalityName.isNotEmpty)
                              Expanded(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Container(
                                        padding: EdgeInsets.only(top: 5),
                                        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
                                        ? Container(
                                            padding: EdgeInsets.only(right: 7, top: 5),
                                            child: 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, bottom: 10),
                        child: Container(
                          width: 60,
                          height: 60,
                          //TODO Elham* create widget for this to make it use every where
                          child: SvgPicture.asset(
                            patientInfo!.gender == 1 ? 'assets/images/svgs/male avatar.svg' : 'assets/images/svgs/female avatar.svg',
                            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)}",
                              ),

                              patientInfo!.arrivedOn != null
                                  ? Column(
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: [
                                        // AppText(
                                        //   AppDateUtils
                                        //       .getDayMonthYearDateFormatted(
                                        //     AppDateUtils
                                        //         .convertStringToDate(
                                        //       patientInfo!.arrivedOn,
                                        //     ),
                                        //     isMonthShort: true,
                                        //   ),
                                        //   fontFamily: 'Poppins',
                                        //   fontWeight: FontWeight.w600,
                                        //   fontSize: 11,
                                        //   letterSpacing: -0.64,
                                        // ),

                                        CustomRow(
                                          label: TranslationBase.of(context).arrivedP + " : ",
                                          value: AppDateUtils.getDayMonthYearDateFormatted(
                                            AppDateUtils.convertStringToDate(
                                              patientInfo!.arrivedOn!,
                                            ),
                                            isMonthShort: true,
                                          ),
                                        ),
                                      ],
                                    )
                                  : (patientInfo!.appointmentDate != null && patientInfo!.appointmentDate!.isNotEmpty)
                                      ? Column(
                                          crossAxisAlignment: CrossAxisAlignment.end,
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          children: [
                                            CustomRow(
                                              label: TranslationBase.of(context).appointmentDate + " : ",
                                              value: "${AppDateUtils.getDayMonthYearDateFormatted(AppDateUtils.convertStringToDate(
                                                    patientInfo!.appointmentDate!,
                                                  ), isMonthShort: true)}",
                                            ),
                                          ],
                                        )
                                      : SizedBox(),

                              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 ?? "",
                                    ),
                                  ],
                                ),
                            ]),
                          ),
                          Padding(
                            padding: const EdgeInsets.only(right: 8),
                            child: Icon(
                              Icons.arrow_forward,
                              size: 24,
                            ),
                          ),
                        ],
                      ))
                    ]),
                    isFromLiveCare
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Container(
                                  padding: EdgeInsets.only(left: 9, right: 9, bottom: 9),
                                  child: SvgPicture.asset(
                                    'assets/images/svgs/profile_screen/livecare.svg',
                                    height: 20,
                                    width: 20,
                                    // color: Colors.grey.shade700,
                                  )),
                            ],
                          )
                        : !isInpatient && !isFromSearch
                            ? Row(mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.start, children: [
                                Container(
                                    padding: EdgeInsets.only(left: 9, right: 9, bottom: 9),
                                    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, crossAxisAlignment: CrossAxisAlignment.start, children: [
                                    Container(
                                        padding: EdgeInsets.only(left: 9, right: 9, bottom: 9),
                                        child: SvgPicture.asset(
                                          'assets/images/svgs/inpatient.svg',
                                          height: 25,
                                          width: 35,
                                        )),
                                  ])
                                : SizedBox()
                  ],
                ),
                onTap: onTap,
              )),
        ));
  }
}

class PatientStatus extends StatelessWidget {
  PatientStatus({
    Key? key,
    required 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,
    );
  }
}
