import 'package:design/design.dart';
import 'package:example/pages/visual/visual_debug_design_model.dart';
import 'package:example/pages/visual/visual_debug_dialog.dart';
import 'package:guru_utils/controller/lifecycle_controller.dart';
import 'package:guru_popup/guru_popup.dart';
import 'package:rxdart/rxdart.dart';
import 'package:guru_utils/extensions/extensions.dart';

enum VisualType { none, lottie }

class VisualInfo {
  final VisualType type;
  final String uri;
  final Duration duration;
  final int? repeat;

  static const VisualInfo invalid =
      VisualInfo(type: VisualType.none, uri: "", duration: Duration.zero, repeat: 0);

  const VisualInfo(
      {required this.type, required this.uri, required this.duration, this.repeat = 0});
}

class VisualDebugController extends LifecycleController {
  final VisualDebugDesignSpec designModel = VisualDebugDesignSpec.get();

  VisualDebugDialogDesignSpec get dialogDesignModel => designModel.dialogSpec;
  final BehaviorSubject<VisualInfo> visualInfoSubject = BehaviorSubject.seeded(VisualInfo.invalid);

  Stream<VisualInfo> get observableVisualInfo => visualInfoSubject.stream;

  Future loadVisual() async {
    final visualInfo = await GuruPopup.instance
        .showCustomDialog(constraintBuilder: null, child: VisualDebugDialog());
    if (visualInfo != null && visualInfo is VisualInfo) {
      visualInfoSubject.addEx(visualInfo);
    }
  }
}
