+class @MindpinHook
+  constructor: (@$fm) ->
+    console.log 'mindpin hook constructor'
+
+  el_click: (el) ->
+    console.log 'mindpin hook el click'
+
+  buckets_success: (buckets) =>
+    console.log 'mindpin hook buckets success'
+
+  create_bucket_success: (bucket) =>
+    console.log 'mindpin hook create bucket success'
+
+  add_to_bucket_success: (bucket) =>
+    console.log 'mindpin hook add to bucket success'
+
+  remove_from_bucket_success: (bucket) ->
+    console.log 'mindpin hook remove_from_bucket_success'
+
+  error: (error) ->
+    console.log 'error'
+    console.log error
+
 class @MindpinBuckets
-  constructor: (@$path_fix, @$progress)->
+  constructor: (@$configs)->
     @_init()
 
+  _default_configs:
+    path_fix: ""
+    selector: "[data-rel=mindpin_buckets]"
+    bucket_type: "Bucket"
+    hook_class: MindpinHook
+
   _init: ->
-    if !@$path_fix
-      @$path_fix = ""
-    @buckets_path = @$path_fix + "/buckets"
-    @bucketings_path = @$path_fix + "/bucketings"
-    @$progress ||= new MindpinBucketsAdapter(@)
+    for key, val of @_default_configs
+      @$configs[key] ||= val
 
-  buckets: (bucket_type, resource_type, resource_id) ->
-    jQuery.ajax
-      url: "#{@buckets_path}?bucket_type=#{bucket_type}&resource_type=#{resource_type}&resource_id=#{resource_id}"
-      method: "GET"
-      success: (res) =>
-        console.log "buckets success"
-        console.log res
-        if res['result']
-          @$progress.get_buckets_success(res.result)
-        else
-          @$progress.error(res.error)
+    @path_fix = @$configs["path_fix"]
+    @buckets_path = @path_fix + "/buckets"
+    @bucketings_path = @path_fix + "/bucketings"
 
-  create_bucket: (bucket_type, name, desc) ->
+    @$el = jQuery(@$configs["selector"])
+
+    @resource_type = @$el.data('resource-type')
+    @resource_id = @$el.data('resource-id')
+    @bucket_type = @$configs["bucket_type"]
+    @hook = new @$configs["hook_class"](@)
+    @_init_buckets()
+    @_bind()
+
+  _bind: () ->
+    console.log '_bind'
+    # 添加按钮点击
+    that = @
+    @$el.click () -> 
+      console.log 'el click'
+      that.hook.el_click(@)
+
+  _init_buckets: () ->
+    console.log '_init_buckets'
+    @buckets(@bucket_type, @resource_type, @resource_id)
+
+  _create_bucket: (bucket_type, name, desc) ->
     jQuery.ajax
       url: @buckets_path
       method: "POST"

        console.log res
        if res['result']
          console.log res.result
          @$progress.remove_from_bucket_success(res.result)
        else
          @$progress.error(res.error)

###
  用于处理MindpinBuckets回调的类
  里面规定了一些方法，可以根据传输的不同结果，做不同的处理
###
###
  获取buckets            成功、失败
  创建bucket             成功、失败
  将resource添加到bucket 成功、失败
  将resource从bucket删除 成功、失败

  失败均为500，结构也一样，可以统一
###
class @MindpinBucketsAdapter
  constructor: (@mindpin_buckets) ->

  get_buckets_success: (buckets) ->
    console.log 'get_buckets_success'
    console.log buckets

  create_bucket_success: (bucket) ->
    console.log 'create_bucket_success'
    console.log bucket

  add_to_bucket_success: (bucket) ->
    console.log 'add_to_bucket_success'
    console.log bucket

  remove_from_bucket_success: (bucket) ->
    console.log 'remove_from_bucket_success'
    console.log bucket

  error: (error) ->
    console.log 'error'
    console.log error

# example 
#class @CustomAdapter extends MindpinBucketsAdapter
  #constructor: ()->

  #get_buckets_success: (buckets) ->
    #alert(buckets)
#
#buckets = new MindpinBuckets("",new CustomAdapter())
#buckets.buckets("folder")

        console.log res
        if res['result']
          console.log res.result
          @hook.create_bucket_success(res.result)
        else
          @hook.error(res.error)

  _add_to_bucket: (resource_type, resource_id, bucket_type, bucket_id) ->
    jQuery.ajax
      url: @bucketings_path
      method: "POST"
