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

import '../../constant/colors.dart';
import '../../constant/style.dart';

class MyTextForm extends StatelessWidget {
  const MyTextForm({
    super.key,
    required this.controller,
    required this.label,
    required this.hint,
    required this.type,
    this.prefixIcon,
  });
  
  final TextEditingController controller;
  final String label, hint;
  final TextInputType type;
  final IconData? prefixIcon;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: TextFormField(
        keyboardType: type,
        style: AppStyle.body,
        cursorColor: AppColor.accent,
        controller: controller,
        decoration: InputDecoration(
          filled: true,
          fillColor: AppColor.surface,
          prefixIcon: prefixIcon != null 
            ? Icon(prefixIcon, color: AppColor.textSecondary, size: 20) 
            : null,
          contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
          enabledBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: AppColor.divider),
            borderRadius: BorderRadius.circular(16),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: AppColor.accent, width: 2),
            borderRadius: BorderRadius.circular(16),
          ),
          errorBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: AppColor.danger),
            borderRadius: BorderRadius.circular(16),
          ),
          focusedErrorBorder: OutlineInputBorder(
            borderSide: const BorderSide(color: AppColor.danger, width: 2),
            borderRadius: BorderRadius.circular(16),
          ),
          labelText: label.tr,
          labelStyle: AppStyle.subtitle,
          hintText: hint.tr,
          hintStyle: AppStyle.caption,
        ),
        validator: (value) {
          if (value == null || value.isEmpty) {
            return '${'Please enter'.tr} ${label.tr}';
          }

          if (type == TextInputType.emailAddress) {
            if (!GetUtils.isEmail(value)) {
              return 'Please enter a valid email.'.tr;
            }
          }

          return null;
        },
      ),
    );
  }
}

