### codetube
    Copyright (C) 2011 payload payload@lavabit.com
    Copyright (C) 2011 dodo dodo.the.last@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
###

Path = require 'path'
{ Backbone } = require '../Backbone'
{ BlobLineView } = require './blobline'
{ BlobIndexView } = require './blobindex'

class BlobView extends Backbone.View

    initialize: ({@el}) ->
        @views = {}
        @view_model = # XXX so we need a BlobCollectionViewModel ??
            path:expose.blob_path or Path.join(expose.current_dir or ".","README")
            sha: expose.sha
        @model.bind 'add', @add_line
        @one 'hide:progressbar', @hide_progressbar
        do @render
        super

    hide_progressbar: =>
        @$(".progressbar").css 'visibility', 'hidden'
        @one 'show:progressbar', @show_progressbar

    show_progressbar: =>
        @$(".progressbar").css 'visibility', 'visible'
        @one 'hide:progressbar', @hide_progressbar

    render: () =>
        for cid, view of @views
            view.line.render()
            view.index.render()
        this

    add_line: (line) =>
        view =
            line:  new BlobLineView( model:line)
            index: new BlobIndexView(model:line)
        @views[line.cid] = view
        i = @model.indexOf(line)
        if i is 0
            if @model.models.length is 1
                @$('.numbers').append view.index.el
                @$('.content').append view.line.el
            else
                @$(".numbers > #"+@views[@model.at(1).cid].index.cid).before view.index.el
                @$(".content > #"+@views[@model.at(1).cid].line.cid ).before view.line.el
        else
            @$(".numbers > #"+@views[@model.at(i-1).cid].index.cid).after view.index.el
            @$(".content > #"+@views[@model.at(i-1).cid].line.cid ).after view.line.el
        do view.line.render
        do view.index.render




module.exports = { BlobView }
