import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:guru_utils/visual_feast/visual_feast_animation.dart';
import 'package:guru_utils/visual_feast/engine/visual_feast_engine.dart';
import 'package:guru_widgets/assetbar/guru_asset_bar.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

/// Created by Haoyi on 2022/7/5

class AssetBackground extends VisualFeastRender {
  // @override
  // String get layerName => "Background";

  // @override
  // int get orderInLayer => 1;
  final GlobalKey barKey;
  final GuruAssetBarSizeSpec barSize;
  final ui.Image assetIcon;

  late Rect boundary;

  final Paint paint = Paint()..color = Colors.cyan.withOpacity(0.3);

  @override
  String get renderId => "background";


  AssetBackground(this.barSize, this.barKey, this.assetIcon);

  @override
  void onAttach() {
    setOpacity(0.8);
  }

  @override
  void onDetach() {
    setOpacity(0);
    engine.detachRenders([this]);
    super.onDetach();
  }

  void hideBackground() {
    final animation = VisualFeastAnimation(
        from: 0.8,
        to: 0,
        delayed: const Duration(milliseconds: 500),
        duration: const Duration(milliseconds: 150),
        onUpdate: (double value, double percent, int elapseInMillis) {
          setOpacity(value);
        }
    );
    attachAnimation(animation);
  }

  @override
  void onRender(Canvas canvas) {
    super.onRender(canvas);
    canvas.save();
    RenderObject? widgetRenderBox = barKey.currentContext?.findRenderObject();
    final GuruAssetBarS1DesignSpec designSpecS1 = GuruThemeDesignSpec.get().assetBarS1DesignSpec;
    final GuruAssetBarS2DesignSpec designSpecS2 = GuruThemeDesignSpec.get().assetBarS2DesignSpec;
    if (widgetRenderBox != null) {
      Offset widgetOffset = (widgetRenderBox as RenderBox).localToGlobal(Offset.zero);
      Size size = widgetRenderBox.size;
      Path path = Path();
      path.fillType = PathFillType.evenOdd;
      path.addRect(Rect.largest);
      if (barSize == GuruAssetBarSizeSpec.s1) {
        RRect rrect = RRect.fromRectAndRadius(Rect.fromLTWH(widgetOffset.dx, widgetOffset.dy + designSpecS1.assetIconTopSpcing, size.width, designSpecS1.barHeight), Radius.circular(designSpecS1.barHeight / 2));
        path.addRRect(rrect);
      } else {
        final double offsetTop = (designSpecS2.measuredSize.height - designSpecS2.barHeight) / 2;
        RRect rrect = RRect.fromRectAndRadius(Rect.fromLTWH(10, offsetTop, (size.width - 11), designSpecS2.barHeight), Radius.circular(designSpecS2.barHeight / 2));
        final path1 = Path();
        path1.addRRect(rrect);
        path.addPath(path1, widgetOffset);
      }
      canvas.clipPath(path);
    }
    canvas.drawColor(Colors.black.withOpacity(opacity), BlendMode.srcOver);
    canvas.restore();
    if (widgetRenderBox != null && barSize == GuruAssetBarSizeSpec.s2) {
      Offset widgetOffset = (widgetRenderBox as RenderBox).localToGlobal(Offset.zero);
      final Paint paint = Paint()..filterQuality = FilterQuality.high;
      final src = Rect.fromLTWH(0, 0, assetIcon.width.toDouble(), assetIcon.height.toDouble());
      final dst = Rect.fromLTWH(widgetOffset.dx, widgetOffset.dy, designSpecS2.assetIconSize, designSpecS2.assetIconSize);
      canvas.drawImageRect(assetIcon, src, dst, paint);
    }
  }
}
