﻿/*
Class representing a single log entry
*/

var LogEntry = Class.create();

LogEntry.prototype =
{
    // Constructor
    initialize : function(piType, piFunctionName, piFunctionParams, piMessageKey, piEntityType, piEntityId, piException)
    {
        // Check if the parameters have been provided
        if((piType == null) || (piFunctionName == null) || (piFunctionParams == null) || (piMessageKey == null) || (piEntityType == null))
        {
            throw 'Error: the constructor of the LogEntry class requires 5 input parameters.';
        }
        
        // Declare Type variable indicating which log type the entry is of
        this.Type = piType;
        
        // Declare variable holding the name of the function that is creating the log entry
        this.FunctionName = piFunctionName;
        // Declare variable holding the parameters of the function that is creating the log entry
        this.FunctionParams = piFunctionParams;
        
        // Declare variable holding the site id
        this.SiteId = '[not available]';
        // Declare variable holding the page id
        this.PageId = '[not available]';
        // Declare variable holding the user id
        this.UserId = '[not available]';
        // Declare variable holding the entity id
        this.EntityId = '[not available]';
        if(piEntityId != null)
        {
            this.EntityId = piEntityId;
        }
        // Declare variable holding the entity Type
        this.EntityType = piEntityType;
        // Declare variable holding the timestamp
        this.Date = new Date();
        
        // Declare variable holding the Exception if available
        this.Exception;
        if(piException != null)
        {
            this.Exception = piException;
        }
        
        // Declare variable holding the log entry's message key
        this.MessageKey = piMessageKey;
        
        // Set SiteId if available
        if((this.EntityType.toUpperCase() == 'SITE') && (this.EntityId != null) && (!this.EntityId.blank()))
        {
            this.SiteId = this.EntityId;
        }
        else if((Site != null) && (Site._entity != null) && (Site._entity.Id != null) && (!Site._entity.Id.blank()))
        {
            this.SiteId = Site._entity.Id;
        }
        
        // Set PageId if available
        if((this.EntityType.toUpperCase() == 'PAGE') && (this.EntityId != null) && (!this.EntityId.blank()))
        {
            this.PageId = this.EntityId;
        }
        else if((Site != null) && (Site.Page != null) && (Site.Page.Id != null) && (!Site.Page.Id.blank()))
        {
            this.PageId = Site.Page.Id;
        }
        
        // Set UserId if available
        if((Site != null) && (Site.User != null) && (Site.User.Id != null) && (!Site.User.Id.blank()))
        {
            this.UserId = Site.User.Id;
        }
    },
    
    Show : function()
    {
        // Check if another log entry is already showing and return if so.
        // Prevents two entries from being open at the same time
        if($$('.LogEntry').length > 0)
        {
            return;
        }
        
        // Create a window holding the log entry
        var win = new WebWindow(this.Type.capitalize());
        win.Width = 400;
        win.zIndex = 2200;
        win.CenterScreen = true;
        
        // Append the window's html node to the document body
        document.body.appendChild(win.GetHtml())
        
        // Create Table holding the log entry data
        var table = Builder.node('table');
        table.className = 'LogEntry';
        
        // Create the date / time row
        var row_Date = table.insertRow(table.rows.length);
        var label_Date = row_Date.insertCell(row_Date.cells.length);
        label_Date.className = 'LogEntryFieldLabel';
        var value_Date = row_Date.insertCell(row_Date.cells.length);
        
        label_Date.appendChild(document.createTextNode('Date / Time'));
        value_Date.appendChild(document.createTextNode(this.Date.getDate() + '.' + this.Date.getMonth() + '.' + this.Date.getFullYear() + ' ' + this.Date.getHours() + ':' + this.Date.getMinutes() + ':' + this.Date.getSeconds()+ ':' + this.Date.getMilliseconds().toString().slice(0,2)));
        
        // Create the site id row
        var row_SiteId = table.insertRow(table.rows.length);
        var label_SiteId = row_SiteId.insertCell(row_SiteId.cells.length);
        label_SiteId.className = 'LogEntryFieldLabel';
        var value_SiteId = row_SiteId.insertCell(row_SiteId.cells.length);
        
        label_SiteId.appendChild(document.createTextNode('Site Id'));
        value_SiteId.appendChild(document.createTextNode(this.SiteId));
        
        // Create the page id row
        var row_PageId = table.insertRow(table.rows.length);
        var label_PageId = row_PageId.insertCell(row_PageId.cells.length);
        label_PageId.className = 'LogEntryFieldLabel';
        var value_PageId = row_PageId.insertCell(row_PageId.cells.length);
        
        label_PageId.appendChild(document.createTextNode('Page Id'));
        value_PageId.appendChild(document.createTextNode(this.PageId));
        
        // Create the user id row
        var row_UserId = table.insertRow(table.rows.length);
        var label_UserId = row_UserId.insertCell(row_UserId.cells.length);
        label_UserId.className = 'LogEntryFieldLabel';
        var value_UserId = row_UserId.insertCell(row_UserId.cells.length);
        
        label_UserId.appendChild(document.createTextNode('User Id'));
        value_UserId.appendChild(document.createTextNode(this.UserId));
        
        // Create the function name row
        var row_FunctionName = table.insertRow(table.rows.length);
        var label_FunctionName = row_FunctionName.insertCell(row_FunctionName.cells.length);
        label_FunctionName.className = 'LogEntryFieldLabel';
        var value_FunctionName = row_FunctionName.insertCell(row_FunctionName.cells.length);
        
        label_FunctionName.appendChild(document.createTextNode('Function Name'));
        value_FunctionName.appendChild(document.createTextNode(this.FunctionName));
        
        // If any function arguments exist, iterate through arguments and add to table
        for(var i = 0; i < this.FunctionParams.length; i++)
        {
            var row_FunctionArg = table.insertRow(table.rows.length);
            var label_FunctionArg = row_FunctionArg.insertCell(row_FunctionArg.cells.length);
            var value_FunctionArg = row_FunctionArg.insertCell(row_FunctionArg.cells.length);    
            
            label_FunctionArg.appendChild(document.createTextNode('Input Parameter ' + (i + 1).toString()));
            label_FunctionArg.className = 'LogEntryFieldLabel';
            value_FunctionArg.appendChild(document.createTextNode(this.FunctionParams[i]));
        }
        
        // Create the message label row
        var row_MessageLabel = table.insertRow(table.rows.length);
        var label_Message = row_MessageLabel.insertCell(row_MessageLabel.cells.length);
        label_Message.className = 'LogEntryFieldLabel';
        label_Message.style.paddingTop = '10px';
        label_Message.colSpan = '2';
                
        label_Message.appendChild(document.createTextNode('Log Entry Message'));
        
        // Create the message value row
        var row_MessageValue = table.insertRow(table.rows.length);
        var value_Message = row_MessageValue.insertCell(row_MessageValue.cells.length);
        value_Message.colSpan = '2';
                
        var textField = Builder.node('textarea', { style: 'width:99%;height:60px' });
        
        var messageTemplate = new Template(Lingo.GetLingoItem(this.MessageKey));
        var messageValues = { ENTITY_TYPE: this.EntityType.capitalize(), ENTITY_ID: this.EntityId, SITE_ID: this.SiteId, PAGE_ID: this.PageId, USER_ID : this.UserId, FUNCTION_NAME: this.FunctionName };
        
        
        textField.appendChild(document.createTextNode(messageTemplate.evaluate(messageValues)));
        
        value_Message.appendChild(textField);
        
        // If the log type is 'EXCEPTION' and this.Exception is filled, create the info
        if((this.Type == 'EXCEPTION') && (this.Exception != null))
        {
            // Create the exception label row
            var row_ExceptionLabel = table.insertRow(table.rows.length);
            var label_Exception = row_ExceptionLabel.insertCell(row_ExceptionLabel.cells.length);
            label_Exception.className = 'LogEntryFieldLabel';
            label_Exception.style.paddingTop = '10px';
            label_Exception.colSpan = '2';
                    
            label_Exception.appendChild(document.createTextNode('Exception Message'));
            
            // Create the exception value row
            var row_ExceptionValue = table.insertRow(table.rows.length);
            var value_Exception = row_ExceptionValue.insertCell(row_ExceptionValue.cells.length);
            value_Exception.colSpan = '2';
                    
            var textField = Builder.node('textarea', { style: 'width:99%;height:60px' });
            textField.appendChild(document.createTextNode(this.Exception.message));
            
            value_Exception.appendChild(textField);
        }
        
        win.appendChild(table);
    },
    
    ShowMessage : function()
    {
        var messageTemplate = new Template(Lingo.GetLingoItem(this.MessageKey));
        var messageValues = { ENTITY_TYPE: this.EntityType.capitalize(), ENTITY_ID: this.EntityId, SITE_ID: this.SiteId, PAGE_ID: this.PageId, USER_ID : this.UserId, FUNCTION_NAME: this.FunctionName };
        
        var webMsg = new WebMessage(Lingo.GetLingoItem(this.Type.toUpperCase()), messageTemplate.evaluate(messageValues));
        
        if((this.Type.toUpperCase() == 'ALERT'))
        {
            webMsg.EnableAlertImage = true;
        }
        else if((this.Type.toUpperCase() == 'ERROR') || (this.Type.toUpperCase() == 'EXCEPTION'))
        {
            webMsg.EnableErrorImage = true;
        }
        else if((this.Type.toUpperCase() == 'NOTIFICATION'))
        {
            webMsg.EnableNotificationImage = true;
        }
        
        webMsg.Show();
    }
}