import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/utils/date_util.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/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/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/features/lab/lab_view_model.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/shimmer/movies_shimmer_widget.dart';
import 'package:provider/provider.dart';

import '../../features/radiology/radiology_view_model.dart';

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

  @override
  State<RadiologyOrdersPage> createState() => _RadiologyOrdersPageState();
}

class _RadiologyOrdersPageState extends State<RadiologyOrdersPage> {
  late RadiologyViewModel radiologyViewModel;

  int? expandedIndex;

  @override
  void initState() {
    scheduleMicrotask(() {
      radiologyViewModel.initRadiologyProvider();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    radiologyViewModel = Provider.of<RadiologyViewModel>(context);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      appBar: AppBar(
        title: LocaleKeys.radiology.tr(context: context).toText18(),
        backgroundColor: AppColors.bgScaffoldColor,
      ),
      body: Padding(
        padding: EdgeInsets.all(24.h),
        child: SingleChildScrollView(
          child: Consumer<RadiologyViewModel>(
            builder: (context, model, child) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.radiology.tr(context: context).toText24(isBold: true),
                      Utils.buildSvgWithAssets(icon: AppAssets.search_icon),
                    ],
                  ),
                  SizedBox(height: 16.h),
                  // Build Tab Bar
                  SizedBox(height: 16.h),
                  // Expandable list
                  ListView.builder(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: model.isRadiologyOrdersLoading ? 5 : model.patientRadiologyOrders.length,
                    itemBuilder: (context, index) {
                      final isExpanded = expandedIndex == index;
                      return model.isRadiologyOrdersLoading
                          ? const MoviesShimmerWidget()
                          : AnimationConfiguration.staggeredList(
                              position: index,
                              duration: const Duration(milliseconds: 500),
                              child: SlideAnimation(
                                verticalOffset: 100.0,
                                child: FadeInAnimation(
                                  child: AnimatedContainer(
                                    duration: Duration(milliseconds: 300),
                                    curve: Curves.easeInOut,
                                    margin: EdgeInsets.symmetric(vertical: 8.h),
                                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 20.h, hasShadow: true),
                                    child: InkWell(
                                      onTap: () {
                                        setState(() {
                                          expandedIndex = isExpanded ? null : index;
                                        });
                                      },
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Padding(
                                            padding: EdgeInsets.all(16.h),
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                Row(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: [
                                                    CustomButton(
                                                      text: LocaleKeys.resultsAvailable.tr(context: context),
                                                      onPressed: () {},
                                                      backgroundColor: AppColors.successColor.withOpacity(0.15),
                                                      borderColor: AppColors.successColor.withOpacity(0.01),
                                                      textColor: AppColors.successColor,
                                                      fontSize: 10,
                                                      fontWeight: FontWeight.w500,
                                                      borderRadius: 8,
                                                      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                                      height: 30.h,
                                                    ),
                                                    Icon(isExpanded ? Icons.expand_less : Icons.expand_more),
                                                  ],
                                                ),
                                                SizedBox(height: 8.h),
                                                Row(
                                                  children: [
                                                    Image.network(
                                                      model.patientRadiologyOrders[index].doctorImageURL!,
                                                      width: 24.h,
                                                      height: 24.h,
                                                      fit: BoxFit.fill,
                                                    ).circle(100),
                                                    SizedBox(width: 4.h),
                                                    model.patientRadiologyOrders[index].doctorName!.toText16(isBold: true)
                                                  ],
                                                ),
                                                SizedBox(height: 8.h),
                                                Row(
                                                  children: [
                                                    CustomButton(
                                                      text: DateUtil.formatDateToDate(DateUtil.convertStringToDate(model.patientRadiologyOrders[index].orderDate), false),
                                                      onPressed: () {},
                                                      backgroundColor: AppColors.greyColor,
                                                      borderColor: AppColors.greyColor,
                                                      textColor: AppColors.blackColor,
                                                      fontSize: 12,
                                                      fontWeight: FontWeight.w500,
                                                      borderRadius: 8,
                                                      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                                      height: 24.h,
                                                    ),
                                                    SizedBox(width: 8.h),
                                                    CustomButton(
                                                      text: model.patientRadiologyOrders[index].clinicDescription!,
                                                      onPressed: () {},
                                                      backgroundColor: AppColors.greyColor,
                                                      borderColor: AppColors.greyColor,
                                                      textColor: AppColors.blackColor,
                                                      fontSize: 12,
                                                      fontWeight: FontWeight.w500,
                                                      borderRadius: 8,
                                                      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                                      height: 24.h,
                                                    ),
                                                  ],
                                                ),
                                              ],
                                            ),
                                          ),
                                          AnimatedCrossFade(
                                            firstChild: SizedBox.shrink(),
                                            secondChild: Padding(
                                              padding: EdgeInsets.symmetric(horizontal: 16.h, vertical: 8.h),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                children: [
                                                  Padding(
                                                    padding: EdgeInsets.only(bottom: 8.h),
                                                    child: '● ${model.patientRadiologyOrders[index].description}'.toText14(weight: FontWeight.w500),
                                                  ),
                                                  Row(
                                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                    children: [
                                                      SizedBox(),
                                                      CustomButton(
                                                        icon: AppAssets.view_report_icon,
                                                        iconColor: AppColors.primaryRedColor,
                                                        iconSize: 16.h,
                                                        text: LocaleKeys.viewReport.tr(context: context),
                                                        onPressed: () {},
                                                        backgroundColor: AppColors.secondaryLightRedColor,
                                                        borderColor: AppColors.secondaryLightRedColor,
                                                        textColor: AppColors.primaryRedColor,
                                                        fontSize: 14,
                                                        fontWeight: FontWeight.bold,
                                                        borderRadius: 12,
                                                        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                                        height: 40.h,
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ),
                                            crossFadeState: isExpanded ? CrossFadeState.showSecond : CrossFadeState.showFirst,
                                            duration: Duration(milliseconds: 300),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            );
                    },
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  Color getLabOrderStatusColor(num status) {
    switch (status) {
      case 44:
        return AppColors.warningColorYellow;
      case 45:
        return AppColors.warningColorYellow;
      case 16:
        return AppColors.successColor;
      case 17:
        return AppColors.successColor;
      default:
        return AppColors.greyColor;
    }
  }

  String getLabOrderStatusText(num status) {
    switch (status) {
      case 44:
        return LocaleKeys.resultsPending.tr(context: context);
      case 45:
        return LocaleKeys.resultsPending.tr(context: context);
      case 16:
        return LocaleKeys.resultsAvailable.tr(context: context);
      case 17:
        return LocaleKeys.resultsAvailable.tr(context: context);
      default:
        return "";
    }
  }
}
