import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/localization/app_localizations.dart';
import '../../../core/theme/app_theme.dart';
import '../../../shared/utils/constants.dart';
import '../../../shared/widgets/common_button.dart';
import '../models/program.dart';
import '../providers/programs_provider.dart';

/// 程序表单弹窗
/// 用于新增和编辑程序
class ProgramFormDialog extends ConsumerStatefulWidget {
  final Program? program;

  const ProgramFormDialog({super.key, this.program});

  @override
  ConsumerState<ProgramFormDialog> createState() => _ProgramFormDialogState();
}

class _ProgramFormDialogState extends ConsumerState<ProgramFormDialog> {
  final _formKey = GlobalKey<FormState>();
  late TextEditingController _codeController;
  late TextEditingController _nameController;
  late TextEditingController _temperatureController;
  late TextEditingController _airflowTimeController;
  bool _isEnabled = true;
  bool _isSaving = false;

  @override
  void initState() {
    super.initState();
    _codeController = TextEditingController(text: widget.program?.code ?? '');
    _nameController = TextEditingController(text: widget.program?.name ?? '');
    _temperatureController =
        TextEditingController(text: '${widget.program?.temperature ?? 50}');
    _airflowTimeController =
        TextEditingController(text: '${widget.program?.airflowTime ?? 60}');
    _isEnabled = widget.program?.status == 1;
  }

  @override
  void dispose() {
    _codeController.dispose();
    _nameController.dispose();
    _temperatureController.dispose();
    _airflowTimeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    final isEditing = widget.program != null;

    return AlertDialog(
      title: Text(
        isEditing
            ? (l10n?.editProgram ?? '编辑程序')
            : (l10n?.addProgram ?? '新增程序'),
      ),
      content: SizedBox(
        width: 400,
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 编号输入
              TextFormField(
                controller: _codeController,
                decoration: InputDecoration(
                  labelText: l10n?.programCode ?? '编号',
                  hintText: l10n?.hintCode ?? '例如: P001',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                validator: (value) {
                  if (value == null || value.trim().isEmpty) {
                    return l10n?.enterCode ?? '请输入编号';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),

              // 名称输入
              TextFormField(
                controller: _nameController,
                decoration: InputDecoration(
                  labelText: l10n?.programName ?? '名称',
                  hintText: l10n?.hintProgramName ?? '请输入程序名称',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                validator: (value) {
                  if (value == null || value.trim().isEmpty) {
                    return l10n?.enterName ?? '请输入名称';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),

              // 温度和吹气时间
              Row(
                children: [
                  Expanded(
                    child: TextFormField(
                      controller: _temperatureController,
                      decoration: InputDecoration(
                        labelText: '${l10n?.temperature ?? '温度'} (°C)',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      keyboardType: TextInputType.number,
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: TextFormField(
                      controller: _airflowTimeController,
                      decoration: InputDecoration(
                        labelText: '${l10n?.airflowTime ?? '吹气时间'} (${Constants.timeUnitSeconds})',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      keyboardType: TextInputType.number,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 16),

              // 状态开关
              Row(
                children: [
                  Text(
                    l10n?.status ?? '状态',
                    style: TextStyle(color: AppTheme.textPrimary),
                  ),
                  const Spacer(),
                  Switch(
                    value: _isEnabled,
                    onChanged: (value) {
                      setState(() {
                        _isEnabled = value;
                      });
                    },
                    activeColor: AppTheme.successColor,
                  ),
                  Text(
                    _isEnabled ? (l10n?.enabled ?? '启用') : (l10n?.disabled ?? '停用'),
                    style: TextStyle(
                      color: _isEnabled ? AppTheme.successColor : AppTheme.idleColor,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
      actions: [
        TextButton(
          onPressed: _isSaving ? null : () => Navigator.of(context).pop(),
          child: Text(l10n?.cancel ?? '取消'),
        ),
        CommonButton(
          text: l10n?.save ?? '保存',
          icon: Icons.save,
          type: ButtonType.primary,
          isLoading: _isSaving,
          onPressed: _isSaving ? null : () => _saveProgram(context, ref, l10n),
        ),
      ],
    );
  }

  /// 保存程序
  Future<void> _saveProgram(
    BuildContext context,
    WidgetRef ref,
    AppLocalizations? l10n,
  ) async {
    if (!_formKey.currentState!.validate()) return;

    setState(() {
      _isSaving = true;
    });

    final notifier = ref.read(programsProvider.notifier);
    final now = DateTime.now().toString().substring(0, 10);

    final program = Program(
      id: widget.program?.id,
      code: _codeController.text.trim(),
      name: _nameController.text.trim(),
      createdAt: widget.program?.createdAt ?? now,
      status: _isEnabled ? 1 : 0,
      temperature: int.tryParse(_temperatureController.text) ?? 50,
      airflowTime: int.tryParse(_airflowTimeController.text) ?? 60,
    );

    bool success;
    if (widget.program != null) {
      success = await notifier.updateProgram(program);
    } else {
      success = await notifier.addProgram(program);
    }

    setState(() {
      _isSaving = false;
    });

    if (success) {
      Navigator.of(context).pop();
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(l10n?.saveFailed ?? '保存失败，请检查编号是否重复'),
          backgroundColor: AppTheme.errorColor,
        ),
      );
    }
  }
}