// 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_components/wyatt_ui_components.dart';
import 'package:wyatt_ui_kit/src/components/cards/widgets/card_background.dart';
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';

class CardWrapper extends StatelessWidget {
  CardWrapper({
    required this.child,
    required this.backgroundColors,
    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 MultiColor? backgroundColors;
  final MultiColor? borderColors;
  final BoxShadow? shadow;
  final Size? minSize;
  final Size? maxSize;
  final double? padding;

  final GlobalKey _key = GlobalKey();

  Widget _buildChild(Widget child) => (background != null)
      ? Stack(
          alignment: Alignment.center,
          children: [
            CardBackground(
              cardKey: _key,
              background: background,
            ),
            Padding(
              padding: EdgeInsets.all(
                padding ?? 25,
              ),
              child: child,
            ),
          ],
        )
      : Padding(
          padding: EdgeInsets.all(
            padding ?? 25,
          ),
          child: child,
        );

  List<BoxShadow> _shadow(BuildContext context) {
    final themeShadow = ThemeHelper.getThemeElement<BoxShadow, BoxShadow>(
      [
        shadow,
        Theme.of(context).extension<CardThemeExtension>()?.shadowColor,
      ],
      valueValidator: (shadow) => shadow != null,
      transform: (shadow) => shadow,
    );
    return (themeShadow != null) ? [themeShadow] : [];
  }

  @override
  Widget build(BuildContext context) => SelectionArea(
        child: DecoratedBox(
          key: _key,
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(12)),
            gradient: ThemeHelper.getThemeElement<MultiColor, Gradient>(
              [
                backgroundColors,
                Theme.of(context)
                    .extension<CardThemeExtension>()
                    ?.backgroundColors,
              ],
              valueValidator: (multiColor) => multiColor?.isGradient,
              transform: (multiColor) =>
                  LinearGradientHelper.fromMultiColor(multiColor!),
            ),
            color: ThemeHelper.getThemeElement<MultiColor, Color>(
              [
                backgroundColors,
                Theme.of(context)
                    .extension<CardThemeExtension>()
                    ?.backgroundColors,
                MultiColor.single(Theme.of(context).cardColor),
              ],
              valueValidator: (multiColor) =>
                  multiColor != null && multiColor.isColor,
              transform: (multiColor) => multiColor?.color,
            ),
            border: ThemeHelper.getThemeElement<MultiColor, BoxBorder>(
              [
                borderColors,
                Theme.of(context).extension<CardThemeExtension>()?.borderColors,
                Theme.of(context)
                    .extension<CardThemeExtension>()
                    ?.backgroundColors,
              ],
              valueValidator: (multiColor) =>
                  multiColor != null && multiColor.isColor,
              transform: (multiColor) {
                if (multiColor!.isGradient) {
                  return GradientBoxBorder(
                    gradient: LinearGradientHelper.fromMultiColor(multiColor),
                  );
                }
                return Border.all(
                  color: multiColor.color,
                );
              },
            ),
            boxShadow: _shadow(context),
          ),
          child: (minSize != null && maxSize != null)
              ? ConstrainedBox(
                  constraints: BoxConstraints(
                    minWidth: minSize!.width,
                    minHeight: minSize!.height,
                    maxWidth: maxSize!.width,
                    maxHeight: maxSize!.height,
                  ),
                  child: _buildChild(child),
                )
              : _buildChild(child),
        ),
      );
}
