/**
 * This file contains utilities for performing various tasks
 */
if (!qcms){
    var qcms = {}
}

qcms.utils = {}

qcms.utils.csv = function (_dhandler){ //(uri,_render,_error) {
    var http = HttpClient.instance()
    var format = (_value)=>{return _value.trim()}
    var _error = (_error == null)? console.log: _error
    http.get(_dhandler._uri,(_x)=>{
        if (_x.status == 200){
            var _data = []
            _x.responseText.split('\n').forEach((_row,_index)=>{
                values = jx.utils.patterns.visitor(_row.split(','),format)
                if(_index == 0){
                    _attr = values
                }else{
                    //
                    // Let's build the object here
                    var _object = {}
                    _attr.forEach((_field,_index)=>{
                        _object[_field] = values[_index]
                    })
                    _data.push(_object)
                }
            })
            //--
            // At this point we have formatted csv to an array of JSON objects
            if (_data.length && _dhandler.render != null){
                _dhandler.render (_data)
            }else{
                //
                // @TODO: We need an error handler otherwise we will have to handle it ourselves
                _error (_data)
            }
        }
    })

}

qcms.code = {}
qcms.code.documentation= function(_uri,_id) {
    this._uri = _uri
    this._id = _id[0] == '#'?_id :( _id[0] == '.' ?_id: '#'+_id)
    this.events = {_open:{},_find:{}}
    this.events._open = function(_uri){
        
        qcms.page.load(_uri,`${_id} .content`)
    }
    
    

    this.render = function (_data){
        //
        //NOTE: Because this function will be a callback, it will lose reference to the class members but will be able to access them without 'this'
        //
        var _open = this.events._open
        var _menu = $('<div class="toc"><div class="border-round"><input type="text" class="doc-find" placeholder="[keyword, term]"/></div></div>')
        var _frame = $('<div class="items"></div>')
        _data.forEach((_row,_index) =>{
            // _div = $('<div class="active label bold"></div>').html(_row.alias).append($('<div class="small"></div>').html(_row.description) )
            // _div = $(`<div class="active bold label">${_row.alias}</div><div class="small">${_row.description}</div>`)
            _div = $(`<div class="active bold label" align="left">${_row.alias}</div><div class="small" style="padding-left:8px;">${_row.description}</div>`)
            $(_div).attr('file',_row.file)
            _div[0].onclick = function (){
                //
                // we need to load the area with the given page
                console.log($(this).attr('file'))
                var _uri = $(this).attr('file')
                // qcms.page.load(_uri,'')
                console.log([_uri ,_id, ' ########'])
                _open(_uri,_id)
                
            }
            $(_frame).append(_div)
        })

        //
        // adding the menu to the location specified
        $(_id).empty()
        $(_menu).append(_frame)
        $(_id).append(_menu)
        $(_id).append($('<div class="content"></div>'))
        $(_id).attr('class','doc-browser')
        $(_id).show()
        //
        // click the first item on the list

        $(_frame).children()[0].click()

    }
    this.reload = function (){
        // var _id = this._id 
        // var events = this.events 
        // var _openEvent =this._openEvent
        // var _find = this.events._find ;
        $(this._id).empty()
        qcms.utils.csv(this) //(this._uri,this.render)
        $(this._id).find('*.child').show() //css({display:'block'})
    }
}


