// 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/wyatt_ui_kit.dart';

class FlatButtonThemeResolver extends ThemeResolver<FlatButtonStyle,
    FlatButtonThemeExtension, ControlState> {
  const FlatButtonThemeResolver({
    required this.customStyleFn,
  });

  /// Values taken from <https://api.flutter.dev/flutter/material/ElevatedButton/defaultStyleOf.html>
  @override
  FlatButtonStyle computeDefaultValue(
    BuildContext context, {
    ControlState? extra,
  }) {
    MultiColor backgroundColor = MultiColor.single(context.colorScheme.primary);
    MultiColor foregroundColor =
        MultiColor.single(context.colorScheme.onPrimary);

    switch (extra) {
      case ControlState.disabled:
        backgroundColor =
            MultiColor.single(context.colorScheme.onSurface.withOpacity(0.12));
        foregroundColor =
            MultiColor.single(context.colorScheme.onSurface.withOpacity(0.38));
        break;
      case ControlState.hovered:
        backgroundColor =
            MultiColor.single(context.colorScheme.primary.withOpacity(0.92));
        break;
      case ControlState.tapped:
        backgroundColor =
            MultiColor.single(context.colorScheme.primary.withOpacity(0.92));
        break;
      case null:
      case ControlState.normal:
      case ControlState.focused:
        break;
    }

    return FlatButtonStyle(
      label:
          context.textTheme.labelLarge?.copyWith(color: foregroundColor.color),
      radius: (context.buttonTheme.shape is RoundedRectangleBorder)
          ? (context.buttonTheme.shape as RoundedRectangleBorder).borderRadius
          : null,
      padding: context.buttonTheme.padding,
      foregroundColors: foregroundColor,
      backgroundColors: backgroundColor,
    );
  }

  @override
  final FlatButtonStyle? Function(
    BuildContext context, {
    ControlState? extra,
  }) customStyleFn;

  @override
  FlatButtonStyle? computeExtensionValueFn(
    BuildContext context,
    FlatButtonThemeExtension? themeExtension, {
    ControlState? extra,
  }) {
    switch (extra) {
      case ControlState.disabled:
        return themeExtension?.disabledStyle;
      case ControlState.focused:
        return themeExtension?.focusedStyle;
      case ControlState.hovered:
        return themeExtension?.hoveredStyle;
      case ControlState.tapped:
        return themeExtension?.tappedStyle;
      case ControlState.normal:
      case null:
        return themeExtension?.normalStyle;
    }
  }
}
