import 'dart:async';
import 'dart:developer';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/todo_section/models/resp_models/ancillary_order_list_response_model.dart';
import 'package:hmg_patient_app_new/features/todo_section/todo_section_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/todo_section/ancillary_procedures_details_page.dart';
import 'package:hmg_patient_app_new/presentation/todo_section/widgets/ancillary_orders_list.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

class ToDoPage extends StatefulWidget {
  const ToDoPage({super.key});

  @override
  State<ToDoPage> createState() => _ToDoPageState();
}

class _ToDoPageState extends State<ToDoPage> {

  late AppState appState;

  @override
  void initState() {
    final TodoSectionViewModel todoSectionViewModel = context.read<TodoSectionViewModel>();
    scheduleMicrotask(() async {
      if (appState.isAuthenticated) {
        await todoSectionViewModel.initializeTodoSectionViewModel();
      }
    });
    super.initState();
  }

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

  Widget _buildLoadingShimmer() {
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: 3,
      padding: EdgeInsetsGeometry.zero,
      itemBuilder: (context, index) {
        return AncillaryOrderCard(
          order: AncillaryOrderItem(),
          isLoading: true,
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    return CollapsingListView(
      title: LocaleKeys.ancillaryOrdersList.tr(context: context),
      isLeading: true,
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 16.h),
            Consumer<TodoSectionViewModel>(
              builder: (BuildContext context, TodoSectionViewModel todoSectionViewModel, Widget? child) {
                return todoSectionViewModel.isAncillaryOrdersLoading
                    ? _buildLoadingShimmer()
                    : AncillaryOrdersList(
                        orders: todoSectionViewModel.patientAncillaryOrdersList,
                        onCheckIn: (order) => log("Check-in for order: ${order.orderNo}"),
                        onViewDetails: (order) async {
                          Navigator.of(context).push(CustomPageRoute(
                              page: AncillaryOrderDetailsList(
                            appointmentNoVida: order.appointmentNo ?? 0,
                            orderNo: order.orderNo ?? 0,
                            projectID: order.projectID ?? 0,
                            projectName: order.projectName ?? "",
                                setupID: order.setupID ?? "0",
                              ),
                            ),
                          );
                        },
                      );
              },
            ),
          ],
        ).paddingSymmetrical(24.w, 0),
      ),
    );
  }
}
