// 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';

extension GradientIconExtension on Icon {
  GradientIcon toGradient(Gradient? gradient) =>
      GradientIcon.from(this, gradient);
}

class GradientIcon extends Icon {
  const GradientIcon(
    super.icon, {
    this.gradient,
    super.key,
    super.size,
    super.fill,
    super.weight,
    super.grade,
    super.opticalSize,
    super.color,
    super.shadows,
    super.semanticLabel,
    super.textDirection,
  });

  factory GradientIcon.from(Icon icon, Gradient? gradient) => GradientIcon(
        icon.icon,
        gradient: gradient,
        key: icon.key,
        size: icon.size,
        fill: icon.fill,
        weight: icon.weight,
        grade: icon.grade,
        opticalSize: icon.opticalSize,
        color: icon.color,
        shadows: icon.shadows,
        semanticLabel: icon.semanticLabel,
        textDirection: icon.textDirection,
      );

  final Gradient? gradient;

  @override
  Widget build(BuildContext context) {
    if (gradient != null) {
      return ShaderMask(
        blendMode: BlendMode.srcIn,
        shaderCallback: (bounds) => gradient!.createShader(
          Rect.fromLTWH(0, 0, bounds.width, bounds.height),
        ),
        child: super.build(context),
      );
    } else {
      return super.build(context);
    }
  }
}
