import Mixin from '@ember/object/mixin';
import { get, set, setProperties } from '@ember/object';
import { inject as service } from '@ember/service'

export default Mixin.create({
  globalStore: service(),

  loadClusterRule(model) {
    const gs = get(this, 'globalStore');
    const t = get(model, 'targetType');

    set(model, '_targetType', t);

    const nodeRule = gs.createRecord({ type: 'nodeRule' });
    const systemServiceRule = gs.createRecord({ type: 'systemServiceRule' });
    const eventRule = gs.createRecord({ type: 'eventRule' });
    const metricRule = gs.createRecord({
      type:           'metricRule',
      comparison:     'greater-than',
      duration:       '5m',
      thresholdValue: 0,
    })

    const et = get(model, 'eventRule.eventType') || '';

    switch (t) {
    case 'event':
      setProperties(model, {
        nodeRule,
        systemServiceRule,
        metricRule,
        _targetType: `${ et.toLowerCase() }Event`,
      });
      break;
    case 'node':
    case 'nodeSelector':
      setProperties(model, {
        eventRule,
        systemServiceRule,
        metricRule,
      });
      break;
    case 'systemService':
      setProperties(model, {
        nodeRule,
        eventRule,
        metricRule,
      });
      break;
    case 'metric':
      setProperties(model, {
        nodeRule,
        systemServiceRule,
        eventRule,
      })
      break;
    }

    return model
  },

  loadProjectRule(model) {
    const globalStore = get(this, 'globalStore');
    const t = get(model, 'targetType');

    set(model, '_targetType', t);
    const workloadRule = globalStore.createRecord({ type: 'workloadRule' })
    const podRule = globalStore.createRecord({ type: 'podRule' })
    const metricRule = globalStore.createRecord({ type: 'metricRule' })

    switch (t) {
    case 'pod':
      setProperties(model, {
        workloadRule,
        metricRule,
      })
      break;
    case 'workload':
    case 'workloadSelector':
      setProperties(model, {
        podRule,
        metricRule,
      })
      break;
    case 'metric':
      setProperties(model, {
        podRule,
        workloadRule,
      })
      break;
    }

    return model
  },
});
