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/utils/numeric_keyboard.dart';

import 'device_control_common.dart';

/// 设置雾化/消毒时间面板
class DeviceControlTimePanel extends StatelessWidget {
  const DeviceControlTimePanel({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<DeviceControlBloc, DeviceControlState>(
      buildWhen: (previous, current) =>
          previous.data.nebulizationTime != current.data.nebulizationTime ||
          previous.data.disinfectionTime != current.data.disinfectionTime,
      builder: (context, state) {
        final nebulizationMinutes = state.data.nebulizationTime.setMinutes;
        final disinfectionMinutes = state.data.disinfectionTime.setMinutes;

        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: 18.h),
              Expanded(
                child: _TimeValueButton(
                  value: nebulizationMinutes,
                  active: false,
                  onTap: () => _showTimeKeyboard(
                    context,
                    initialValue: nebulizationMinutes,
                    eventBuilder: (value) =>
                        DeviceControlNebulizationTimeChanged(value),
                  ),
                ),
              ),
              Divider(
                color: Colors.white.withValues(alpha: 0.16),
                height: 36.h,
              ),
              const DeviceControlPanelTitle('设置消毒时间'),
              SizedBox(height: 18.h),
              Expanded(
                child: _TimeValueButton(
                  value: disinfectionMinutes,
                  active: true,
                  onTap: () => _showTimeKeyboard(
                    context,
                    initialValue: disinfectionMinutes,
                    eventBuilder: (value) =>
                        DeviceControlDisinfectionTimeChanged(value),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  static Future<void> _showTimeKeyboard(
    BuildContext context, {
    required String initialValue,
    required DeviceControlEvent Function(String value) eventBuilder,
  }) async {
    final result = await NumericKeyboard.show(
      context,
      initialValue: initialValue == '--' ? '' : initialValue,
      decimalEnabled: false,
    );
    if (!context.mounted || result == null) return;

    final value = result.trim();
    if (value.isEmpty) return;
    context.read<DeviceControlBloc>().add(eventBuilder(value));
  }
}

class _TimeValueButton extends StatelessWidget {
  const _TimeValueButton({
    required this.value,
    required this.active,
    required this.onTap,
  });

  final String value;
  final bool active;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: DeviceControlDigitalValueBox(
        value: value,
        unit: '分钟',
        active: active,
      ),
    );
  }
}
