import 'package:fis_measure/interfaces/process/modes/mode.dart';
import 'package:fis_common/event/event_type.dart';
import 'package:fis_measure/interfaces/process/visuals/visual.dart';
import 'package:fis_measure/process/modes/mode_base.dart';
import 'package:get/get.dart';
import 'package:vid/us/vid_us_2d_visual.dart';
import 'package:vid/us/vid_us_display_mode.dart';
import 'package:vid/us/vid_us_visual.dart';
import 'package:fis_measure/interfaces/process/visuals/visual_area.dart';

import 'tissue_area.dart';

class VisualBase implements IVisual {
  late final VidUsVisual _visualData;
  late final List<IVisualArea> _visualAreas;

  VisualBase(VidUsVisual visualData) {
    _visualData = visualData;
    if (visualData is VidUs2DVisual) {
      _visualAreas = [];
      for (var modeInfo in visualData.modes) {
        final mode = ModeBase(
          modeInfo.name,
          ModeTypeEnum.values[modeInfo.type.index],
          this,
        );

        final area = TissueArea(this, mode)..init();
        _visualAreas.add(area);
      }
    }
  }

  @override
  bool get isActive => visualAreas.any((e) => e.isActive);

  @override
  IMode? get activeMode => activeArea?.mode;

  @override
  VidUsDisplayMode get displayMode => visualData.displayMode;

  @override
  List<IMode> get modes => visualAreas.map((e) => e.mode).toSet().toList();

  @override
  List<IVisualArea> get visualAreas => _visualAreas;

  @override
  IVisualArea? get activeArea =>
      visualAreas.firstWhereOrNull((e) => e.isActive);

  @override
  VidUsVisual get visualData => _visualData;

  @override
  var activeChanged = FEventHandler<bool>();
}
