import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription/prescription_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/procedure_View_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.dart';
import 'package:doctor_app_flutter/screens/prescription/add_prescription/add_prescription.dart';
import 'package:doctor_app_flutter/screens/procedures/add_procedure_page.dart';
import 'package:doctor_app_flutter/screens/procedures/favorite_procedure/add_favourite_procedure.dart';
import 'package:doctor_app_flutter/screens/procedures/procedure_type.dart';
import 'package:doctor_app_flutter/screens/procedures/tab_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/material.dart';

class BaseAddProcedureTabPage extends StatefulWidget {
  final ProcedureViewModel? previousProcedureViewModel;
  final PrescriptionViewModel? prescriptionModel;
  final PatiantInformtion? patient;
  final ProcedureType procedureType;

  const BaseAddProcedureTabPage({Key? key, this.previousProcedureViewModel, this.prescriptionModel, this.patient, required this.procedureType}) : super(key: key);

  @override
  _BaseAddProcedureTabPageState createState() => _BaseAddProcedureTabPageState(patient: patient, procedureType: procedureType);
}

class _BaseAddProcedureTabPageState extends State<BaseAddProcedureTabPage> with SingleTickerProviderStateMixin {
  final PatiantInformtion? patient;
  final ProcedureType? procedureType;

  _BaseAddProcedureTabPageState({this.patient, this.procedureType});

  late TabController _tabController;
  int _activeTab = 0;

  @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 BaseView<ProcedureViewModel>(
      onModelReady: (model) async {
        if (widget.previousProcedureViewModel == null) {
          await model.getProcedureTemplate(categoryID: widget.procedureType.getCategoryId());
        }else{
          await model.getProcedureTemplate(categoryID: widget.procedureType.getCategoryId());
        }

      },
      builder: (BuildContext context, ProcedureViewModel model, Widget? child) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: true,
        appBar: BottomSheetTitle(
          title: procedureType!.getToolbarLabel(context),
        ),
        body: NetworkBaseView(
            baseViewModel: model,
            child: Scaffold(
              extendBodyBehindAppBar: true,
              appBar: PreferredSize(
                preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.070),
                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.tabWidget(screenSize, _activeTab == 0, procedureType!.getFavouriteTabName(context), isFirst: true, context: context),
                    TabWidget.tabWidget(screenSize, _activeTab == 1, procedureType!.getAllLabelName(context), isLast: true, context: context),
                  ],
                ),
              ),
              body: Column(
                children: [
                  Expanded(
                    child: TabBarView(
                      physics: BouncingScrollPhysics(),
                      controller: _tabController,
                      children: [
                        AddFavouriteProcedure(
                          previousProcedureViewModel: widget.previousProcedureViewModel ?? model,
                          prescriptionModel: widget.prescriptionModel,
                          patient: patient,
                          procedureType: procedureType,
                        ),
                        if (widget.procedureType == ProcedureType.PRESCRIPTION)
                          AddPrescription(
                            widget.prescriptionModel!,
                            widget.patient!,
                            widget.prescriptionModel!.prescriptionList,
                          )
                        else
                          AddProcedurePage(
                            model: widget.previousProcedureViewModel ?? model,
                            patient: patient!,
                            procedureType: procedureType!,
                          ),
                      ],
                    ),
                  ),
                ],
              ),
            )),
      ),
    );
  }
}
