import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/bloc/curtain_module_bloc.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/widgets/accurate_dialog_widget.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/widgets/calibrate_completed_dialog.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/widgets/number_input_textfield.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class QuickCalibratingDialog extends StatefulWidget {
  final int timControl;
  final String deviceId;
  final BuildContext parentContext;
  const QuickCalibratingDialog({
    super.key,
    required this.timControl,
    required this.deviceId,
    required this.parentContext,
  });

  @override
  State<QuickCalibratingDialog> createState() => _QuickCalibratingDialogState();
}

class _QuickCalibratingDialogState extends State<QuickCalibratingDialog> {
  late TextEditingController _controller;
  String? _errorText;

  void _onRightTap() {
    final value = int.tryParse(_controller.text);

    if (value == null || value < 10 || value > 120) {
      setState(() {
        _errorText = 'Number should be between 10 and 120';
      });
      return;
    }

    setState(() {
      _errorText = null;
    });
    widget.parentContext.read<CurtainModuleBloc>().add(
          ChangeTimerControlEvent(
            deviceId: widget.deviceId,
            timControl: value,
          ),
        );
    Navigator.of(widget.parentContext).pop();
    showDialog(
      context: widget.parentContext,
      builder: (_) => CalibrateCompletedDialog(
        parentContext: widget.parentContext,
        deviceId: widget.deviceId,
      ),
    );
  }

  @override
  void initState() {
    _controller = TextEditingController(text: widget.timControl.toString());
    super.initState();
  }

  @override
  Widget build(_) {
    return AlertDialog(
      backgroundColor: ColorsManager.whiteColors,
      contentPadding: EdgeInsets.zero,
      content: AccurateDialogWidget(
        title: 'Calibrating',
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Expanded(
              child: Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(right: 75),
                  child: Text(
                    '1.please Enter the Travel Time:',
                    style: TextStyle(color: ColorsManager.lightGrayColor),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Align(
                alignment: Alignment.center,
                child: Container(
                  width: 130,
                  padding: const EdgeInsets.all(5),
                  decoration: BoxDecoration(
                    color: ColorsManager.neutralGray.withValues(
                      alpha: 0.5,
                    ),
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Padding(
                            padding: const EdgeInsetsGeometry.only(left: 5),
                            child: NumberInputField(controller: _controller)),
                      ),
                      Expanded(
                        child: Text(
                          'seconds',
                          style: TextStyle(
                            fontSize: 12,
                            color: ColorsManager.dialogBlueTitle,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            if (_errorText != null)
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                  child: Text(
                    _errorText!,
                    style: const TextStyle(
                      color: ColorsManager.red,
                      fontSize: 14,
                    ),
                  ),
                ),
              ),
            const Expanded(
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Text(
                  '2.click Next to Complete the calibration',
                  style: TextStyle(color: ColorsManager.lightGrayColor),
                ),
              ),
            )
          ],
        ),
        leftOnTap: () => Navigator.of(widget.parentContext).pop(),
        rightOnTap: _onRightTap,
      ),
    );
  }
}
