import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:guru_ui/widget/alerts/dialog_single_input_alert_design_model.dart';

import 'dialog_standard_container.dart';

class DialogSingleInputAlertSpaceConfig {
  final double? titleTopSpace;
  final double? inputTopSpace;
  final double? buttonTopSpace;
  final double? buttonBottomSpace;
  DialogSingleInputAlertSpaceConfig({
    this.titleTopSpace,
    this.inputTopSpace,
    this.buttonTopSpace,
    this.buttonBottomSpace,
  });
}

class DialogSingleInputAlertWidget extends StatefulWidget {
  final Color? backgroundColor;
  final Image? closeImage;
  final String titleText;
  final TextStyle? titleTextStyle;
  final Color? titleColor;
  final double? titleFontSize;

  final String buttonText;
  final TextStyle? buttonTextStyle;
  final Color? buttonTextColor;
  final double? buttonTextFontSize;
  final Function(String value)? buttonCallback;
  final Color? buttonBackgroundColor;
  final VoidCallback? closeCallback;
  final TextInputType? textInputType;
  final Color? inputBackgroundColor;
  final double? inputFontSize;
  final TextStyle? inputTextStyle;
  final Color? inputTextColor;
  final String? placeholderText;
  final TextStyle? placeholderTextStyle;
  final Color? placeholderTextColor;
  final double? placeholderTextFontSize;
  final Color? cursorColor;
  final DialogSingleInputAlertSpaceConfig? spaceConfig;
  const DialogSingleInputAlertWidget({
    Key? key,
    this.closeImage,
    required this.titleText,
    this.titleTextStyle,
    this.titleColor,
    this.titleFontSize,
    required this.buttonText,
    this.buttonTextStyle,
    this.buttonTextColor,
    this.buttonTextFontSize,
    this.buttonCallback,
    this.buttonBackgroundColor,
    this.closeCallback,
    this.backgroundColor,
    this.inputBackgroundColor,
    this.inputFontSize,
    this.inputTextColor,
    this.inputTextStyle,
    this.textInputType,
    this.placeholderText,
    this.placeholderTextStyle,
    this.placeholderTextColor,
    this.placeholderTextFontSize,
    this.cursorColor,
    this.spaceConfig,
  }) : super(key: key);

  @override
  _DialogSingleInputAlertWidgetState createState() => _DialogSingleInputAlertWidgetState();
}

class _DialogSingleInputAlertWidgetState extends State<DialogSingleInputAlertWidget> {
  DialogSingleInputAlertDesignSpec get designSpec => DialogSingleInputAlertDesignSpec.get();
  DialogSingleInputAlertContentDesignSpec get contentSpec => designSpec.contentSpec;
  String _submitText = "";
  @override
  Widget build(BuildContext context) {
    return Center(
        child: DialogStandardContainer(
      backgroundColor: widget.backgroundColor,
      closeImage: widget.closeImage,
      closeCallback: widget.closeCallback,
      showCloseImage: true,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: widget.spaceConfig?.titleTopSpace != null
                ? EdgeInsets.only(top: widget.spaceConfig!.titleTopSpace!, left: contentSpec.titleInsets.left, right: contentSpec.titleInsets.right)
                : contentSpec.titleInsets,
            child: Text(
              widget.titleText,
              style: widget.titleTextStyle ??
                  TextStyle(
                      color: widget.titleColor ?? const Color(0xff000000),
                      fontSize: widget.titleFontSize ?? contentSpec.titleFontSize,
                      fontWeight: FontWeight.w700),
            ),
          ),
          SizedBox(
            height: widget.spaceConfig?.inputTopSpace ?? contentSpec.inputContainerTopSpace,
          ),
          Container(
            width: contentSpec.inputWidth,
            constraints: BoxConstraints(
              maxHeight: 200.0,
              minHeight: contentSpec.inputHeight,
            ),
            decoration: BoxDecoration(color: widget.inputBackgroundColor ?? Colors.grey[200], borderRadius: contentSpec.inputRadius),
            padding: contentSpec.inputInsets,
            child: TextField(
              autofocus: true,
              cursorWidth: contentSpec.cursorWidth,
              cursorRadius: Radius.circular(contentSpec.cursorWidth / 2),
              cursorColor: widget.cursorColor ?? const Color(0xff008B54),
              cursorHeight: widget.inputFontSize ?? contentSpec.inputFontSize,
              maxLines: null,
              keyboardType: widget.textInputType ?? TextInputType.multiline,
              style:widget.inputTextStyle ?? TextStyle(color:widget.inputTextColor ?? const Color(0xff000000) ,fontSize: widget.inputFontSize ?? contentSpec.inputFontSize,fontWeight: FontWeight.w500, textBaseline: TextBaseline.alphabetic),
              decoration: InputDecoration.collapsed(
                hintText: widget.placeholderText ?? "",
                hintStyle: widget.placeholderTextStyle ?? TextStyle(color:widget.placeholderTextColor ?? const Color(0xff808080), fontSize:widget.placeholderTextFontSize ?? contentSpec.inputFontSize,fontWeight: FontWeight.w500),
              ),
              onChanged: (string) {
                _submitText = string;
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.only(
                top: widget.spaceConfig?.buttonTopSpace ?? contentSpec.buttonTopSpace,
                bottom: widget.spaceConfig?.buttonBottomSpace ?? contentSpec.buttonBottomSpace),
            child: GestureDetector(
              onTap: () {
                widget.buttonCallback?.call(_submitText);
              },
              child: Container(
                  width: contentSpec.buttonWidth,
                  height: contentSpec.buttonHeight,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(color: widget.buttonBackgroundColor ?? const Color(0xff008B54), borderRadius: contentSpec.buttonRadius),
                  child: Text(
                    widget.buttonText,
                    style: widget.buttonTextStyle ??
                        TextStyle(
                            fontSize: widget.buttonTextFontSize ?? contentSpec.buttonFontSize,
                            color: widget.buttonTextColor ?? Colors.white,
                            fontWeight: FontWeight.w600),
                    textAlign: TextAlign.center,
                  )),
            ),
          )
        ],
      ),
    ));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
