// Wyatt Studio Copyright (c) 2023

// 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/widgets.dart';
import 'package:wyatt_ui_components/wyatt_ui_components.dart';
import 'package:wyatt_ui_kit/wyatt_ui_kit.dart';

extension GradientTextExtension on Text {
  /// If this text contains a [GradientTextStyle] it simply transforms it in
  /// [GradientText], if not it needs a [MultiColor].
  GradientText toGradient({MultiColor? gradientColors}) {
    if (style is GradientTextStyle?) {
      // Gradient
      final gradientStyle = (style as GradientTextStyle?)?.gradientColors;
      return GradientText.from(this, gradientStyle ?? gradientColors);
    }

    return GradientText.from(this, gradientColors);
  }

  GradientText toFlutterGradient(Gradient? gradient) =>
      GradientText.from(this, MultiColor(gradient?.colors));
}

class GradientText extends Text {
  const GradientText(
    super.data, {
    super.style,
    super.strutStyle,
    super.textAlign,
    super.textDirection,
    super.locale,
    super.softWrap,
    super.overflow,
    super.textScaleFactor,
    super.maxLines,
    super.semanticsLabel,
    super.textWidthBasis,
    super.textHeightBehavior,
    super.selectionColor,
    super.key,
  });

  factory GradientText.from(Text text, MultiColor? gradientColors) =>
      GradientText(
        text.data ?? '',
        style: GradientTextStyle.from(text.style, gradientColors),
        strutStyle: text.strutStyle,
        textAlign: text.textAlign,
        textDirection: text.textDirection,
        locale: text.locale,
        softWrap: text.softWrap,
        overflow: text.overflow,
        textScaleFactor: text.textScaleFactor,
        maxLines: text.maxLines,
        semanticsLabel: text.semanticsLabel,
        textWidthBasis: text.textWidthBasis,
        textHeightBehavior: text.textHeightBehavior,
        selectionColor: text.selectionColor,
        key: text.key,
      );

  @override
  Widget build(BuildContext context) {
    if (style is GradientTextStyle?) {
      // Gradient
      final gradientStyle = (style as GradientTextStyle?)?.gradientColors;
      final gradient = (gradientStyle?.isGradient ?? false)
          ? LinearGradientHelper.fromMultiColor(gradientStyle!)
          : null;
      if (gradient != null) {
        return ShaderMask(
          blendMode: BlendMode.srcIn,
          shaderCallback: (bounds) => gradient.createShader(
            Rect.fromLTWH(0, 0, bounds.width, bounds.height),
          ),
          child: super.build(context),
        );
      }
    }
    return super.build(context);
  }
}
