import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/viewModel/doctor_replay_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/doctor/doctor_repaly_chat.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/doctor/doctor_reply_widget.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/dr_app_circular_progress_Indeicator.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/dr_app_embedded_error.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/*
 *@author: Mohammad Aljammal
 *@Date:28/4/2020
 *@param:
 *@return:
 *@desc: Doctor Reply Screen display data from GtMyPatientsQuestions service
 */
class DoctorReplyScreen extends StatefulWidget {
  final Function changeCurrentTab;

  const DoctorReplyScreen({Key key, this.changeCurrentTab}) : super(key: key);

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

class _DoctorReplyScreenState extends State<DoctorReplyScreen> {
  int pageIndex = 1;

  @override
  Widget build(BuildContext context) {
    return BaseView<DoctorReplayViewModel>(
      onModelReady: (model) {
        model.getDoctorReply(isLocalBusy: false);
      },
      builder: (_, model, w) => WillPopScope(
        onWillPop: () async {
          widget.changeCurrentTab();
          return false;
        },
        child: AppScaffold(
          baseViewModel: model,
          appBarTitle: TranslationBase.of(context).replay2,
          isShowAppBar: false,
          body: model.listDoctorWorkingHoursTable.isEmpty
              ? DrAppEmbeddedError(error: TranslationBase.of(context).noItem)
              : Column(
                children: [
                  Expanded(
                    child: Container(
                        padding: EdgeInsetsDirectional.fromSTEB(30, 0, 30, 0),
                        child: NotificationListener(
                          child: ListView.builder(
                              scrollDirection: Axis.vertical,
                              itemCount: model.listDoctorWorkingHoursTable.length,
                              shrinkWrap: true,
                              itemBuilder: (BuildContext ctxt, int index) {
                                return Column(
                                  children: [
                                    InkWell(
                                      onTap: () {
                                        Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                              builder: (BuildContext context) =>
                                                  DoctorReplayChat(
                                                reply:
                                                    model.listDoctorWorkingHoursTable[
                                                        index],
                                                previousModel: model,
                                              ),
                                              settings: RouteSettings(
                                                  name: 'DoctorReplayChat'),
                                            ));
                                      },
                                      child: DoctorReplyWidget(
                                          reply: model
                                              .listDoctorWorkingHoursTable[index]),
                                    ),
                                    if(model.state == ViewState.BusyLocal && index == model.listDoctorWorkingHoursTable.length-1)
                                      DrAppCircularProgressIndeicator()

                                  ],
                                );
                              }),
                          onNotification: (t)  {
                            if (t is ScrollUpdateNotification && t.metrics.pixels >= t.metrics.maxScrollExtent - 50 &&
                                model.state != ViewState.BusyLocal) {
                              setState(() {
                                pageIndex++;
                              });
                               model.getDoctorReply(pageIndex: pageIndex);
                            }
                            return;
                          },
                        ),
                      ),
                  ),
                ],
              ),
        ),
      ),
    );
  }
}
