### 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'
{ client } = require '../client'
{ Backbone } = require '../Backbone'
{ TreeView } = require './tree'
{ obj_merge } = require '../helpers'

class TreesView extends Backbone.View

    initialize: ({@el, @owner, @project}) ->
        @rendering = off
        @branch = expose.branch or "master"
        @view_model = # XXX so we need a TreesCollectionViewModel ??
            current_dir:  expose.current_dir or "."
            small_tree: true
            _: (x)->x

        @model.comparator = (tree) ->
            ft = tree.get 'filetype'
            ft = { folder: 1, file: 2 }[ft] or 3
            ft + tree.get('basename')?.toLowerCase()
        @model.bind 'add', @add_file
        @bind 'show:progressbar', =>
            @$('.progressbar').css 'visibility', 'visible'
        @bind 'hide:progressbar', =>
            @$('.progressbar').css 'visibility', 'hidden'
        @render()
        super

    add_file: (tree) =>
        { current_dir } = @view_model
        if not @rendering and tree.get('dirname') is current_dir
            @rendering = on
            setTimeout =>
                @rendering = off
                @render()
            , 123

    render: () =>
        $('tr.file').remove()
        { current_dir } = @view_model
        models = _.clone @model.models
        unless current_dir is "."
            models.unshift new @model.model
                filetype: 'folder'
                path    : current_dir.split('/').slice(0, -1).join('/') or '.'
                dirname : current_dir
                basename: '..'

        for model in models
            if model.get('dirname') is current_dir
                type = {folder:'tree', file:'blob'}[model.get 'filetype']
                model.href = path.join '/', @owner, @project, type, @branch, model.get 'path'
                view = new TreeView { model }
                view.one 'clicked:folder', (folder) =>
                    Backbone.history.navigate folder.href, yes
                    if "." is folder.get 'path'
                        client.router.trigger 'route:tree', @owner, @project, @branch, "."
                view.render()
                @el.append view.el
        @$('.spinner').css 'visibility', 'hidden'
        this

module.exports = { TreesView }

