import { get, computed } from '@ember/object';
import { inject as service } from '@ember/service';
import Resource from 'ember-api-store/models/resource';
import C from 'ui/utils/constants';
import { parseExternalId } from 'ui/utils/parse-externalid';

export const BUILT_IN_UI = ['amazonec2','digitalocean', 'azure', 'exoscale','packet','rackspace','vmwarevsphere','aliyunecs'];
export const BUILT_IN_ICON_ONLY = ['openstack'];

function displayUrl(url) {
  url = url||'';
  let parts = url.split('/');
  let out    = null;

  if ( parts.length < 2 )
  {
    return url;
  }

  if (url.indexOf('github.com') >= 0) {
    out = `.../${parts[parts.length-2]}/${parts[parts.length-1]}`;
  } else {
    out = url;
  }
  return out;
}

export default Resource.extend({
  type: 'nodeDriver',
  modalService: service('modal'),
  catalog: service(),
  intl: service(),

  actions: {
    activate: function() {
      return this.doAction('activate');
    },

    deactivate: function() {
      return this.doAction('deactivate');
    },

    edit: function() {
      get(this,'modalService').toggleModal('modal-edit-driver', this);
    },
  },

  catalogTemplateIcon: computed('externalId', function() {
    let parsedExtId = parseExternalId(get(this,'externalId')) || null;

    if (!parsedExtId) {
      return null;
    }

    if (get(this,'catalog').getTemplateFromCache(parsedExtId.templateId)) {
      return get(this,'catalog').getTemplateFromCache(parsedExtId.templateId).get('links.icon');
    } else {
      return `${get(this,'app.baseAssets')}assets/images/providers/generic-driver.svg`;
    }

  }),

  displayName: computed('name', 'intl.locale', function() {
    const intl = get(this,'intl');
    const name = get(this, 'name');
    const key = `nodeDriver.displayName.${name}`;

    if ( name && intl.exists(key) ) {
      return intl.t(key);
    } else if ( name ) {
      return name;
    } else {
      return '(' + get(this, 'id') + ')';
    }
  }),

  displayIcon: computed('name', function() {
    let name = get(this,'name');

    if ( get(this,'hasBuiltinUi') ) {
      return name;
    } else {
      return 'generic';
    }
  }),

  displayUrl: function() {
    return displayUrl(get(this,'url'));
  }.property('url'),

  displayChecksum: computed('checksum', function() {
    return get(this,'checksum').substring(0, 8);
  }),

  displayUiUrl: function() {
    return displayUrl(get(this,'uiUrl'));
  }.property('uiUrl'),

  hasBuiltinUi: function() {
    return BUILT_IN_UI.indexOf(get(this,'name')) >= 0;
  }.property('name'),

  hasBuiltinIconOnly: function() {
    return BUILT_IN_ICON_ONLY.indexOf(get(this,'name')) >= 0;
  }.property('name'),

  isCustom: function() {
    return !get(this,'builtin') && !get(this,'externalId');
  }.property('builtin','externalId'),

  hasUi: function() {
    return get(this,'hasBuiltinUi') || !!get(this,'uiUrl');
  }.property('hasBuiltinUi'),

  newExternalId: function() {
    var externalId = C.EXTERNAL_ID.KIND_CATALOG + C.EXTERNAL_ID.KIND_SEPARATOR + get(this,'selectedTemplateModel.id');
    return externalId;
  }.property('isSystem','selectedTemplateModel.id'),

  availableActions: computed('links.{update,remove}','actionLinks.{activate,deactivate}','builtin', function() {
    let a = get(this,'actionLinks');
    let l = get(this,'links');
    let builtin = !!get(this,'builtin');

    return [
      { label: 'action.edit',        icon: 'icon icon-edit',         action: 'edit',         enabled: !!l.update && !builtin },
      { divider: true },
      { label: 'action.activate',    icon: 'icon icon-play',         action: 'activate',     enabled: !!a.activate, bulkable: true},
      { label: 'action.deactivate',  icon: 'icon icon-pause',        action: 'deactivate',   enabled: !!a.deactivate, bulkable: true},
      { divider: true },
      { label: 'action.remove',      icon: 'icon icon-trash',        action: 'promptDelete', enabled: !!l.remove && !builtin, altAction: 'delete', bulkable: true},
      { divider: true },
      { label: 'action.viewInApi',   icon: 'icon icon-external-link',action: 'goToApi',      enabled: true },
    ];
  }),

  externalIdInfo: function() {
    return parseExternalId(get(this,'externalId'));
  }.property('externalId'),
});

@@ -2151,24 +2151,40 @@
  displayUrl:
+ computed('url',
  function() 
@@ -2232,23 +2232,8 @@
 %0A  %7D
-.property('url'
 ),%0A%0A
@@ -2356,16 +2356,34 @@
 ayUiUrl:
+ computed('uiUrl',
  functio
@@ -2437,25 +2437,8 @@
 %0A  %7D
-.property('uiUrl'
 ),%0A%0A
@@ -2452,16 +2452,33 @@
 iltinUi:
+ computed('name',
  functio
@@ -2541,32 +2541,16 @@
 = 0;%0A  %7D
-.property('name'
 ),%0A%0A  ha
@@ -2566,16 +2566,33 @@
 conOnly:
+ computed('name',
  functio
@@ -2666,39 +2666,56 @@
 %0A  %7D
-.property('name'),%0A%0A  isCustom:
+),%0A%0A  isCustom: computed('builtin','externalId',
  fun
@@ -2791,52 +2791,45 @@
 %0A  %7D
-.property('builtin','externalId'),%0A%0A  hasUi:
+),%0A%0A  hasUi: computed('hasBuiltinUi',
  fun
@@ -2905,52 +2905,76 @@
 %0A  %7D
-.property('hasBuiltinUi'),%0A%0A  newExternalId:
+),%0A%0A  newExternalId: computed('isSystem','selectedTemplateModel.id',
  fun
@@ -3132,55 +3132,8 @@
 %0A  %7D
-.property('isSystem','selectedTemplateModel.id'
 ),%0A%0A
@@ -4144,16 +4144,39 @@
 lIdInfo:
+ computed('externalId',
  functio
@@ -4240,30 +4240,8 @@
 %0A  %7D
-.property('externalId'
 ),%0A%7D

Replace property methods with computed