 KDButtonView           = kd.ButtonView
 KDListItemView         = kd.ListItemView
 KDCustomHTMLView       = kd.CustomHTMLView
+KDCheckBox             = kd.CheckBox
+KDListViewController = kd.ListViewController
+KDHitEnterInputView  = kd.HitEnterInputView
 
+TopicLeafItemView    = require './topicleafitemview'
+SimilarItemView      = require './similaritemview'
 
 module.exports = class TopicItemView extends KDListItemView
 

        @typeLabel.toggleClass 'active'
    
    @createSettingsView()

  createSettingsView: ->


    @settings.addSubView deleteButton = new KDButtonView
      cssClass : 'solid compact outline'
      title    : 'DELETE CHANNEL'
      
    @settings.addSubView @searchContainer = new KDCustomHTMLView
      cssClass: 'search'
      partial : '<span class="label">Find similar channels</span>'

    @searchContainer.addSubView @searchInput = new KDHitEnterInputView
      type        : 'text'
      placeholder : @getData().name
      callback    : @bound 'searchSimilarChannels'

    @createSimilarChannelsListController()
    @settings.addSubView @similarChannelsListController.getView()

  searchSimilarChannels: ->

    @skip  = 0
    query = @searchInput.getValue()

    @similarChannelsListController.removeAllItems()
    @similarChannelsListController.lazyLoader.show()
    @fetchSimilarChannels query


  fetchSimilarChannels:(query = "") ->

    options  =
      name   : query
      limit  : @getOptions().itemLimit
      sort   : { timestamp: -1 }
      skip   : @skip
      
    kd.singletons.socialapi.channel.searchTopics options , (err, channels) =>
      if err
        @similarChannelsListController.lazyLoader?.hide()
        return kd.warn err
      
      @listSimilarChannels channels
      


  listSimilarChannels: (channels) ->

    unless channels.length
      return @similarChannelsListController.lazyLoader?.hide()

    @skip += channels.length

    for channel in channels
      @similarChannelsListController.addItem channel

    @similarChannelsListController.lazyLoader?.hide()
    @searchContainer.show()



  createLeafChannelsListController: ->

    @leafChannelsListController       = new KDListViewController
      viewOptions         :
        wrapper           : yes
        itemClass         : TopicLeafItemView
        itemOptions       : {}
      noItemFoundWidget   : new KDCustomHTMLView
      startWithLazyLoader : yes
      lazyLoadThreshold   : .99
      lazyLoaderOptions   :
        spinnerOptions    :
          size            : width: 28

    #@listController.on 'LazyLoadThresholdReached', @bound 'fetchChannels'
    
    
  createSimilarChannelsListController: ->

    @similarChannelsListController       = new KDListViewController
      viewOptions         :
        wrapper           : yes
        itemClass         : SimilarItemView
        itemOptions       : {}
      noItemFoundWidget   : new KDCustomHTMLView
      startWithLazyLoader : yes
      lazyLoadThreshold   : .99
      lazyLoaderOptions   :
        spinnerOptions    :
          size            : width: 28

    #@listController.on 'LazyLoadThresholdReached', @bound 'fetchChannels'

  pistachio: ->
    data     = @getData()
