import 'dart:math';

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

abstract class SkBaseFieldWidget extends StatelessWidget {
  final FocusNode focusNode = FocusNode();
  late final SkBaseFieldController baseFieldController;
  final bool customLabel;
  final String? labelText;
  final Color? fillColor;
  SkBaseFieldWidget({
    super.key,
    this.customLabel = false,
    this.labelText,
    this.fillColor,
    autoFocus = false,
  }) {
    baseFieldController = Get.put(
      SkBaseFieldController(autoFocus: autoFocus, focusNode: focusNode),
      tag: '${labelText ?? Random().nextInt(1000)}',
    );
  }
}

class SkBaseFieldController extends GetxController {
  RxBool isFocus = false.obs;
  final bool autoFocus;
  FocusNode? focusNode;
  SkBaseFieldController({required this.autoFocus, this.focusNode});

  @override
  void onReady() {
    if (autoFocus) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        focusNode?.requestFocus();
      });
    }
    focusNode?.addListener(() {
      isFocus.value = focusNode?.hasFocus ?? false;
    });
    super.onReady();
  }
}
