/// Created by @Haoyi on 4/7/21
///
///
///

// visible 可见
// invisible 不可见

part of '../visual_feast_engine.dart';

enum RenderVisibility { visible, invisible }

///     onAttach
///         ↓
///     onStart
///         ↓
///   onFrameUpdate
///         ↓
///     onRender
///         ↓
///     onDetach

abstract class Render {
  late Render _next;
  late Render _prev;

  RenderVisibility _visibility = RenderVisibility.visible;

  RenderVisibility get visibility => _visibility;

  void _initRenderNode() {
    _next = this;
    _prev = this;
  }

  void _unlink() {
    _prev._next = _next;
    _next._prev = _prev;
  }

  void setVisible() {
    _visibility = RenderVisibility.visible;
  }

  void setInvisible() {
    _visibility = RenderVisibility.invisible;
  }

  void dispatchAttach() {
    onAttach();
  }

  void dispatchDetach() {
    onDetach();
  }

  void dispatchStart() {
    onStart();
  }

  void dispatchFrameUpdate(double dt) {
    onFrameUpdate(dt);
  }

  void onAttach() {}

  void onDetach() {}

  void onStart() {} //开始渲染第一帧

  void onRender(Canvas canvas) {} // 渲染

  void onFrameUpdate(double dt) {} // 更新
}

abstract class RenderElement extends Render {
  String get renderId;

  String get layerName;

  int get layerPriority;

  int get orderInLayer;

  @mustCallSuper
  RenderElement();

  bool get isVisible => visibility == RenderVisibility.visible;

  @override
  void onStart() {}

  @override
  void onAttach() {}

  @mustCallSuper
  @override
  void onDetach() {}

  void onFrameEvent(dynamic event) {}
}
