import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

import '../../../core/constants/assets.dart';
import '../../../core/theme/colors.dart';
import '../../../core/utils/size_config.dart';

class LoginTextField extends StatelessWidget {
  final TextEditingController controller;
  final FocusNode focusNode;
  final String label;
  final String? iconPath;
  final bool isPassword;
  final bool obscureText;
  final double? borderRedus;
  final double? fieldHeight;
  final double? prefixIconPadding;
  final VoidCallback? onToggleVisibility;

  const LoginTextField({
    required this.controller,
    required this.focusNode,
    required this.label,
    this.iconPath,
    super.key,
    this.isPassword = false,
    this.obscureText = false,
    this.fieldHeight,
    this.onToggleVisibility,
    this.borderRedus,
    this.prefixIconPadding,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: fieldHeight ?? 55.0.h(),
      child: TextField(
        controller: controller,
        focusNode: focusNode,
        obscureText: isPassword && obscureText,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.symmetric(
            vertical: (fieldHeight != null ? (fieldHeight! / 2.8) : 16.0).h(),
            horizontal: 12.0.w(),
          ),
          labelText: label,
          prefixIcon:
              iconPath != null
                  ? Padding(
                    padding: EdgeInsets.all(prefixIconPadding ?? 12.0.w()),
                    child: SvgPicture.asset(
                      iconPath!,
                      width: 24.0.w(),
                      height: 24.0.h(),
                      colorFilter: ColorFilter.mode(
                        focusNode.hasFocus
                            ? AppColors.grey80
                            : AppColors.grey60,
                        BlendMode.srcIn,
                      ),
                    ),
                  )
                  : null,
          suffixIcon:
              isPassword
                  ? IconButton(
                    onPressed: onToggleVisibility,
                    icon: SvgPicture.asset(
                      obscureText
                          ? AssetsManager.passwordVisibility
                          : AssetsManager.passwordVisibilityOff,
                      width: 24.0.w(),
                      height: 24.0.h(),
                      colorFilter: ColorFilter.mode(
                        focusNode.hasFocus
                            ? AppColors.grey80
                            : AppColors.grey60,
                        BlendMode.srcIn,
                      ),
                    ),
                  )
                  : null,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(borderRedus ?? 12.0.w()),
            borderSide: BorderSide(color: AppColors.grey40),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(borderRedus ?? 12.0.w()),
            borderSide: BorderSide(color: AppColors.grey40),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: AppColors.borderBlue),
            borderRadius: BorderRadius.circular(borderRedus ?? 12.0.w()),
          ),
        ),
      ),
    );
  }
}
