         # @warning.show()
 
 
+  handleStateChange: (domainItem, state) ->
+
+    domainItem.setLoadingMode yes
+
+    # @warning.hide()
+
+    @askForPermission domainItem, state, (approved) =>
+      if approved
+        @changeDomainState domainItem, state
+      else
+        @revertToggle domainItem, state
+        domainItem.setLoadingMode no
+
+
+  changeDomainState: (domainItem, state) ->
+
+    { computeController } = kd.singletons
+    { stateToggle }       = domainItem
+    { domain }            = domainItem.getData()
+    machineId             = @machine._id
+    action                = if state then 'setDomain' else 'unsetDomain'
+
+    computeController.getKloud()[action] { domainName: domain, machineId }
+      .then ->
+        computeController.domains = []
+        domainItem.data.machineId = null  unless state
+
+      .catch (err) =>
+        kd.warn "Failed to change domain state:", err
+        # @warning.setTooltip title: err.message
+        # @warning.show()
+
+        @revertToggle domainItem, state
+        domainItem.setLoadingMode no
+
+      .finally =>
+        domainItem.setLoadingMode no
+
+
+  revertToggle: (domainItem, state) ->
+
+    { stateToggle } = domainItem
+    if state then stateToggle.setOff no else stateToggle.setOn no
+
+
+  askForPermission: (domainItem, state, callback) ->
+
+    { domain, machineId } = domainItem.getData()
+
+    return callback yes  if not state or not machineId
+
+    { computeController } = kd.singletons
+
+    modal = new KDModalView
+      cssClass      : 'domain-assign-modal'
+      title         : 'Reassign domain ?'
+      content       : """
+        <div class='modalformline'>
+          <p>
+            The domain that you are trying to add: <b>#{domain}</b> is already
+            assigned to another VM that you own. If you continue, this domain
+            will be reassigned to the current VM and disassociated with all others.
+          </p>
+          <p></p>
+          <p>Continue?</p>
+        </div>
+      """
+      overlay       : yes
+      cancel        : ->
+        modal.destroy()
+        callback no
+      buttons       :
+        OK          :
+          title     : 'Yes'
+          style     : 'solid red medium'
+          loader    :
+            color   : 'darkred'
+          callback  : ->
+            modal.destroy()
+            callback yes
+        cancel      :
+          title     : 'Cancel'
+          style     : 'solid light-gray medium'
+          callback  : -> modal.cancel()


    @listController.getListView()
      .on 'DeleteDomainRequested', @bound 'removeDomain'


  createAddInput: ->

KDView                    = kd.View
globals                   = require 'globals'
DomainItem                = require 'app/domains/domainitem'
KDModalView               = kd.ModalView
KDCustomHTMLView          = kd.CustomHTMLView
MachineSettingsCommonView = require './machinesettingscommonview'

