import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';
import 'package:tacban/assets/theme/colors.dart';

class CodeInputController extends StatefulWidget {
  final TextEditingController controller;
  final ValueChanged<String>? onCompleted;
  const CodeInputController({super.key, this.onCompleted, required this.controller});

  @override
  State<CodeInputController> createState() => _CodeInputControllerState();
}

class _CodeInputControllerState extends State<CodeInputController> {

  @override
  Widget build(BuildContext context) {
    final focusedBorderColor = Theme.of(context).primaryColor;
    final defaultPinTheme = PinTheme(
      width: 60,
      height: 60,
      textStyle: TextStyle(
        fontSize: 22,
        color: AppColorLight.text01Color,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(50),
        border: Border.all(width: 1, color: AppColorLight.text04Color),
      ),
    );


    return  SizedBox(
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Pinput(
          controller: widget.controller,
          keyboardType: TextInputType.none,
          length: 5,
          androidSmsAutofillMethod: AndroidSmsAutofillMethod.smsUserConsentApi,
          listenForMultipleSmsOnAndroid: true,
          defaultPinTheme: defaultPinTheme,
          onClipboardFound: (value) {
            widget.controller.setText(value);
          },
          hapticFeedbackType: HapticFeedbackType.lightImpact,
          onCompleted: widget.onCompleted,
          cursor: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Container(
                margin: const EdgeInsets.only(bottom: 9),
                width: 22,
                height: 1,
                color: focusedBorderColor,
              )
            ]
          ),
          focusedPinTheme: defaultPinTheme.copyWith(
            decoration: defaultPinTheme.decoration!.copyWith(
              border: Border.all(color: focusedBorderColor),
            ),
          ),
          submittedPinTheme: defaultPinTheme.copyWith(
            decoration: defaultPinTheme.decoration!.copyWith(
              // color: fillColor,
              border: Border.all(color: focusedBorderColor),
            ),
          ),
          errorPinTheme: defaultPinTheme.copyBorderWith(
            border: Border.all(color: Colors.redAccent),
          )
        )
      )
    );
  }
}