import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laki_icu_app/models/bo/monitoring_bo.dart';
 

class MonitoringPetInfoCard extends StatelessWidget {
  final PatientInfoBO? patientInfo;
  final VoidCallback? onExit;
  final VoidCallback? onBindPet;

  const MonitoringPetInfoCard({
    super.key,
    required this.patientInfo,
    this.onExit,
    this.onBindPet,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(image: AssetImage('lib/assets/monitoring/box_bg_460x905.png'),fit: BoxFit.fill),
        // border: Border.all(color: Colors.red,width: 1.w)
      ),
      padding: EdgeInsets.only(left: 40.w,right: 40.w, top: 32.h),
      child: patientInfo == null ? _buildEmpty() : _buildContent(patientInfo!),
    );
  }

  Widget _buildEmpty() {
    return Column(
      children: [
        SizedBox(height: 10.h),
        // 顶部区域 - 空闲舱
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              width: 150.w,
              height: 160.w,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20.r),
                color: const Color(0xFF1F4E79).withOpacity(0.3),
              ),
              child: Center(
                child: Icon(
                  Icons.pets,
                  color: const Color(0xFFAAFAFF).withOpacity(0.5),
                  size: 80.w,
                ),
              ),
            ),
            SizedBox(width: 24.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '空闲舱',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 40.sp,
                      fontWeight: FontWeight.w700,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h),
                  Text(
                    '已就绪可入住',
                    style: TextStyle(
                      color: const Color(0xFFB8E8FF),
                      fontSize: 25.sp,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        SizedBox(height: 132.h),
        // 猫咪图片
        Image.asset(
          'lib/assets/monitoring/cat_sleep.png',
          width: 254.w,
          height: 132.h,
          fit: BoxFit.contain,
        ),
        SizedBox(height: 20.h),
        // 护理等级区域
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '护理等级',
              style: TextStyle(
                color: Colors.white,
                fontSize: 30.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: 41.h),
            Text(
              '暂无',
              style: TextStyle(
                color: Colors.white,
                fontSize: 64.sp,
                fontWeight: FontWeight.bold,
                letterSpacing: 8.w,
              ),
            ),
            SizedBox(height: 55.h),
            // 进度条
            Container(
              padding: EdgeInsets.symmetric(horizontal: 4.w),
              decoration: BoxDecoration(
                color: const Color(0xFF0A1A2E),
                borderRadius: BorderRadius.circular(6.r),
              ),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 2.w),
                      height: 10.h,
                      decoration: BoxDecoration(
                        color: const Color(0xFFFFFFFF),
                        borderRadius: BorderRadius.circular(2.r),
                        boxShadow: const [
                          BoxShadow(
                            color: Color(0x99FFFFFF),
                            blurRadius: 6,
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 2.w),
                      height: 10.h,
                      decoration: BoxDecoration(
                        color: const Color(0xFF25E5B0).withOpacity(0.3),
                        borderRadius: BorderRadius.circular(2.r),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 2.w),
                      height: 10.h,
                      decoration: BoxDecoration(
                        color: const Color(0xFFFFB347).withOpacity(0.3),
                        borderRadius: BorderRadius.circular(2.r),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 2.w),
                      height: 10.h,
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF7A4D).withOpacity(0.3),
                        borderRadius: BorderRadius.circular(2.r),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 2.w),
                      height: 10.h,
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF4E4E).withOpacity(0.3),
                        borderRadius: BorderRadius.circular(2.r),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        SizedBox(height: 26.h),
        // 绑定宠物按钮
        GestureDetector(
          onTap: onBindPet,
          child: Container(
            width: double.infinity,
            height: 140.h,
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('lib/assets/monitoring/title_bg_spacewalk.png'),
                fit: BoxFit.fill,
              ),
            ),
            child: Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                   
                  SizedBox(width: 24.w),
                  Text(
                    '绑定宠物',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 32.sp,
                      fontWeight: FontWeight.w700,
                      letterSpacing: 4.w,
                    ),
                  ),
                  SizedBox(width: 24.w),
                  
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildContent(PatientInfoBO info) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 10.h),
        _buildHeader(info),
        SizedBox(height: 24.h),
        _buildDivider(),
        SizedBox(height: 20.h),
        _buildInfoBlock(info),
        SizedBox(height: 20.h),
        _buildDivider(),
        SizedBox(height: 30.h),
        _buildCareLevelBlock(info),
        SizedBox(height: 25.h),
        
        _buildExitButton(),
      ],
    );
  }

  Widget _buildHeader(PatientInfoBO info) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 150.w,
          height: 160.w, 
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20.r),
            color: const Color(0xFF1F4E79),
          ),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(20.r),
            child: info.avatarUrl.isNotEmpty
                ? Image.network(
                    info.avatarUrl,
                    fit: BoxFit.cover,
                    errorBuilder: (_, __, ___) => Icon(
                      Icons.pets,
                      color: const Color(0xFFAAFAFF),
                      size: 60.w,
                    ),
                  )
                : Icon(
                    Icons.pets,
                    color: const Color(0xFFAAFAFF),
                    size: 60.w,
                  ),
          ),
        ),
        SizedBox(width: 24.w),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                info.name,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 42.sp,
                  fontWeight: FontWeight.w700,
                  height: 1.2,
                ),
              ),
              SizedBox(height: 20.h),
              _buildTypeRow('类型', info.type, true),
              SizedBox(height: 12.h),
              _buildTypeRow('品种', info.breed, false),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildTypeRow(String label, String value, bool first) {
    return Row(
      children: [
        SizedBox(
          width: 20.w,
          child: Column(
            children: [
              if (first)
                Container(
                  width: 6.w,
                  height: 6.w,
                  decoration: const BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color(0xFF5EE6FF),
                  ),
                )
              else
                SizedBox(height: 6.w),
              Container(
                width: 1.w,
                height: 18.h,
                color: const Color(0xFF5EE6FF),
              ),
            ],
          ),
        ),
        SizedBox(width: 10.w),
        Text(
          '$label：$value',
          style: TextStyle(
            color: const Color(0xFFB8E8FF),
            fontSize: 22.sp,
          ),
        ),
      ],
    );
  }

  Widget _buildDivider() {
    return Container(
      height: 1.h,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Colors.transparent,
            Color(0x405EE6FF),
            Color(0x805EE6FF),
            Color(0x405EE6FF),
            Colors.transparent,
          ],
        ),
      ),
    );
  }

  Widget _buildInfoBlock(PatientInfoBO info) {
    return Column(
      children: [
        _buildInfoItem(Icons.description_outlined, '宠物病因', info.disease),
        SizedBox(height: 20.h),
        _buildInfoItem(Icons.home_outlined, '入住时间', info.checkInDate),
        SizedBox(height: 20.h),
        _buildInfoItem(Icons.medical_services_outlined, '责任医生', info.doctor),
        SizedBox(height: 20.h),
        _buildInfoItem(Icons.phone_iphone_outlined, '家长电话', info.phone),
      ],
    );
  }

  Widget _buildInfoItem(IconData icon, String label, String value) {
    return Row(
      children: [
        Icon(icon, color: const Color(0xFF5EE6FF), size: 30.w),
        SizedBox(width: 18.w),
        Text(
          '$label：',
          style: TextStyle(
            color: const Color(0xFFB8E8FF),
            fontSize: 22.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: Colors.white,
            fontSize: 22.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget _buildCareLevelBlock(PatientInfoBO info) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '护理等级',
          style: TextStyle(
            color: Colors.white,
            fontSize: 30.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        SizedBox(height: 40.h),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              info.careLevel,
              style: TextStyle(
                color: Colors.white,
                fontSize: 64.sp,
                fontWeight: FontWeight.bold,
                letterSpacing: 6.w,
                shadows: const [
                  Shadow(
                    color: Color(0x99FF5E5E),
                    blurRadius: 16,
                  ),
                ],
              ),
            ),
            const Spacer(),
            Image.asset(
              'lib/assets/monitoring/warning.png',
              width: 100.w,
              height: 100.w,
            ),
          ],
        ),
        SizedBox(height: 49.h),
        _buildLevelBar(),
      ],
    );
  }

  Widget _buildLevelBar() {
    const segments = [
      Color(0xFFFFFFFF),
      Color(0xFF25E5B0),
      Color(0xFFFFB347),
      Color(0xFFFF7A4D),
      Color(0xFFFF4E4E),
    ];
    const shadowColors = [
      Color(0x99FFFFFF),
      Color(0x9925E5B0),
      Color(0x99FFB347),
      Color(0x99FF7A4D),
      Color(0x99FF4E4E),
    ];
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 4.w),
      decoration: BoxDecoration(
        color: const Color(0xFF0A1A2E),
        borderRadius: BorderRadius.circular(6.r),
      ),
      child: Row(
        children: segments
            .asMap()
            .entries
            .map((entry) => Expanded(
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 2.w),
                    height: 10.h,
                    decoration: BoxDecoration(
                      color: entry.value,
                      borderRadius: BorderRadius.circular(2.r),
                      boxShadow: [
                        BoxShadow(
                          color: shadowColors[entry.key],
                          blurRadius: 6,
                        ),
                      ],
                    ),
                  ),
                ))
            .toList(),
      ),
    );
  }

  Widget _buildExitButton() {
    return GestureDetector(
      onTap: onExit,
      child: Container(
        width: double.infinity,
        height: 140.h,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('lib/assets/monitoring/title_bg_spacewalk.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(width: 24.w),
              Text(
                '出舱',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 28.sp,
                  fontWeight: FontWeight.w700,
                  letterSpacing: 4.w,
                ),
              ),
              SizedBox(width: 24.w),
            ],
          ),
        ),
      ),
    );
  }
}
