Ext.define('AuthorLogModel', {
    extend:'Ext.data.Model',
    fields:[
        {name:'time', type:'date'},
        {name:'log', type:'string'}
    ]
});
Ext.define('AuthorConsole', {
    constructor:function () {
        this.LastX = 0;
        this.LastY = Ext.Element.getViewportHeight() - 200;

        this.ShownOnce = false;
        this.LogStore = Ext.create('Ext.data.Store', {
            model:'AuthorLogModel'
        });
        this.LogListView = Ext.create('Ext.grid.Panel', {
            id:'LogListPanel',
            store:this.LogStore,
            multiSelect:false,
            columns:[
                {
                    header:'Time',
                    width:80,
                    dataIndex:'time',
                    align:'center',
                    tpl:'{time}',
                    renderer:Ext.util.Format.dateRenderer('g:i:s.u'),
                    menuDisabled:true
                },
                {
                    sortable:false,
                    header:'Log',
                    dataIndex:'log',
                    flex:1,
                    tpl:'<p style=\"white-space:normal\";>{log}</p>',
                    menuDisabled:true
                }
            ]
        });
        this.LogPanel = Ext.create('Ext.panel.Panel', {
            id:'LogPanel',
            layout:'fit',
            tbar:[
                {
                    ref:'../removeBtn',
                    icon:'./lib/images/delete.gif',
                    text:'Clear logs',
                    listeners:{
                        click:function () {
                            this.LogStore.removeAll();
                        },
                        scope:this
                    }
                }
            ],
            items:this.LogListView
        });
        this.LogWindow = Ext.create('Ext.Window', {
            title:'Log',
            id:'LogWindow',
            closeAction:'hide',
            layout:'fit',
            width:300,
            height:200,
            items:this.LogPanel,
            listeners:{
                hide:this.onHide,
                show:this.onShown,
                move:this.onMove,
                scope:this
            }
        });
    },
    moveToRightBottom:function () {
        var viewSize = Ext.Element.getViewSize();
        this.LogWindow.setPosition(viewSize.width - this.LogWindow.getWidth(), viewSize.height - this.LogWindow.getHeight());
    },
    onMove:function (c, x, y) {
        if (!this.ShownOnce) return;
        //  alert(Ext.String.format("Move to: {0}, {1}", x, y));
        this.LastX = Math.max(x, 0);
        this.LastY = Math.max(y, 0);
        // this.LogWindow.setPosition(this.LastX, this.LastY);
    },
    onShown:function () {
        // alert('shown,' + this.LastX + "," + this.LastY);
        this.ShownOnce = true;
        this.LogWindow.setPosition(this.LastX, this.LastY);
    },
    onHide:function () {
        // alert("closed");
    },
    show:function () {
        this.LogWindow.show();
    },
    hide:function () {
        this.LogWindow.hide();
    },
    isVisible:function () {
        return (this.LogWindow.isVisible());
    },
    log:function (info) {


        if (typeof(info) == 'undefined') {
            return;
        }
        info = info.toString();
        if (this.LogStore.getCount() > 50) {
            this.LogStore.removeAt(0);
        }
        var newLog = Ext.create('AuthorLogModel', {
            time:new Date(),
            log:info.split("\n").join("<br />")
        });
        this.LogStore.add(newLog);
    }
});
