import 'package:hmg_patient_app/core/model/ImagesInfo.dart';
import 'package:hmg_patient_app/core/viewModels/er/rrt-view-model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/rrt/service_price.dart';
import 'package:hmg_patient_app/pages/ErService/rapid-response-team/rrt-logs-page.dart';
import 'package:hmg_patient_app/pages/ErService/rapid-response-team/rrt-request-page.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class RRTMainScreen extends StatefulWidget {
  final index = 0;

  RRTMainScreen({index});

  @override
  State<StatefulWidget> createState() => RRTMainScreenState();
}

class RRTMainScreenState extends State<RRTMainScreen> with SingleTickerProviderStateMixin {
  int currentIndex = 0;
  late TabController tabController;
  PageController pageController = PageController(initialPage: 0, keepPage: true);

  late RRTViewModel viewModel;
  late bool loadingData;

  List<ImagesInfo> imagesInfo =[];

  late ProjectViewModel projectViewModel;

  @override
  void initState() {
    super.initState();
    imagesInfo.add(ImagesInfo(imageEn: 'https://hmgwebservices.com/Images/MobileApp/RRT/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/RRT/ar/0.png'));
    tabController = TabController(length: 2, vsync: this);
  }

  late TranslationBase localize;

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    localize = TranslationBase.of(context);

    return AppScaffold(
        appBarTitle: localize.rapidResponseTeam,
        isShowAppBar: true,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        description: TranslationBase.of(context).rrtDDetails,
        imagesInfo: imagesInfo,
        body: BaseView<RRTViewModel>(
          onModelReady: (vm) async {
            viewModel = vm;
            loadingData = true;
            await vm.loadRequiredData(projectViewModel.isArabic ? 1 : 2).then((value) {}).whenComplete(() => setState(() => loadingData = false));
          },
          builder: (ctx, vm, widget) => content(),
        ));
  }

  Widget content() {
    if (loadingData == true) {
      return Center(child: CircularProgressIndicator());
      // else if(viewModel.state == ViewState.Error)

    } else if (viewModel.rrtServiceData != null && viewModel.rrtProcedureList != null) {
      return Column(
        children: [tabBar(), Expanded(child: contentPager())],
      );
    } else {
      return Container(
        alignment: Alignment.center,
        child: Text(
          localize.somethingWentWrongTryLater,
          style: TextStyle(color: Colors.red),
          maxLines: 5,
        ),
      );
    }
  }

  Widget tabBar() => Container(
        margin: EdgeInsets.all(0.0),
        clipBehavior: Clip.hardEdge,
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10)),
        child: TabBar(
            onTap: onPageChanged,
            controller: tabController,
            indicatorWeight: 3.0,
            indicatorSize: TabBarIndicatorSize.tab,
            labelColor: Color(0xff2B353E),
            unselectedLabelColor: Color(0xff575757),
            labelPadding: EdgeInsets.only(top: 5, bottom: 5),
            labelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 15,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            unselectedLabelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 15,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            tabs: [
              Tab(
                child: Text(
                  localize.rapidResponseTeam,
                  style: TextStyle(color: Theme.of(context).appBarTheme.backgroundColor),
                ),
              ),
              Tab(
                child: Text(
                  localize.orderLog,
                  style: TextStyle(color: Theme.of(context).appBarTheme.backgroundColor!),
                ),
              ),
            ]),
      );

  Widget contentPager() => PageView(
        onPageChanged: onPageChanged,
        controller: pageController,
        children: [
          RRTRequestPage(servicePrice: viewModel.rrtServiceData.servicePrice, pendingOrders: viewModel.rrtServiceData.pendingOrders, viewModel: viewModel),
          RRTLogPage(orders: viewModel.rrtServiceData.completedOrders),
        ],
      );

  void onPageChanged(int index) {
    pageController.animateToPage(index, duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
    tabController.animateTo(index);
  }
}
