// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:bestapp_package/bestapp_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tacban/assets/theme/colors.dart';

class SearchInput extends StatefulWidget {
  final TextEditingController? textController;
  final bool? showIcon;
  final IconData? prefixIcon;
  final String? hintText;
  final bool enable;
  final IconData? sufixIcon;
  final TextInputType? textInputType;
  final List<TextInputFormatter>? inputFormatters;
  final Function? onSufixtap;
  final int? maxLines;
  final Function? onChanged;
  final Function(String)? onFieldSubmitted;
  final bool validator;
  final bool validatorEmail;
  final bool validatorPhone;
  final String? labelText;
  final TextInputAction textInputAction;
  const SearchInput({
    Key? key,
    this.textController,
    this.showIcon,
    this.prefixIcon,
    this.hintText,
    this.enable=true,
    this.sufixIcon,
    this.textInputType,
    this.inputFormatters,
    this.onSufixtap,
    this.maxLines,
    this.onChanged,
    this.onFieldSubmitted,
    this.validator=true,
    this.validatorEmail=false,
    this.validatorPhone=false,
    this.textInputAction = TextInputAction.done,
    this.labelText,
  }) : super(key: key);

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

class _SearchInput extends State<SearchInput> {
  @override
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      data: MediaQuery.of(context).copyWith(
        textScaleFactor: 1.0,
      ),
      child: Container(
        padding: const EdgeInsets.all(0),
        width: MediaQuery.of(context).size.width,
        child: TextFormField(
          validator: (value) {
            if (value!.isEmpty && widget.validator) {
              return 'Campo não pode estar vazio!';
            }
            if(!EmailValidator.validate(value) && widget.validatorEmail == true && value.isNotEmpty){
              return 'E-mail inválido!';
            }
            if(value.isNotEmpty && BestappUtils.removeCaracteres(value).length < 10 && widget.validatorPhone){
              return 'Telefone inválido!';
            }
            return null;
          },
          controller: widget.textController,
          textInputAction: widget.textInputAction,
          // textCapitalization: widget.textInputType == TextInputType.emailAddress ? TextCapitalization.none : TextCapitalization.words,
          enabled: widget.enable,
          keyboardType: widget.textInputType,
          onChanged: widget.onChanged as void Function(String)?,
          onFieldSubmitted: widget.onFieldSubmitted,
          inputFormatters: widget.inputFormatters,
          decoration: InputDecoration(
            labelText: widget.labelText,
            fillColor: Theme.of(context).colorScheme.background,
            filled: true,
            floatingLabelBehavior: FloatingLabelBehavior.always,
            contentPadding: const EdgeInsets.fromLTRB(10.0, 9.0, 10.0, 9.0),
            prefixIcon: widget.prefixIcon != null ? Icon(
              widget.prefixIcon,
              color: AppColorLight.secondaryColor,
            ) : null,
            hintText:  widget.hintText,
            suffixIcon: widget.sufixIcon != null ? InkWell(
              onTap: widget.onSufixtap as void Function()?,
              child: Icon(
                widget.sufixIcon,
              ),
            ) : null,
            hintStyle: Theme.of(context).textTheme.bodyMedium!.copyWith(
              fontWeight: FontWeight.normal
            ),
            focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Theme.of(context).primaryColor,
                width: 1.0
              ),
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Theme.of(context).primaryColor,
                width: 1.0
              ),
            ),
            disabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Theme.of(context).primaryColor,
                width: 1.0
              ),
            ),
            errorBorder:  OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Theme.of(context).colorScheme.error,
                width: 1.0
              ),
            ),
            focusedErrorBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
              borderSide: BorderSide(
                color: Theme.of(context).colorScheme.error,
                width: 1.0
              ),
            ),
          ) 
        )
      ),
    );
  }
}