import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'alert_dialog.dart';

/// 小弹窗输入
class VDialogInput extends StatelessWidget {
  /// 标题
  final String? title;

  /// 描述
  final String? description;

  /// 输入占位符
  final String? placeholder;

  /// 初始值
  final String? initialValue;

  const VDialogInput({
    super.key,
    this.title,
    this.description,
    this.placeholder,
    this.initialValue,
  });

  Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);

  @override
  Widget build(BuildContext context) {
    final controller = TextEditingController(text: initialValue);
    return VAlertDialog(
      title: title,
      width: 440,
      contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
      content: _buildContent(context, controller),
      showCancel: true,
      onConfirm: () {
        Get.back(result: controller.text);
      },
    );
  }

  Widget _buildContent(
    BuildContext context,
    TextEditingController controller,
  ) {
    final children = <Widget>[];
    if (description != null) {
      children.add(
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 4),
          child: Text(
            description!,
            style: const TextStyle(color: Colors.black87, fontSize: 18),
          ),
        ),
      );
      children.add(const SizedBox(height: 8));
    } else {
      children.add(const SizedBox(height: 12));
    }
    children.add(_buildInputWidget(context, controller));
    return SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: children,
      ),
    );
  }

  Widget _buildInputWidget(
    BuildContext context,
    TextEditingController controller,
  ) {
    const fontSize = 20.0;
    const height = 56.0;
    return SizedBox(
      height: height,
      child: TextField(
        controller: controller,
        readOnly: false,
        autofocus: true,
        style: const TextStyle(fontSize: fontSize),
        decoration: InputDecoration(
          border: const UnderlineInputBorder(
            borderRadius: BorderRadius.zero,
            borderSide: BorderSide(),
          ),
          enabledBorder: const UnderlineInputBorder(
            borderRadius: BorderRadius.zero,
            borderSide: BorderSide(
              color: Colors.black54,
            ),
          ),
          focusedBorder: UnderlineInputBorder(
            borderRadius: BorderRadius.zero,
            borderSide: BorderSide(
              color: Theme.of(context).primaryColor.withOpacity(.4),
            ),
          ),
          filled: true,
          fillColor: Colors.white,
          contentPadding: const EdgeInsets.symmetric(
            vertical: (height - fontSize * 1.2) / 2,
            horizontal: 8,
          ),
          hintStyle: const TextStyle(fontSize: fontSize),
          labelStyle: const TextStyle(fontSize: fontSize),
          hintText: placeholder,
          isCollapsed: true,
        ),
        onSubmitted: (value) {
          Get.back(result: value);
        },
      ),
    );
  }
}
