var h = require('hyperscript')
var obs = require('observable')
var u = require('../util')
var pull = require('pull-stream')

exports.needs = {
  avatar_name: 'first',
  avatar_link: 'first',
  sbot_get: 'first',
  sbot_query: 'first',
  markdown: 'first',
  message_confirm: 'first',
  ting_myskills: 'first',
  ting_inquiry_adopted: 'first'
}

exports.gives = { message_content: true }

exports.create = function (api) {

  var self_id = require("../keys").id

  return {
    message_content: function (msg) {
      var c = msg.value.content
      if(c.type === 'ting-inquiry') {
        var positions_el = h('ul')

        var skillMatching = {}

        // terribly convoluted
        api.ting_myskills(self_id, function(err, mySkills) {
          if(err) { throw err; return}
          mySkills = mySkills.map(function(msg) { return msg.value.content.sk0rg })

          c.skills.forEach(function(skillID) {
            skillMatching[skillID] = obs()
            api.ting_inquiry_adopted(msg.key, skillID, function(err, grouped) {
              skillMatching[skillID](h('span',
                Object.keys(grouped).map(function(author) {
                  if (grouped[author] > 0) {
                    var el = h("span", api.avatar_link(author, api.avatar_name(author)))
                    if (author == self_id) {
                      el.appendChild( h("a",{href:"#", onclick: function(ev) {
                        ev.preventDefault()
                        api.message_confirm({
                          "type": 'ting-adopt', "adopted": false,
                          "skill": skillID, "inquiry": msg.key,
                        }, function (err, msg) {
                          if(err) {throw err; return}
                          if(!msg) return
                        })
                      }}, "(-)"))
                    }
                    return el
                  }
                })
              ))
            })


            api.sbot_get(skillID, function(err, skMsg) {
              if(err) { throw err; return}
              positions_el.appendChild(h('li',
                (mySkills.indexOf(skillID) >= 0) ?
                  h("a",{href:"#", onclick: function(ev) {
                    ev.preventDefault()
                    api.message_confirm({
                      "type": 'ting-adopt', "adopted": true,
                      "skill": skillID, "inquiry": msg.key,
                    }, function (err, msg) {
                      if(err) {throw err; return}
                      if(!msg) return
                    })
                  }}, "(+)") : null,
                skMsg.content.name+": ",
                skillMatching[skillID]
              ))
            })
          })
        });

        var el = h('div.inquiry',
          h('p', api.markdown(c.text)),
          h('strong','Hat: ', c.hat ? api.avatar_link(c.hat, api.avatar_name(c.hat)): "TODO: open hat"),
          h('br'),
          h('strong', 'skills needed:'),
          positions_el
        )
        if (c.location && c.location.length == 4){
          el.appendChild( h('strong', 'location:'))
          el.appendChild( h("iframe", {
            "style": "width:100%",
            "frameborder":"0",
            "scrolling":"no",
            "src":"http://www.openstreetmap.org/export/embed.html?layer=mapnik&bbox="+encodeURIComponent(c.location.join(","))}))
        }

        return el
      }
    }

  }
}

@@ -249,16 +249,44 @@
 first',%0A
+  message_compose: 'first',%0A
   messag
@@ -668,16 +668,608 @@
 ing = %7B%7D
+%0A        var currDescr = c.text%0A        var mdDescr = obs(api.markdown(currDescr))%0A%0A        // convoluted - load text edits%0A        pull(%0A          api.sbot_query(%7Bquery:%5B%0A            %7B%22$filter%22: %7B %22value%22: %7B%0A              %22author%22: msg.value.author,%0A              %22content%22: %7B%0A                %22type%22:%22ting-edit%22,%0A                %22inquiry%22: msg.key%0A              %7D%0A            %7D%7D%7D,%0A          %5D, %22reverse%22: true, limit:1%7D),%0A          pull.drain(function (update) %7B%0A            currDescr = update.value.content.text%0A            mdDescr(api.markdown(update.value.content))%0A          %7D)%0A        )
 %0A%0A      
@@ -1292,16 +1292,41 @@
 nvoluted
+ - load (matching) skills
 %0A       
@@ -2589,16 +2589,52 @@
    %7D)%0A%0A%0A
+            // adopt skills buttons%0A
         
@@ -3467,33 +3467,618 @@
  h('
-p', api.markdown(c.text))
+strong', 'description'),%0A          (self_id === msg.value.author) ? h('a', %7Bhref:%22#%22, onclick: function(e) %7B%0A            e.preventDefault()%0A%0A            mdDescr(api.message_compose(%0A              %7B%0A                type:%22ting-edit%22,%0A                %22inquiry%22: msg.key,%0A              %7D,%0A              %7B%0A                value: currDescr,%0A              %7D,%0A              function(err, msg) %7B%0A                if (err) throw err%0A                currDescr = msg.value.content.text%0A                mdDescr(api.markdown(msg.value.content))%0A              %7D)%0A            )%0A          %7D%7D, %22(edit)%22) : null,%0A          mdDescr
 ,%0A  
@@ -4265,32 +4265,89 @@
 ns_el%0A        )%0A
+          /* TODO for v1.5: missing picker and queries..%0A
         if (c.lo
@@ -4704,24 +4704,35 @@
 ))%0A        %7D
+%0A        */
 %0A%0A        re

edit description text