import 'package:flutter/scheduler.dart';
import 'package:flut/flut/object.dart';
import 'package:flut/flut/runtime.dart';
import 'package:flut/flut/error.dart';

class FlutSchedulerBinding extends FlutRealtimeObject<SchedulerBinding> {
  FlutSchedulerBinding.wrap({
    required super.runtime,
    required super.oid,
    required super.target,
  }) : super.wrap(type: 'SchedulerBinding');

  static void registerStatics(FlutRuntime runtime) {
    runtime.registerStatic('SchedulerBinding.instance', instance);
  }

  static FlutSchedulerBinding instance(FlutRuntime runtime) {
    final binding = SchedulerBinding.instance;
    return runtime.wrapObject<FlutSchedulerBinding>(
      binding,
      (oid) => FlutSchedulerBinding.wrap(
        runtime: runtime,
        oid: oid,
        target: binding,
      ),
    );
  }

  @override
  dynamic getRawProperty(String property) {
    throw FlutUnknownPropertyException('SchedulerBinding', property);
  }

  @override
  bool setProperty(String property, dynamic value) {
    throw FlutUnknownPropertyException('SchedulerBinding', property);
  }

  @override
  dynamic callMethod(String method, Map<String, dynamic> args) {
    switch (method) {
      case 'addPostFrameCallback':
        final callbackId = args['callbackId'] as String;
        flutTarget.addPostFrameCallback((_) {
          flutRuntime.callAction(callbackId);
        });
        return null;
    }
    throw FlutUnknownMethodException(method);
  }
}
