import 'package:pip_services3_commons/pip_services3_commons.dart';

class MyComponentB
    implements IConfigurable, IReferenceable, IOpenable, IUnreferenceable {
  String _param1 = 'ABC2';
  int _param2 = 456;
  bool _open = false;
  String? _status;

  // Creates a new instance of the component.
  MyComponentB() {
    _status = 'Created';
    print('MyComponentB has been created.');
  }

  @override
  Future close(String? correlationId) async {
    // pass
  }

  @override
  void configure(ConfigParams config) {
    // pass
  }

  @override
  bool isOpen() {
    // pass
    return true;
  }

  @override
  Future open(String? correlationId) async {
    // pass
  }

  @override
  void setReferences(IReferences references) {
    // pass
  }

  @override
  void unsetReferences() {
    // pass
  }
}

class MyComponentA
    implements IConfigurable, IReferenceable, IOpenable, IUnreferenceable {
  String _param1 = 'ABC';
  int _param2 = 123;
  bool _open = false;
  String? _status;
  MyComponentB? _anotherComponent;

  String? dummyVariable;

  // Creates a new instance of the component.
  MyComponentA() {
    _status = 'Created';
    print('MyComponentA has been created.');
  }

  @override
  void setReferences(IReferences references) {
    _anotherComponent = references.getOneRequired<MyComponentB>(
        Descriptor('myservice', 'mycomponent-b', '*', '*', '1.0'));

    _status = 'Configured';
    print("MyComponentA's references have been defined.");
  }

  @override
  void configure(ConfigParams config) {
    _param1 = config.getAsStringWithDefault('param1', 'ABC');
    _param2 = config.getAsIntegerWithDefault('param2', 123);
    _status = 'Configured';

    print('MyComponentA has been configured.');
  }

  @override
  bool isOpen() {
    return _open;
  }

  @override
  Future open(String? correlationId) async {
    _open = true;
    _status = 'Open';
    print('MyComponentA has been opened.');
  }

  @override
  Future close(String? correlationId) async {
    _open = false;
    _status = 'Closed';
    print('MyComponentA has been closed.');
  }

  void myTask(String correlationId) {
    print('Doing my business task');
    dummyVariable = 'dummy value';
  }

  @override
  void unsetReferences() {
    _anotherComponent = null;
    _status = 'Un-referenced';
    print('References cleared');
  }
}