/**
 * (c) 2017 Ángel Olea Gómez   www.ovalo-software.com
 *
 * #FIXME description
 *
 * ---------------- ------------------------------------------
 * 2017-03-11 11:48 - aolea - creation
 *
 */


const ViewGen = function (_, h$, m$) {
  const {div} = h$
  
  this.component = function (css, ...rest) {
    return div({'class':`${css} ts-component`,
                'data-compo-klass':(ctx) => ctx.meta.name,
                'data-compo-id':(ctx) => ctx.compoId}, ...rest)
  }

  this.mold = {}

  const _ifCan = this.mold.ifCan = function (thing) {
    return function (context) {
      if (_.isNull(context.mold)) return ''
      return thing(context)
    }
  }

  const _ifOn = this.mold.ifOn = function (thing) {
    return function (context) {
      if (context.mold !== true) return ''
      return thing(context)
    }
  }

  this.mold.button = {}

  this.mold.button.onOff = _ifCan(
    div('ui toggle checkbox',
      h$.input({type:'checkbox', checked:(ctx) => ctx.mold === true ? 'checked' : undefined,
                class:'ts-change-command', 'data-verb':'moldMode', autocomplete:"off",
                'data-subject':(ctx) => ctx.mold === true ? 'false' : 'true' }),
      h$.label({}, (ctx) => ctx.mold === true ? 'ON' : 'OFF')
    )
  )
  this.mold.button.edit = _ifOn(
    h$.button({class:'ts-btn-command', 'data-verb':'mold-edit', 'data-modal':true}, 'edit')
  )
  this.mold.button.add = _ifOn(h$.button({}, 'add'))
  this.mold.button.move = _ifOn(h$.button({}, 'move'))
  this.mold.button.remove = _ifOn(h$.button({}, 'remove'))

  this.meta = {}

  const _newCtx = this.meta.newContext = function (context, key, value) {
    const newctx = _.extendOwn({}, context)
    newctx[key] = value
    return newctx
  }

  this.meta.children = function (attr, childView) {
    return div({'class':'ts-children', 'data-type':'array'},
               (context) => _.map(context.meta[attr],
                                  (child) => childView(_newCtx(context, 'meta', child))))
  }
    
  this.form = function () {

  }

  this.field = function () {}

  this.fieldMeta = function () {}
}


module.exports = {
  ViewGen: ViewGen
}

