import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/doctor_replay_view_model.dart';
import 'package:doctor_app_flutter/core/model/doctor/list_gt_my_patients_question_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/TextFields.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';

class DoctorReplayChat extends StatefulWidget {
  final ListGtMyPatientsQuestions reply;
  final DoctorReplayViewModel previousModel;
  bool showMsgBox = false;

  DoctorReplayChat({
    Key? key,
    required this.reply,
    required this.previousModel,
  });

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

class _DoctorReplayChatState extends State<DoctorReplayChat> {
  TextEditingController msgController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    if (widget.reply.doctorResponse!.isNotEmpty) {
      msgController.text = widget.reply.doctorResponse!;
    } else {
      widget.showMsgBox = true;
    }
    return BaseView<DoctorReplayViewModel>(
        onModelReady: (model) async {},
        builder: (_, model, w) => AppScaffold(
              baseViewModel: model,
              isShowAppBar: false,
              body: Container(
                height: MediaQuery.of(context).size.height * 1,
                color: Color(0XFFF2F2F2),
                child: Stack(
                  children: [
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.only(
                                left: 0, right: 5, bottom: 5, top: 5),
                            decoration: BoxDecoration(
                              color: Colors.white,
                            ),
                            height: 115,
                            child: Container(
                              padding: EdgeInsets.only(left: 10, right: 10),
                              margin: EdgeInsets.only(top: 40),
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Expanded(
                                        child: RichText(
                                          text: TextSpan(
                                            style: TextStyle(
                                                fontSize: 1.6 *
                                                    SizeConfig.textMultiplier!,
                                                color: Colors.black),
                                            children: <TextSpan>[
                                              new TextSpan(
                                                  text: widget.reply.patientName
                                                      .toString(),
                                                  style: TextStyle(
                                                      color: Color(0xFF2B353E),
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      fontFamily: 'Poppins',
                                                      fontSize: 20)),
                                            ],
                                          ),
                                        ),
                                      ),
                                      InkWell(
                                          onTap: () {
                                            Navigator.pop(context);
                                          },
                                          child: Icon(FontAwesomeIcons.times.data,
                                              size: 30,
                                              color: Color(0xFF2B353E)))
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Expanded(
                            child: Padding(
                              padding: const EdgeInsets.all(16.0),
                              child: SingleChildScrollView(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(
                                      height: 30,
                                    ),
                                    Container(
                                      // color: Color(0xFF2B353E),
                                      width: MediaQuery.of(context).size.width *
                                          0.8,
                                      padding: EdgeInsets.all(10),
                                      decoration: BoxDecoration(
                                        color: Color(0xFF2B353E),
                                        borderRadius: BorderRadius.all(
                                          Radius.circular(10.0),
                                        ),
                                        border: Border.all(
                                            color: HexColor('#707070'),
                                            width: 0.30),
                                      ),
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        children: <Widget>[
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: [
                                                  Container(
                                                    margin:
                                                        EdgeInsets.only(top: 5),
                                                    width: 50,
                                                    height: 50,
                                                    child: Image.asset(
                                                      widget.reply.gender == 1
                                                          ? 'assets/images/male_avatar.png'
                                                          : 'assets/images/female_avatar.png',
                                                      fit: BoxFit.cover,
                                                    ),
                                                  ),
                                                  Divider(),
                                                  SizedBox(
                                                    width: 10,
                                                  ),
                                                  Container(
                                                    width:
                                                        MediaQuery.of(context)
                                                                .size
                                                                .width *
                                                            0.31,
                                                    child: AppText(
                                                      widget.reply.patientName
                                                          .toString(),
                                                      fontSize: SizeConfig
                                                              .getTextMultiplierBasedOnWidth() *
                                                          3,
                                                      fontFamily: 'Poppins',
                                                      color: Colors.white,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      // fontSize: 18
                                                    ),
                                                  ),
                                                  Container(
                                                    margin:
                                                        EdgeInsets.symmetric(
                                                            horizontal: 0),
                                                    child: InkWell(
                                                      onTap: () {
                                                        launchUrlString("tel://" +
                                                            widget.reply
                                                                .mobileNumber!);
                                                      },
                                                      child: Icon(
                                                        Icons.phone,
                                                        color: Colors.white,
                                                      ),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.end,
                                                children: [
                                                  AppText(
                                                    widget.reply.createdOn !=
                                                            null
                                                        ? getFormattedDate(
                                                            widget.reply
                                                                .createdOn)
                                                        : AppDateUtils
                                                            .getDayMonthYearDateFormatted(
                                                                DateTime.now()),
                                                    fontWeight: FontWeight.w500,
                                                    color: Colors.white,
                                                    fontSize: SizeConfig
                                                            .getTextMultiplierBasedOnWidth() *
                                                        2.8,
                                                  ),
                                                  AppText(
                                                    widget.reply.createdOn !=
                                                            null
                                                        ? AppDateUtils.getHour(
                                                            AppDateUtils
                                                                .getDateTimeFromServerFormat(
                                                                    widget.reply
                                                                        .createdOn!))
                                                        : AppDateUtils.getHour(
                                                            DateTime.now()),
                                                    fontSize: SizeConfig
                                                            .getTextMultiplierBasedOnWidth() *
                                                        2.8,
                                                    fontFamily: 'Poppins',
                                                    color: Colors.white,
                                                    // fontSize: 18
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                          SizedBox(
                                            height: 10,
                                          ),
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Column(
                                                children: [
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets.all(
                                                            8.0),
                                                    child: Container(
                                                      width:
                                                          MediaQuery.of(context)
                                                                  .size
                                                                  .width *
                                                              0.7,
                                                      child: AppText(
                                                        widget.reply.remarks!,
                                                        fontSize: 15,
                                                        fontFamily: 'Poppins',
                                                        color: Colors.white,
                                                        // fontSize: 18
                                                      ),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 30,
                                    ),
                                    SizedBox(
                                      height: 30,
                                    ),
                                    if (widget.reply.doctorResponse != null &&
                                        widget.reply.doctorResponse!.isNotEmpty)
                                      Align(
                                        alignment: Alignment.centerRight,
                                        child: Container(
                                          // color: Color(0xFF2B353E),
                                          width: MediaQuery.of(context)
                                                  .size
                                                  .width *
                                              0.8,
                                          padding: EdgeInsets.all(10),
                                          decoration: BoxDecoration(
                                            color: Colors.white,
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(10.0),
                                            ),
                                            border: Border.all(
                                                color: HexColor('#707070'),
                                                width: 0.30),
                                          ),
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.end,
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: <Widget>[
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Row(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .spaceBetween,
                                                    children: [
                                                      Container(
                                                        margin: EdgeInsets.only(
                                                            top: 5),
                                                        width: 50,
                                                        height: 50,
                                                        child: Image.asset(
                                                          widget
                                                                      .previousModel
                                                                        .doctorProfile
                                                                      !.gender ==
                                                                  0
                                                              ? 'assets/images/male_avatar.png'
                                                              : 'assets/images/female_avatar.png',
                                                          fit: BoxFit.cover,
                                                        ),
                                                      ),
                                                      Divider(),
                                                      SizedBox(
                                                        width: 10,
                                                      ),
                                                      Container(
                                                        width: MediaQuery.of(
                                                                    context)
                                                                .size
                                                                .width *
                                                            0.35,
                                                        child: AppText(
                                                          widget
                                                              .previousModel
                                                              .doctorProfile!
                                                              .doctorName!,
                                                          fontSize: SizeConfig
                                                                  .getTextMultiplierBasedOnWidth() *
                                                              3,
                                                          fontFamily: 'Poppins',
                                                          color:
                                                              Color(0xFF2B353E),
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          // fontSize: 18
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                  Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment.end,
                                                    children: [
                                                      AppText(
                                                        widget.reply.responseDate !=
                                                                null
                                                            ? getFormattedDate(
                                                                widget.reply
                                                                    .responseDate)
                                                            : AppDateUtils
                                                                .getDayMonthYearDateFormatted(
                                                                    DateTime
                                                                        .now()),
                                                        fontWeight:
                                                            FontWeight.w500,
                                                        color:
                                                            Color(0xFF2B353E),
                                                        fontSize: SizeConfig
                                                                .getTextMultiplierBasedOnWidth() *
                                                            2.8,
                                                      ),
                                                      AppText(
                                                        widget.reply.responseDate !=
                                                                null
                                                            ? AppDateUtils.getHour(
                                                                AppDateUtils
                                                                    .getDateTimeFromServerFormat(widget
                                                                        .reply
                                                                        .responseDate))
                                                            : AppDateUtils
                                                                .getHour(
                                                                    DateTime
                                                                        .now()),
                                                        fontSize: SizeConfig
                                                                .getTextMultiplierBasedOnWidth() *
                                                            2.8,
                                                        fontFamily: 'Poppins',
                                                        color:
                                                            Color(0xFF2B353E),
                                                        // fontSize: 18
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                              SizedBox(
                                                height: 10,
                                              ),
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Column(
                                                    children: [
                                                      Padding(
                                                        padding:
                                                            const EdgeInsets
                                                                .all(8.0),
                                                        child: Container(
                                                          width: MediaQuery.of(
                                                                      context)
                                                                  .size
                                                                  .width *
                                                              0.7,
                                                          child: AppText(
                                                            widget.reply
                                                                .doctorResponse!,
                                                            fontSize: 15,
                                                            fontFamily:
                                                                'Poppins',
                                                            color: Color(
                                                                0xFF2B353E),
                                                            // fontSize: 18
                                                          ),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                  ],
                                ),
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                    Positioned(
                      bottom: !widget.showMsgBox ? 80 : 0,
                      left: !widget.showMsgBox ? 20 : 0,
                      child: !widget.showMsgBox
                          ? Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                AppText(
                                    "You already replied to this question."),
                                SizedBox(
                                  height: 10,
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    SizedBox(
                                      width: 20,
                                    ),
                                    Container(
                                      width: MediaQuery.of(context).size.width *
                                          0.8,
                                      child: Center(
                                        child: AppButton(
                                          fontWeight: FontWeight.w700,
                                          color: AppGlobal.appGreenColor,
                                          title: "Update Your Answer",
                                          //TranslationBase.of(context).close,
                                          onPressed: () {
                                            setState(() {
                                              widget.showMsgBox = true;
                                            });
                                          },
                                        ),
                                      ),
                                    ),
                                  ],
                                )
                              ],
                            )
                          : Container(
                              width: MediaQuery.of(context).size.width * 1,
                              height: MediaQuery.of(context).size.height * 0.12,
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  FractionallySizedBox(
                                    child: Container(
                                      child: TextFields(
                                          borderRadius: 0,
                                          hintText: TranslationBase.of(context)
                                              .typeHereToReply,
                                          fontSize: 13.5,
                                          suffixIcon:
                                              FontAwesomeIcons.arrowRight.data,
                                          suffixIconColor:
                                              AppGlobal.appGreenColor,
                                          onSuffixTap: () async {
                                            if (msgController.text.isEmpty) {
                                              Utils.showErrorToast(
                                                  "Please Add Doctor Reply");
                                              return;
                                            } else {
                                              GifLoaderDialogUtils.showMyDialog(
                                                  context);
                                              await widget.previousModel
                                                  .createDoctorResponse(
                                                      msgController.text,
                                                      widget.reply);
                                              if (widget.previousModel.state ==
                                                  ViewState.ErrorLocal) {
                                                Utils.showErrorToast(
                                                    widget.previousModel.error);
                                              } else {
                                                DrAppToastMsg.showSuccesToast(
                                                    "Thank you for your replay ");
                                                await widget.previousModel
                                                    .getDoctorReply(
                                                        isLocalBusy: false);
                                                Navigator.pop(context);
                                              }
                                              GifLoaderDialogUtils.hideDialog(
                                                  context);
                                            }
                                          },
                                          // hintColor: Colors.black,
                                          fontWeight: FontWeight.w500,
                                          maxLines: 50,
                                          minLines: 3,
                                          controller: msgController,
                                          validator: (value) {
                                            if (value == null || value == "")
                                              return TranslationBase.of(context)
                                                  .emptyMessage;
                                            else
                                              return null;
                                          }),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                    )
                  ],
                ),
              ),
            ));
  }

  getFormattedDate(date) {
    return (AppDateUtils.getDateTimeFromServerFormat(date).day.toString() +
        " " +
        AppDateUtils.getMonth(
                AppDateUtils.getDateTimeFromServerFormat(date).month)
            .toString()
            .substring(0, 3) +
        ' ' +
        AppDateUtils.getDateTimeFromServerFormat(date).year.toString());
  }
}
