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/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/doctor/doctor_replay/doctor_reply_widget.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/dr_app_circular_progress_Indeicator.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:flutter/material.dart';

import 'doctor_repaly_chat.dart';

class AllDoctorQuestions extends StatefulWidget {
  final Function? changeCurrentTab;

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

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

class _AllDoctorQuestionsState extends State<AllDoctorQuestions> {
  int pageIndex = 1;

  @override
  Widget build(BuildContext context) {
    return BaseView<DoctorReplayViewModel>(
      onModelReady: (model) {
        model.getDoctorReply(isLocalBusy: false);
      },
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).replay2,
        isShowAppBar: false,
        body: model.listDoctorWorkingHoursTable.isEmpty
            ? ErrorMessage(
                error: TranslationBase.of(context)
                    .noItem) // 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 false;
                        },
                      ),
                    ),
                  ),
                ],
              ),
      ),
    );
  }
}
