import 'package:flutter/material.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

import 'custom_standard_button_theme.dart';

/// Created by Haoyi on 2023/6/1

class CustomStandardButtonDecoration extends Decoration {
  final bool pressed;
  final GuruButtonStyle style;

  const CustomStandardButtonDecoration(this.style, this.pressed);

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return CustomStandardButtonPainter(style, pressed);
  }
}

class CustomStandardButtonPainter extends BoxPainter {
  final Paint bottomPaint;
  final Paint middlePaint;
  final Paint topPaint;
  final GuruButtonStyle style;
  final bool pressed;

  static final styleIdxMapping = {
    GuruButtonStyle.positive: 0,
    GuruButtonStyle.neutral: 1,
    GuruButtonStyle.negative: 2,
    CustomStandardButtonTheme.custom: 3
  };

  static const bottomColors = [
    Color(0xFF05753E),
    Color(0xFF4C4C4C),
    Color(0xFFAC2929),
    Color(0xFFAC2929),  
  ];

  static const middleColors = [Color(0xFF4CE69B), Color(0xFF858585), Color(0xFFEA7777), Color.fromARGB(255, 245, 162, 162),];

  static const topGradients = [
    LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFF04D56E), Color(0xFF08A658)]),
    LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFF787878), Color(0xFF5B5B5B)]),
    LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFEF5B5B), Color(0xFFEC3B3B)]),
    LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [Color(0xFFEF5B5B), Color.fromARGB(255, 53, 217, 50)])
  ];

  CustomStandardButtonPainter(this.style, this.pressed)
      : topPaint = Paint()..isAntiAlias = true,
        middlePaint = Paint()..isAntiAlias = true,
        bottomPaint = Paint()..isAntiAlias = true;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final size = configuration.size!;
    final top = size.height * 0.01973684;
    final bottom = size.height * 0.03289474;
    final bottomRect = Rect.fromLTWH(0, top, size.width, size.height).shift(offset);
    final middleRect = Rect.fromLTWH(0, 0, size.width, size.height - bottom).shift(offset);
    final topRect = Rect.fromLTWH(0, top, size.width, size.height - bottom).shift(offset);
    final idx = styleIdxMapping[style] ?? 0;
    final radius = Radius.circular(size.height * 0.5);
    bottomPaint.color = bottomColors[idx];
    canvas.drawRRect(RRect.fromRectAndRadius(bottomRect, radius), bottomPaint);

    middlePaint.color = pressed ? bottomColors[idx] : middleColors[idx];
    canvas.drawRRect(RRect.fromRectAndRadius(middleRect, radius), middlePaint);

    if (!pressed) {
      topPaint.shader = topGradients[idx].createShader(topRect);
      canvas.drawRRect(RRect.fromRectAndRadius(topRect, radius), topPaint);
    }
  }
}
