import {API, BaseService} from '@bthlabs/homehub-core';
import isEqual from 'lodash/isEqual';

import {kServiceIFormicarium, kWidgetIFormicarium} from './defs';

export class IFormicariumService extends BaseService {
  static kind = kServiceIFormicarium;
  static widget = kWidgetIFormicarium;
  static emptyCharacteristics () {
    return {
      name: '',
      mqtt: '',
      topic: 'iformicarium',
    };
  }
  async start () {
    const result = await API.Services.start(
      kServiceIFormicarium, this.instance, this.characteristics
    );

    this.notify(result);
  }
  async stop () {
    return API.Services.stop(kServiceIFormicarium, this.instance);
  }
  setCharacteristics (newCharacteristics) {
    const shouldRestart = Object.keys(
      IFormicariumService.emptyCharacteristics()
    ).some(
      (key) => {
        return !isEqual(this.characteristics[key], newCharacteristics[key]);
      }
    );

    super.setCharacteristics(newCharacteristics);

    if (shouldRestart) {
      this.restart();
    }
  }
}
