import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:smart_hotel_app/blocs/auth/auth_bloc.dart';
import 'package:smart_hotel_app/blocs/auth/auth_state.dart';
import 'package:smart_hotel_app/repositories/inspection_device_repository.dart';
import 'package:smart_hotel_app/routes/app_router.gr.dart';
import 'package:smart_hotel_app/services/inspection_device_service.dart';
import 'package:smart_hotel_app/utils/toast_utils.dart';

class StartInspectionButton extends StatelessWidget {
  final String deviceId;

  const StartInspectionButton({super.key, required this.deviceId});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, authState) {
        return GestureDetector(
          onTap: () async {
            final inspectorId =
                authState is AuthSuccess ? authState.userInfo.userId : 0;
            if (inspectorId == 0) return;

            if (context.mounted) {
              showDialog(
                context: context,
                barrierDismissible: false,
                builder: (context) => Center(
                  child: Container(
                    width: 140.w,
                    height: 140.h,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20.r),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        LoadingAnimationWidget.waveDots(
                          color: const Color.fromRGBO(66, 165, 245, 1.0),
                          size: 50,
                        ),
                        SizedBox(height: 12.h),
                        Text(
                          '巡检中...',
                          style: TextStyle(
                            color: const Color.fromRGBO(100, 116, 139, 1.0),
                            fontSize: 24.sp,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            }

            final service = InspectionDeviceService(
              repository: InspectionDeviceRepository(),
            );
            try {
              await service.startInspection(
                deviceId: int.tryParse(deviceId) ?? 0,
                inspectorId: inspectorId,
              );
              if (context.mounted) {
                context.popRoute();
                context.pushRoute(
                  InspectionHistoryRoute(deviceId: deviceId),
                );
              }
            } catch (e) {
              if (context.mounted) {
                context.popRoute();
                showToast('巡检失败: $e');
              }
            }
          },
          child: Container(
            width: double.infinity,
            height: 88.h,
            decoration: BoxDecoration(
              color: const Color.fromRGBO(66, 165, 245, 1.0),
              borderRadius: BorderRadius.circular(24.r),
            ),
            child: Center(
              child: Text(
                '开始巡检',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 32.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}