import 'package:adaptix/adaptix.dart';
import 'package:flutter/material.dart';

import '../../themes/app_theme.dart';

class CustomTextField extends StatefulWidget {
  CustomTextField({
    Key? key,
    required this.labelText,
    required this.controller,
    required this.hintText,
    required this.obscureText,
    required this.inputType,
    required this.isPassword,
  }) : super(key: key);

  final String labelText, hintText;
  final TextEditingController controller;
  bool obscureText, isPassword;
  final TextInputType inputType;

  @override
  State<CustomTextField> createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  void _toggle() {
    setState(() {
      widget.obscureText = !widget.obscureText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: widget.controller,
      obscureText: widget.obscureText,
      keyboardType: widget.inputType,
      cursorColor: AppTheme.lightPrimaryColor,
      decoration: InputDecoration(
        labelText: widget.labelText,
        hintText: widget.hintText,
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10.adaptedPx()),
          borderSide: BorderSide(
            color: AppTheme.lightPrimaryColor,
            width: 3.adaptedPx(),
          ),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(10.adaptedPx()),
          borderSide: BorderSide(
            color: AppTheme.redColor,
            width: 3.adaptedPx(),
          ),
        ),
        labelStyle: Theme.of(context).primaryTextTheme.bodyMedium?.copyWith(
              color: AppTheme.lightTextColor,
              fontSize: 13.adaptedPx(),
            ),
        suffixIcon: widget.isPassword
            ? IconButton(
                onPressed: () {
                  setState(() {
                    _toggle();
                  });
                },
                icon: Icon(
                  widget.obscureText
                      ? Icons.visibility_outlined
                      : Icons.visibility_off_outlined,
                ),
                color: AppTheme.lightTextColor,
              )
            : IconButton(
                onPressed: () {},
                icon: Container(),
              ),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(5.adaptedPx()),
          borderSide: BorderSide(
            color: AppTheme.lightTextColor,
            width: 2.adaptedPx(),
          ),
        ),
      ),
      validator: (value) {
        if (value == null || value.length < 8) {
          return 'password_length_8';
        }
        return null;
      },
    );
  }
}
