import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class URLInput extends StatelessWidget {
  final String title;
  final String initialText;
  final Function(String)? newText;

  URLInput(this.title, this.initialText, this.newText, {super.key});

  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    _controller.text = initialText;

    return Container(
        margin: const EdgeInsets.only(top: 20, left: 16, right: 16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title,
                style: const TextStyle(
                    decoration: TextDecoration.none,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: CupertinoColors.black)),
            const SizedBox(height: 15),
            CupertinoTextField(
              padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 18),
              decoration: const BoxDecoration(
                border: null,
                borderRadius: BorderRadius.all(Radius.circular(12)),
                color: CupertinoColors.white,
              ),
              minLines: 1,
              maxLines: 10,
              readOnly: true,
              maxLength: null,
              autocorrect: false,
              keyboardType: TextInputType.multiline,
              controller: _controller,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                CupertinoButton(
                    onPressed: _getClipboardText,
                    child: const Text('Вставка',
                        style: TextStyle(
                            decoration: TextDecoration.none,
                            fontSize: 17,
                            fontWeight: FontWeight.w500,
                            color: CupertinoColors.link))),
                CupertinoButton(
                    onPressed: _cleanTextField,
                    child: const Text('Очистить',
                        style: TextStyle(
                            decoration: TextDecoration.none,
                            fontSize: 17,
                            fontWeight: FontWeight.w500,
                            color: CupertinoColors.link)))
              ],
            )
          ],
        )
    );
  }

  void _getClipboardText() async {
    final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
    _controller.text = clipboardData?.text ?? '';
    newText?.call(_controller.text);
  }

  void _cleanTextField() async {
    _controller.text = '';
    newText?.call('');
  }
}