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:smart_hotel_app/routes/app_router.gr.dart';
import 'package:smart_hotel_app/utils/deal_utils.dart';
import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_state.dart';
import 'package:smart_hotel_app/views/home/index/cubit/home_index_cubit.dart';
import 'package:smart_hotel_app/utils/toast_utils.dart';

class VoltageOperateBlock extends StatelessWidget {
  final int alertId;
  final String voltageAlarmTitle;
  final String voltageAlarmLocation;
  final String voltageValue;
  final String alertContent;
  final String voltageWaitTime;
  final String voltageAlarmLevel;
  final String alertCategory;

  const VoltageOperateBlock({
    super.key,
    required this.alertId,
    required this.voltageAlarmTitle,
    required this.voltageAlarmLocation,
    required this.voltageValue,
    required this.alertContent,
    required this.voltageWaitTime,
    required this.voltageAlarmLevel,
    required this.alertCategory,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20.r),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: double.infinity,
            margin: EdgeInsets.only(bottom: 16.h),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: 66.w,
                  height: 54.h,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: const Color.fromRGBO(235, 244, 255, 1.0),
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Icon(DealUtils.deviceIcon(alertCategory), size: 40.w,
                      color: const Color.fromRGBO(73, 149, 234, 1.0)),
                ),
                SizedBox(width: 16.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    spacing: 4.h,
                    children: [
                      Text(
                        voltageAlarmTitle,
                        style: TextStyle(
                          fontSize: 28.sp,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                      Text(
                        voltageAlarmLocation,
                        style: TextStyle(
                          fontSize: 24.sp,
                          color: const Color.fromRGBO(100, 116, 139, 1.0),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(width: 15.w),
                Text(
                  voltageAlarmLevel,
                  style: TextStyle(
                    fontSize: 24.sp,
                    color: voltageAlarmLevel == "置顶" ?
                    const Color.fromRGBO(255, 100, 101, 1.0) :
                    const Color.fromRGBO(255, 204, 21, 1.0),
                  ),
                ),
              ],
            ),
          ),
          Container(
            width: double.infinity,
            margin: EdgeInsets.only(left: 80.w, bottom: 20.h),
            child: Text(
              '$alertContent$voltageWaitTime',
              style: TextStyle(
                fontSize: 24.sp,
                color: const Color.fromRGBO(100, 116, 139, 1.0),
              ),
            ),
          ),
          SizedBox(
            width: double.infinity,
            child: Row(
              spacing: 20.w,
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () => _showHandleDialog(context),
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 20.h),
                      decoration: BoxDecoration(
                        color: const Color.fromRGBO(66, 165, 245, 1.0),
                        borderRadius: BorderRadius.circular(16.r),
                      ),
                      child: Center(
                        child: Text(
                          '确认处理',
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () async {
                      await context.pushRoute(
                          AbnormalDetailRoute(alertId: alertId,
                          alarmStatus: AlarmStatus.pending)
                      ).then((value) {
                        if (value != null && value is bool && value) {
                          context.read<HomeIndexCubit>().loadData();
                        }
                      });
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 20.h),
                      decoration: BoxDecoration(
                        color: const Color.fromRGBO(235, 244, 255, 1.0),
                        borderRadius: BorderRadius.circular(16.r),
                      ),
                      child: Center(
                        child: Text(
                          '查看详情',
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: const Color.fromRGBO(100, 116, 139, 1.0),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void _showHandleDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (dialogContext) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.r),
          ),
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(16.r),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                        onTap: () => Navigator.of(dialogContext).pop(),
                        child: Container(
                          margin: EdgeInsets.only(top: 20.h, right: 20.h),
                          padding: EdgeInsets.symmetric(horizontal: 2.w, vertical: 2.h),
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            border: Border.all(
                              width: 2.w,
                              color: const Color.fromRGBO(100, 116, 139, 1.0),
                            ),
                          ),
                          child: Icon(
                            Icons.close,
                            color: const Color.fromRGBO(100, 116, 139, 1.0),
                            size: 32.sp,
                          ),
                        )
                    ),
                  ],
                ),
                Text(
                  voltageAlarmTitle,
                  style: TextStyle(
                    fontSize: 30.sp,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
                SizedBox(height: 20.h),
                Container(
                  width: double.infinity,
                  margin: EdgeInsets.symmetric(horizontal: 40.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 30.h),
                  decoration: BoxDecoration(
                    color: const Color.fromRGBO(242, 243, 245, 1),
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '解决办法:',
                        style: TextStyle(
                          fontSize: 26.sp,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                      SizedBox(height: 12.h),
                      Text(
                        '1.检查设备是否通风良好;\n2.确认设备负载是否正常;\n3.若异常,请联系电工。',
                        style: TextStyle(
                          fontSize: 26.sp,
                          color: const Color.fromRGBO(100, 116, 139, 1.0),
                          height: 1.5,
                        ),
                      ),
                    ],
                  ),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () async {
                    Navigator.of(dialogContext).pop();
                    try {
                      await context.read<HomeIndexCubit>().handleAlert(alertId);
                      if (context.mounted) {
                        showToast('操作成功');
                      }
                    } catch (e) {
                      if (context.mounted) {
                        showToast('操作失败: $e');
                      }
                    }
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 30.h),
                    child: Text(
                      '确认',
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: const Color.fromRGBO(73, 149, 234, 1.0),
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}