const nest = require('depnest')
const isBlog = require('scuttle-blog/isBlog')
const { h, Value, computed, when, resolve } = require('mutant')


exports.gives = nest('message.html', {
  canRender: true,
  render: true
})

exports.needs = nest({
  'about.obs.color': 'first',
  'app.navigate': 'first',
  'blob.sync.url': 'first',
  'message.html.decorate': 'reduce',
  'message.html.layout': 'first',
  'message.html.markdown': 'first',
  'sbot.obs.connection': 'first'
})

exports.create = function (api) {

  return nest('message.html', {
    render: blogRenderer,
    canRender
  })

  return nest('message.html.render', blogRenderer)

  function blogRenderer (msg, opts) {
    if (!canRender(msg)) return

    var content = null
    // show a card (if there's no body loaded) or the full blog (if the blog body is loaded)
    // msg is decorated with a `body` attribute when loaded using feed.obs.thread from patchcore
    if (msg.body) {
      content = h('BlogFull.Markdown', [
        h('h1', msg.value.content.title),
        api.message.html.markdown(msg.body)
      ])
    } else {
      content = BlogCard({
        blog: msg.value.content,
        onClick: () => api.app.navigate(msg.key),
        color: api.about.obs.color,
        blobUrl: api.blob.sync.url
      })
    }
    
    const element = api.message.html.layout(msg, Object.assign({}, {
      content,
      layout: 'default'
    }, opts))

    return api.message.html.decorate(element, { msg })
  }
}

function BlogCard ({ blog, blobUrl, onClick, color }) {
  const thumbnail = when(blog.thumbnail,
    h('Thumbnail', {
      style: {
        'background-image': `url("${blobUrl(resolve(blog.thumbnail))}")`,
        'background-position': 'center',
        'background-size': 'cover'
      }
    }),
    h('Thumbnail -empty', {
      style: { 'background-color': color(blog.title) }
    }, [
      h('i.fa.fa-file-text-o')
    ])
  )

  var b = h('BlogCard', { 'ev-click': onClick }, [
    // h('div.context', [
    //   api.about.html.avatar(author, 'tiny'),
    //   h('div.name', api.about.obs.name(author)),
    //   api.message.html.timeago(blog)
    // ]),
    h('div.content', [
      thumbnail,
      h('div.text.Markdown', [
        h('h1', blog.title),
        // when(blog.channel, api.message.html.channel(blog.channel))
        h('div.summary', blog.summary),
        h('div.read', 'Read blog')
      ])
    ])
  ])

  return b
}

function canRender (msg) {
  if (isBlog(msg)) return true
}
@@ -85,25 +85,8 @@
 %7B h,
- Value, computed,
  whe
@@ -119,17 +119,16 @@
 tant')%0A%0A
-%0A
 exports.
@@ -482,17 +482,16 @@
 (api) %7B%0A
-%0A
   return
@@ -564,60 +564,8 @@
 %7D)%0A%0A
-  return nest('message.html.render', blogRenderer)%0A%0A
   fu
@@ -1213,13 +1213,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2392,13 +2392,14 @@
 return true%0A
-
 %7D
+%0A

clean up some code in blog (standard js)