import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laki_icu_app/blocs/device_control/device_control_bloc.dart';
import 'package:laki_icu_app/blocs/device_control/device_control_event.dart';
import 'package:laki_icu_app/blocs/device_control/device_control_state.dart';
import 'package:laki_icu_app/models/bo/device_control_bo.dart';

import 'device_control_common.dart';

/// 风速调节面板
class DeviceControlWindPanel extends StatelessWidget {
  const DeviceControlWindPanel({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<DeviceControlBloc, DeviceControlState>(
      buildWhen: (previous, current) =>
          previous.data.windSpeed != current.data.windSpeed,
      builder: (context, state) {
        final currentMode = state.data.windSpeed.currentMode;

        return DeviceControlPanelFrame(
          backgroundAsset: assetPanelTall,
          padding: EdgeInsets.fromLTRB(34.w, 28.h, 34.w, 28.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const DeviceControlPanelTitle('风速调节'),
              SizedBox(height: 24.h),
              Expanded(
                child: _WideModeButton(
                  label: '睡眠模式',
                  icon: Icons.nightlight_round,
                  active: currentMode == WindSpeedMode.sleep,
                  onTap: () => context.read<DeviceControlBloc>().add(
                        const DeviceControlWindSpeedModeChanged(
                          WindSpeedMode.sleep,
                        ),
                      ),
                ),
              ),
              SizedBox(height: 18.h),
              Expanded(
                child: _WideModeButton(
                  label: '舒适模式',
                  icon: Icons.favorite_border,
                  active: currentMode == WindSpeedMode.comfort,
                  onTap: () => context.read<DeviceControlBloc>().add(
                        const DeviceControlWindSpeedModeChanged(
                          WindSpeedMode.comfort,
                        ),
                      ),
                ),
              ),
              SizedBox(height: 18.h),
              Expanded(
                child: _WideModeButton(
                  label: '强劲模式',
                  icon: Icons.air,
                  active: currentMode == WindSpeedMode.strong,
                  onTap: () => context.read<DeviceControlBloc>().add(
                        const DeviceControlWindSpeedModeChanged(
                          WindSpeedMode.strong,
                        ),
                      ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}

class _WideModeButton extends StatelessWidget {
  const _WideModeButton({
    required this.label,
    required this.icon,
    this.active = false,
    this.onTap,
  });

  final String label;
  final IconData icon;
  final bool active;
  final VoidCallback? onTap;

  @override
  Widget build(BuildContext context) {
    final color = active ? const Color(0xFF45F36F) : const Color(0xFF7DC4EA);
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        width: double.infinity,
        decoration: deviceControlButtonDecoration(
          active: active,
          backgroundAsset: assetButton,
        ),
        padding: EdgeInsets.symmetric(horizontal: 28.w),
        child: Row(
          children: [
            Icon(icon, color: color, size: 52.w),
            SizedBox(width: 34.w),
            Expanded(
              child: Text(
                label,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: color,
                  fontSize: 26.sp,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
