import 'package:bestapp_package/bestapp_package.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:tacban/src/shared/utils/enums.dart';
import 'package:tacban/src/shared/utils/input_utils.dart';
import 'package:tacban/src/shared/widgets/inputs/theme/input_theme.dart';
import 'package:tacban/src/shared/widgets/text/text_error.dart';
import '../../../../assets/theme/colors.dart';



enum TypInputText {
  DEFAULT,
  EMAIL,
  STYLE_MOBILE,
  RAZAO_SOCIAL
}

class SimpleInput extends StatefulWidget {
  final TextEditingController? controller;
  final Widget? prefix;
  final String? title;
  final bool? enabled;
  final bool? validator;
  final TypInputText? typeInput;
  final String? hintText;
  final VoidCallback? onTap;
  final ValueChanged<String>? onFieldSubmitted;
  final SizeInput? size;

  const SimpleInput({
    super.key, 
    this.hintText,
    this.prefix, 
    this.onTap,
    this.size=SizeInput.lg, 
    this.onFieldSubmitted, 
    this.typeInput=TypInputText.DEFAULT,  
    this.validator, 
    this.title, 
    this.enabled=true, 
    this.controller
  });

  @override
  State<SimpleInput> createState() => _SimpleInputState();
}

class _SimpleInputState extends State<SimpleInput> {

  StatusTextField statusValided = StatusTextField.valided;

  bool isEmail(String input) => EmailValidator.validate(input);
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          height: InputUtils.defineSize(widget.size),
          padding: const EdgeInsets.symmetric(horizontal: 15),
          decoration: BoxDecoration(
            border: Border.all(
              width: 1,
              color: InputUtils.getColorBorder(typInputText: widget.typeInput, statusValided: statusValided, enabled: widget.enabled!)
              // color: widget.enabled == false ? AppColorLight.text05Color : isvalidate ? AppColorLight.text04Color : AppColorLight.erroColor
            ),
            color: widget.enabled == false ? AppColorLight.text05Color : null,
            borderRadius: BorderRadius.circular(10)
          ),
          child: TextFormField(
            controller: widget.controller,
            autofocus: false,
            enabled: widget.enabled,
            validator: (value) {
              setState(() => statusValided = StatusTextField.valided);
              if(widget.validator == false)return null;
              
              if (value!.isEmpty) {
                setState(() => statusValided = StatusTextField.errorFieldRequired);
                return '';
              }
              if (!isEmail(value.trim()) && widget.typeInput==TypInputText.EMAIL) {
                setState(() => statusValided = StatusTextField.emailIsInvalid);
                return '';
              }
              return null;
            },
            style: Theme.of(context).textTheme.bodyLarge?.copyWith(
              fontSize: AppFontSize.fontSize15,
              height: 1.5,
              fontWeight: FontWeight.w600,
              color: InputUtils.getColorText(typInputText: widget.typeInput, enabled: widget.enabled!)
            ),
            cursorHeight: AppFontSize.cursorHeight,
            onChanged: (value) {
              if (!isEmail(value.trim()) && widget.typeInput==TypInputText.EMAIL) {
                setState(() => statusValided = StatusTextField.emailIsInvalid);
              }else{
                setState(() => statusValided = StatusTextField.valided);
              }
            },
            onFieldSubmitted: widget.onFieldSubmitted,
            textInputAction: kIsWeb ? TextInputAction.none : TextInputAction.done,
            decoration: inputThemeDecoration(
              context,
              statusTextField: statusValided,
              hintText: widget.hintText,
              title: widget.title,
              prefix: widget.prefix
            )
          )
        ),
        if(statusValided != StatusTextField.valided)
          TextErrorMessageWidget(statusTextField: statusValided)
      ],
    );
  }

  // Color _getColorText() {
  //   if(widget.typeInput == TypInputText.STYLE_MOBILE && widget.enabled == false){
  //     return AppColorLight.text03Color;
  //   }
  //   return AppColorLight.text01Color;
  // }

  // Color _getColorBorder(){
  //   if(statusValided != StatusTextField.valided) return AppColorLight.erroColor;

  //   if(widget.enabled == false && widget.typeInput == TypInputText.STYLE_MOBILE){
  //     return AppColorLight.text04Color;
  //   }
  //   if(widget.enabled == true){
  //     return AppColorLight.text04Color;
  //   }
  //   return AppColorLight.text05Color;
  // }

  // double defineSize(SizeInput? size){
  //   if(size == SizeInput.lg) return 56;
  //   return 42;
  // }
}