import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class ROutlinedElevated extends StatefulWidget {
  ROutlinedElevated({
    super.key,
    this.text,
    this.onPressed,
    this.foregroundColor,
    this.backgroundColor,
    this.borderColor,
    this.disabledBackgroundColor,
    this.pressedBackgroundColor,
    this.radius,
    this.textStyle,
    this.child,
    this.width,
    this.height,
    this.enabled = true,
  }) : assert(text != null || child != null, 'Either text or child must be provided');

  final String? text;
  final VoidCallback? onPressed;
  final double? width;
  final double? height;
  Color? foregroundColor;
  Color? backgroundColor;
  Color? borderColor;
  Color? disabledBackgroundColor;
  Color? pressedBackgroundColor;
  double? radius;
  TextStyle? textStyle;
  Widget? child;
  bool enabled;

  @override
  State<ROutlinedElevated> createState() => _ROutlinedElevatedState();
}

class _ROutlinedElevatedState extends State<ROutlinedElevated> {
  final WidgetStatesController _statesController = WidgetStatesController();
  final GlobalKey _widgetKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _getWidgetHeight();
    });
  }

  void _getWidgetHeight() {
    final RenderBox? renderBox = _widgetKey.currentContext?.findRenderObject() as RenderBox?;
    if (renderBox != null) {
      final height = renderBox.size.height;
      debugPrint('ارتفاع ویجت بعد از رندر شدن: $height');
    } else {
      debugPrint('ویجت هنوز رندر نشده است');
    }
  }

  @override
  void dispose() {
    _statesController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints.tightFor(
        width: widget.width ?? 150.w,
        height: widget.height ?? 40.h,
      ),
      child: OutlinedButton(
        key: _widgetKey,
        statesController: _statesController,
        onPressed: widget.enabled ? widget.onPressed : null,
        style: ButtonStyle(
          side: WidgetStateProperty.resolveWith<BorderSide?>((states) {
            if (states.contains(WidgetState.pressed)) {
              return BorderSide(color: widget.borderColor ?? AppColor.blueNormal, width: 2);
            } else if (states.contains(WidgetState.disabled)) {
              return BorderSide(
                color: widget.borderColor?.disabledColor ?? AppColor.blueNormal.withAlpha(38),
                width: 2,
              );
            }
            return BorderSide(color: widget.borderColor ?? AppColor.blueNormal, width: 2);
          }),
          backgroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
            if (states.contains(WidgetState.pressed)) {
              if (widget.pressedBackgroundColor != null) {
                return widget.pressedBackgroundColor;
              }
              return widget.backgroundColor?.pressedColor ?? widget.borderColor?.pressedColor;
            } else if (states.contains(WidgetState.hovered)) {
              return widget.backgroundColor?.hoverColor ?? AppColor.blueNormal.hoverColor;
            } else if (states.contains(WidgetState.disabled)) {
              return widget.backgroundColor?.disabledColor ?? Colors.transparent;
            }
            return widget.backgroundColor;
          }),
          foregroundColor: WidgetStateProperty.resolveWith<Color?>((states) {
            if (states.contains(WidgetState.pressed)) {
              return Colors.white;
            } else if (states.contains(WidgetState.disabled)) {
              return widget.foregroundColor?.disabledColor ??
                  widget.borderColor?.disabledColor ??
                  AppColor.blueNormal.disabledColor;
            }
            return widget.foregroundColor ?? widget.borderColor ?? AppColor.blueNormal;
          }),
          shape: WidgetStatePropertyAll(
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(widget.radius ?? 8)),
          ),
          fixedSize: WidgetStatePropertyAll(Size(widget.width ?? 150.w, widget.height ?? 56.h)),
          padding: WidgetStatePropertyAll(EdgeInsets.zero),
          textStyle: WidgetStateProperty.resolveWith<TextStyle?>((states) {
            if (states.contains(WidgetState.pressed)) {
              return widget.textStyle?.copyWith(color: Colors.white) ??
                  AppFonts.yekan18.copyWith(color: Colors.white);
            }
            return widget.textStyle ?? AppFonts.yekan18.copyWith(color: AppColor.blueNormal);
          }),
        ),
        child: widget.child ?? Text(widget.text ?? ''),
      ),
    );
  }
}
