import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/tab_utils.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:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'all_doctor_questions.dart';
import 'not_replaied_Doctor_Questions.dart';

class DoctorReplyScreen extends StatefulWidget {
  final Function changeCurrentTab;

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

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

class _DoctorReplyScreenState extends State<DoctorReplyScreen>
    with SingleTickerProviderStateMixin {
  TabController? _tabController;
  int _activeTab = 0;
  int pageIndex = 1;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _tabController!.addListener(_handleTabSelection);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController!.dispose();
  }

  _handleTabSelection() {
    setState(() {
      _activeTab = _tabController!.index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return WillPopScope(
      onWillPop: () async {
        widget.changeCurrentTab();
        return false;
      },
      child: AppScaffold(
        appBarTitle: TranslationBase.of(context).replay2,
        isShowAppBar: false,
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Scaffold(
                extendBodyBehindAppBar: false,
                appBar: PreferredSize(
                  preferredSize: Size.fromHeight(
                      MediaQuery.of(context).size.height * 0.070),
                  child: Container(
                    color: TabUtils.getBgTabColor(),
                    child: TabBar(
                      isScrollable: false,
                      controller: _tabController,
                      indicatorColor: Colors.transparent,
                      indicatorWeight: 1.0,
                      indicatorSize: TabBarIndicatorSize.tab,
                      labelColor: Theme.of(context).primaryColor,
                      labelPadding:
                          EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 0),
                      unselectedLabelColor: Colors.grey[800],
                      tabs: [
                        tabWidget(
                            screenSize,
                            _activeTab == 0,

                            /// TODO Elham* Add Tran
                            "Not Replied",
                            isFirst: true,
                            context: context),
                        tabWidget(screenSize, _activeTab == 1,
                            TranslationBase.of(context).all,
                            isLast: true, context: context),
                      ],
                    ),
                  ),
                ),
                body: Column(
                  children: [
                    Expanded(
                      child: TabBarView(
                        physics: BouncingScrollPhysics(),
                        controller: _tabController,
                        children: [
                          NotRepliedDoctorQuestions(),
                          AllDoctorQuestions(),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Widget tabWidget(
  Size screenSize,
  bool isActive,
  String title, {
  int counter = -1,
  bool isFirst = false,
  bool isMiddle = false,
  bool isLast = false,
  context,
}) {
  ProjectViewModel projectViewModel = Provider.of(context);

  return Center(
    child: Container(
      height: TabUtils.getTabHeight(context),
      decoration: TabUtils.getBoxTabsBoxDecoration(
          isActive: isActive,
          isFirst: isFirst,
          isMiddle: isMiddle,
          isLast: isLast,
          projectViewModel: projectViewModel),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TabUtils.getTabText(title: title, isActive: isActive),
          if (counter != -1)
            TabUtils.getTabCounter(isActive: isActive, counter: counter)
        ],
      ),
    ),
  );
}
