// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:flutter/material.dart';
import 'package:wyatt_ui_kit/src/components/gradients/gradient_box_border.dart';

class CardWrapper extends StatefulWidget {
  const CardWrapper({
    required this.child,
    required this.backgroundColor,
    required this.borderColors,
    required this.shadow,
    required this.minSize,
    required this.maxSize,
    required this.padding,
    required this.background,
    super.key,
  });

  final Widget? background;
  final Widget child;
  final Color? backgroundColor;
  final List<Color>? borderColors;
  final BoxShadow? shadow;
  final Size? minSize;
  final Size? maxSize;
  final double? padding;

  @override
  State<CardWrapper> createState() => _CardWrapperState();
}

class _CardWrapperState extends State<CardWrapper> {
  Size _cardSize = Size.zero;
  final GlobalKey _key = GlobalKey();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final RenderObject? renderBox = _key.currentContext?.findRenderObject();
      if (renderBox != null) {
        setState(() {
          _cardSize =
              Size(renderBox.paintBounds.width, renderBox.paintBounds.height);
        });
      }
    });
  }

  Widget _buildChild(Widget child) => (widget.background != null)
      ? Stack(
          alignment: Alignment.center,
          children: [
            SizedBox(
              width: _cardSize.width,
              height: _cardSize.height,
              child: Center(child: widget.background),
            ),
            Padding(
              padding: EdgeInsets.all(
                widget.padding ?? 25,
              ),
              child: child,
            ),
          ],
        )
      : Padding(
          padding: EdgeInsets.all(
            widget.padding ?? 25,
          ),
          child: child,
        );

  @override
  Widget build(BuildContext context) => SelectionArea(
        child: DecoratedBox(
          key: _key,
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(12)),
            color: widget.backgroundColor ?? Theme.of(context).cardColor,
            border: GradientBoxBorder(
              gradient: LinearGradient(
                colors: (widget.borderColors != null &&
                        widget.borderColors!.length >= 2)
                    ? widget.borderColors!
                    : [
                        Theme.of(context).cardColor,
                        Theme.of(context).cardColor
                      ],
              ),
              width: 1,
            ),
            boxShadow: [
              if (widget.shadow != null) ...[widget.shadow!]
            ],
          ),
          child: (widget.minSize != null && widget.maxSize != null)
              ? ConstrainedBox(
                  constraints: BoxConstraints(
                    minWidth: widget.minSize!.width,
                    minHeight: widget.minSize!.height,
                    maxWidth: widget.maxSize!.width,
                    maxHeight: widget.maxSize!.height,
                  ),
                  child: _buildChild(widget.child),
                )
              : _buildChild(widget.child),
        ),
      );
}
