// 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 SymbolButtonThemeResolver extends ThemeResolver<SymbolButtonStyle,
    SymbolButtonThemeExtension, ButtonState> {
  const SymbolButtonThemeResolver({
    required this.customStyleFn,
  });

  @override
  SymbolButtonStyle computeDefaultValue(
    BuildContext context, {
    ButtonState? extra,
  }) {
    MultiColor backgroundColor = MultiColor.single(context.colorScheme.primary);
    MultiColor foregroundColor =
        MultiColor.single(context.colorScheme.onPrimary);

    switch (extra?.state) {
      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 SymbolButtonStyle(
      label: context.textTheme.labelLarge,
      dimension: context.buttonTheme.height * 1.5,
      radius: (context.buttonTheme.shape is RoundedRectangleBorder)
          ? (context.buttonTheme.shape as RoundedRectangleBorder).borderRadius
          : null,
      padding: context.buttonTheme.padding,
      foregroundColors: foregroundColor,
      backgroundColors: backgroundColor,
    );
  }

  @override
  final SymbolButtonStyle? Function(
    BuildContext context, {
    ButtonState? extra,
  }) customStyleFn;

  @override
  SymbolButtonStyle? computeExtensionValueFn(
    BuildContext context,
    SymbolButtonThemeExtension? themeExtension, {
    ButtonState? extra,
  }) {
    SymbolButtonStyle? style;
    switch (extra?.state) {
      case ControlState.disabled:
        style = themeExtension?.disabledStyle;
        break;
      case ControlState.focused:
        style = themeExtension?.focusedStyle;
        break;
      case ControlState.hovered:
        style = themeExtension?.hoveredStyle;
        break;
      case ControlState.tapped:
        style = themeExtension?.tappedStyle;
        break;
      case ControlState.normal:
      case null:
        style = themeExtension?.normalStyle;
        break;
    }
    if (extra?.isSelected ?? false) {
      style = themeExtension?.selectedStyle;
    }

    return style;
  }
}
