       position :
         top    : 20
 
-    {advanced, domain, addDomain} = @modalTabs.forms.Settings.inputs
-    {hostnameAlias} = @getData()
-    {label} = advanced.getOptions()
+    {hostnameAlias}    = @getData()
+    {advanced, domain} = @modalTabs.forms.Settings.inputs
+    {label}            = advanced.getOptions()
 
     advanced.hide()
     label.setClass 'advanced'

        Settings            :
          fields            :
            domain          :
              label         : 'Your VM is pointed to'
            #   name          : 'domain'
            #   type          : 'select'
            #   itemClass     : KDSelectBox
            #   defaultValue  : ''
            #   selectOptions : @bound 'prepareDomainOptions'
            # addDomain       :
            #   label         : ''
              name          : 'addDomain'
              placeholder   : 'type a domain name'
              nextElement   :
                extras      :
                  itemClass : KDCustomHTMLView
                  cssClass  : 'root-domain'
                  partial   : ".#{KD.whoami().profile.nickname}.kd.io"
                domainButton:
                  itemClass : KDButtonView
                  title     : 'Add domain'
                  callback  : => log 'add ulan'
                cancelButton:
                  itemClass : KDButtonView
                  title     : 'Cancel'
                  callback  : @bound 'hideAddDomain'
            alwaysOn        :
              label         : 'Keep your VM always on'
              name          : 'alwaysOn'

      title    : 'Re-initialize VM'
      callback : -> KD.singletons.vmController.reinitialize hostnameAlias, ->

    advanced.addSubView @deleteButton = new KDButtonView
      style    : 'solid compact red'
      title    : 'Delete VM'
      callback : @bound 'deleteVM'

    label.on 'click', @bound 'toggleAdvancedSettings'

    @fetchDomains (jDomain) ->
      domain.setValue jDomain.domain  if jDomain





    # FIXME - SY
    # domain.setValue to the domain which vm is connected to
    # alwaysOn.setValue if the vm is always on

  handleChange: ->

    {domain, Save} = @modalTabs.forms.Settings.inputs
    inputValue     = domain.getValue()

    if inputValue isnt @currentDomain?.domain and inputValue isnt ''
    then Save.show()
    else Save.hide()



  submitDomainChange: ->

    {domain} = @modalTabs.forms.Settings.inputs
    if domain.getValue() is 'add-domain'
    then @convertToSubdomainField()
    else @revertToDomainField()


  convertToSubdomainField: (toggle) ->

    {rootDomain, domain} = @modalTabs.forms.Settings.inputs
    rootDomain.show()

    domain.setValue ''

    @handleChange()
    domain.setFocus()
    toggle()


  revertToDomainField: (toggle) ->

    {nickname} = KD.whoami().profile
    {rootDomain, domain} = @modalTabs.forms.Settings.inputs
    rootDomain.hide()
    domain.setValue "#{nickname}.kd.io"

    @handleChange()
    toggle()


  toggleAdvancedSettings: (event) ->

    KD.utils.stopDOMEvent event

    {advanced} = @modalTabs.forms.Settings.inputs
    {label} = advanced.getOptions()

    label.toggleClass 'expanded'
    advanced.toggleClass 'hidden'


  deleteVM: ->

    {hostnameAlias} = @getData()

    KD.singletons.vmController.confirmVmDeletion @getData(), (err) =>

      return KD.showError err  if err

      @refreshSidebarVMs()
      @destroy()


  linkDomain: ->

    {hostnameAlias} = @getData()

    {domain} = @modalTabs.forms.Settings.inputs

    log "@gokmen please add this domain #{domain.getValue()}"


  fetchDomains: (callback = ->) ->

    {hostnameAlias} = @getData()

    KD.remote.api.JDomain.fetchDomains (err, domains) =>

      if err
        @modalTabs.forms.Settings.fields.domain.hide()
        return callback [ title : "Couldn't retrieve domains!" ]

      [domain] = domains.filter (domain) -> domain.hostnameAlias.first is hostnameAlias
      @currentDomain = domain
      callback domain


  refreshSidebarVMs: ->

    {dock} = KD.singletons
    dock.fetchVMs (vms) ->
      dock.vmsList.removeAllItems()
      dock.listVMs vms

