import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laki_icu_app/models/bo/monitoring_bo.dart';
import 'package:laki_icu_app/views/monitoring/index/cubit/monitoring_index_cubit.dart';

class MonitoringAlertDetailDialog extends StatelessWidget {
  final AlertInfoBO alert;

  const MonitoringAlertDetailDialog({
    super.key,
    required this.alert,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      insetPadding: EdgeInsets.zero,
      child: _buildDialogContent(context),
    );
  }

  Widget _buildDialogContent(BuildContext context) {
    return Container(
      width: 1300.w,
      height: 800.h,
      margin: EdgeInsets.fromLTRB(100.w, 100.h, 100.w, 100.h),
      decoration: BoxDecoration(
        image: const DecorationImage(
          image: AssetImage('lib/assets/monitoring/dialog_alert_bg.png'),
          fit: BoxFit.fill,
        ),
        borderRadius: BorderRadius.circular(20.r),
      ),
      child: Stack(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildHeader(),
              SizedBox(height: 30.h),
              _buildContent(context),
              _buildDesc(),
            ],
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 40.h,
            child: _buildBottomButtons(context),
          ),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Container(
      padding: EdgeInsets.fromLTRB(40.w, 34.h, 40.w, 34.h),
      decoration: const BoxDecoration(
        // border: Border(
        //   bottom: BorderSide(
        //     color: Color(0xFF00D4FF),
        //     width: 1,
        //   ),
        // ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '告警详情',
            style: TextStyle(
              color: Colors.white,
              fontSize: 28.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildContent(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.only(left: 60.w, right: 60.w),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildImagePlaceholder(),
            SizedBox(width: 30.w),
            Expanded(
              child: _buildAlertInfo(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildImagePlaceholder() {
    return Container(
      width: 280.w,
      height: 200.h,
      decoration: BoxDecoration(
        color: const Color(0xFF1A2332),
        borderRadius: BorderRadius.circular(12.r),
      ),
    );
  }

  Widget _buildAlertInfo() {
    final isPending = alert.status.contains('待处理');

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              alert.title,
              style: TextStyle(
                color: const Color(0xFFFFB700),
                fontSize: 48.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              alert.time,
              style: TextStyle(
                color: Color(0xFFB1F0FF),
                fontSize: 24.sp,
              ),
            ),
          ],
        ),
        SizedBox(height: 20.h),
        _buildInfoRow('处理状态', isPending ? '待处理' : '已处理', isPending),
        _buildInfoRow('处理时间', '', false),
        SizedBox(height: 30.h),
      ],
    );
  }

  Widget _buildDesc() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: 60.w,right: 60.w),
      decoration: BoxDecoration(
        // border: Border.all(
        //   color: Colors.red,
        //   width: 1.w,
        // ),
      ),
      child: SingleChildScrollView(
        child: Text(
          alert.description,
          style: TextStyle(
            color: Color(0xFFB1F0FF),
            fontSize: 24.sp,
            height: 1.5,
          ),
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String value, bool isHighlight) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 8.h),
      child: Row(
        children: [
          SizedBox(
            width: 140.w,
            child: Text(
              label,
              style: TextStyle(
              color: Colors.white,
              fontSize: 30.sp,
            ),
            ),
          ),
          SizedBox(width: 20.w),
          Text(
            value,
            style: TextStyle(
              color: isHighlight ? const Color(0xFFFFB700) : Colors.white,
              fontSize: 30.sp,
              fontWeight: isHighlight ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBottomButtons(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(40.w, 20.h, 40.w, 30.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _buildButton(
            '关闭窗口',
            const Color(0xFF2A2E3B),
            Colors.white,
            const Color(0xFF4A4E5B),
            () => Navigator.of(context).pop(),
          ),
          SizedBox(width: 30.w),
          _buildButton(
            '标记误报',
            const Color(0xFF2A2E3B),
            Colors.white,
            const Color(0xFF4A4E5B),
            () => _handleMarkAsFalse(context),
          ),
          SizedBox(width: 30.w),
          _buildButton(
            '已处理',
            const Color(0xFF00D4FF),
            Color(0xFFB1F0FF),
            const Color(0xFF00D4FF),
            () => _handleMarkAsProcessed(context),
            isActive: true,
          ),
        ],
      ),
    );
  }

  Widget _buildButton(
    String text,
    Color backgroundColor,
    Color textColor,
    Color borderColor,
    VoidCallback onPressed, {
    bool isActive = false,
  }) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        width: 261.w,
        height: 84.h,
        decoration: BoxDecoration(
          image: const DecorationImage(
            image: AssetImage('lib/assets/monitoring/button_alert.png'),
            fit: BoxFit.fill,
          ),
          // border: Border.all(
          //   color: borderColor,
          //   width: 2.w,
          // ),
          borderRadius: BorderRadius.circular(8.r),
          boxShadow: isActive
              ? [
                  BoxShadow(
                    color: borderColor.withValues(alpha: 128),
                    blurRadius: 10.r,
                    offset: const Offset(0, 0),
                  ),
                ]
              : null,
        ),
        child: Center(
          child: Text(
            text,
            style: TextStyle(
              color: textColor,
              fontSize: 32.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }

  void _handleMarkAsFalse(BuildContext context) {
    context.read<MonitoringIndexCubit>().markAlertAsFalse(alert);
    Navigator.of(context).pop();
  }

  void _handleMarkAsProcessed(BuildContext context) {
    context.read<MonitoringIndexCubit>().markAlertAsProcessed(alert);
    Navigator.of(context).pop();
  }
}