import $         from '../$'
import selectize from 'es-selectize'
import {raf}     from 'es-raf'

import Text from './text'
import html from '../../templates/controls/dropdown'

isABrokenBrowser = (window.navigator.userAgent.indexOf('MSIE') > 0 || window.navigator.userAgent.indexOf('Trident') > 0)

coolDown = -1

export default class Select extends Text
  tag: 'dropdown'
  html: html

  selectOptions: {}

  options: ->
    return @selectOptions

  readOnly: false
  ignore: false

  events:
    updated: ->
      @onUpdated()
    mount: ->
      @onUpdated()

  getValue: (event)->
    return $(event.target).val()?.trim().toLowerCase()

  initSelect: ($select)->
    options = []
    invertedOptions = {}
    for value, name of @options()
      options.push
        text: name
        value: value

      invertedOptions[name] = value

    selectize $select,
      dropdownParent: 'body'
      # valueField: 'value'
      # labelField: 'text'
      # searchField: 'text'
    .on 'change', (event) =>
      # This isn't working right, sometimes you have one change firing events on unrelated fields
      if coolDown != -1
        return

      coolDown = setTimeout ->
        coolDown = -1
      , 100

      @change event
      event.preventDefault()
      event.stopPropagation()
      false

    select = $select[0]
    select.selectize.addOption options
    select.selectize.addItem [@input.ref.get(@input.name)] || [], true
    select.selectize.refreshOptions false

    #support auto fill
    $input = $select.parent().find('.selectize-input input:first')
    $input.on 'change', (event) ->
      val = $(event.target).val()
      if invertedOptions[val]?
        $select[0].selectize.setValue(invertedOptions[val])

    #support read only
    if @readOnly
      $input.attr('readonly', true)

  init:(opts)->
    super arguments...

    @style = @style || 'width:100%'

  onUpdated: ->
    if !@input?
      return

    $select = $(@root).find('select')
    select = $select[0]
    if select?
      v = @input.ref.get @input.name
      if !@initialized
        raf =>
          @initSelect $select
          @initialized = true
      else if select.selectize? && v != select.selectize.getValue()
        select.selectize.clear true
        select.selectize.addItem v, true
    else
      $control = $(@root).find('.selectize-control')
      if !$control[0]?
        raf =>
          @scheduleUpdate()

    # @on 'unmount', ()=>
    #   $select = $(@root).find('select')

Select.register()

