import 'dart:ui';

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

class CustomAssetBarDecoration extends Decoration {
  const CustomAssetBarDecoration();

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return CustomAssetBarPainter(GuruThemeDesignSpec.get().assetBarS1DesignSpec);
  }
}

class CustomAssetBarPainter extends BoxPainter {
  final Paint painter = Paint()..isAntiAlias = true;
  final GuruAssetBarS1DesignSpec designSpec;
  final double _dp1;

  CustomAssetBarPainter(this.designSpec)
      : _dp1 = designSpec.measuredMetrics.measureHeight(1);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final dp2 = _dp1 + _dp1;
    final size = configuration.size!;
    final Offset center = size.center(offset);

    final boundary = Rect.fromCenter(
        center: center, width: size.width, height: designSpec.barHeight);

    final bg1 = RRect.fromLTRBR(boundary.left, boundary.top + _dp1,
        boundary.right, boundary.bottom, Radius.circular(size.height / 2));

    // 低出2px
    final bg2 = RRect.fromLTRBR(boundary.left, boundary.top, boundary.right,
        boundary.bottom - dp2, Radius.circular(size.height / 2));

    final bg3 = RRect.fromLTRBR(
        boundary.left,
        boundary.top + _dp1,
        boundary.right,
        boundary.bottom - dp2,
        Radius.circular(size.height / 2));

    painter.color = const Color(0xFF1A1A20);
    canvas.drawRRect(bg1, painter);
    painter.color = const Color(0xFF3C3C4A);
    canvas.drawRRect(bg2, painter);
    painter.color = const Color(0xFF26262F);
    canvas.drawRRect(bg3, painter);
  }
}
