﻿if (!window.wesreid)
    window.wesreid = {};

window.wesreid.testing = {};
window.wesreid.testing.framework = {
    createTestingStructure: function (testObj, onsubmitFn) {
        return {
            testobj: testObj,
            fncall: onsubmitFn
        };
    },
    ui: {
        contentElement: null,

        uiForm: function (test, contentElement, contentFormName, headingElement, resourceName) {
            if (!contentElement && !ui.contentElement)
                return;
            $(contentElement).html('');
            if (contentFormName) {
                if (headingElement) {
                    $(headingElement).text(contentFormName);
                }
                contentFormName = resourceName + 'Form';
            }
            else {
                contentFormName = 'uiform_' + Math.random();
            }

            // Private
            var that = this;
            var writeToDom = function (uieltype, labelName, elName, elValue, jqueryElement) {
                var htmlFormRow = '';
                if (elValue == null || elValue == undefined)
                    elValue = '';
                var type = '';
                switch (typeof (elValue)) {
                    case "object":
                    case "function":
                    case "undefined":
                    case "string":
                        type = 'string';
                        break;
                    case "boolean":
                        type = 'bool';
                        break;
                    case "number":
                        type = 'num';
                        break;
                    default:
                        type = typeof (elValue);
                        break;
                }
                switch (uieltype) {
                    case that.uiElType.section:
                        htmlFormRow += '<div style="clear:both;" class="formSection"><div>' + labelName + '</div></div>';
                        break;
                    case that.uiElType.textBox:
                        htmlFormRow += '<div style="clear:both;" class="formRowContainer formTextBox"><div style="float:left;">' + labelName + '</div><div style="float:left;"><input type="text" id="frm' + elName.replace(".", "_") + '" name="' + elName + '" value="' + elValue + '" dataType="' + type + '"/></div></div>';
                        break;
                    case that.uiElType.selectMenu:
                        htmlFormRow += '<div style="clear:both;" class="formRowContainer formSelectMenu"><div style="float:left;">' + labelName + '</div><div style="float:left;"><input type="text" id="frm' + elName.replace(".", "_") + '" name="' + elName + '" value="' + elValue + '" dataType="' + type + '"/></div></div>';
                        break;
                    case that.uiElType.checkBox:
                        htmlFormRow += '<div style="clear:both;" class="formRowContainer formCheckBox"><div style="float:left;">' + labelName + '</div><div style="float:left;"><input type="checkbox" id="frm' + elName.replace(".", "_") + '" name="' + elName + '" value="' + elValue + '" dataType="' + type + '"/></div></div>';
                        break;
                    case that.uiElType.radioButtonGroup:
                        htmlFormRow += '<div style="clear:both;" class="formRowContainer formRadioButton"><div style="float:left;">' + labelName + '</div><div style="float:left;"><input type="text" id="frm' + elName.replace(".", "_") + '" name="' + elName + '" value="' + elValue + '" dataType="' + type + '"/></div></div>';
                        break;
                }
                $(jqueryElement).append(htmlFormRow);
            };
            this.buildObjUi = function (obj, jqueryElement, objStringPrefix, formName, resourceName) {
                try {
                    var formFnCall = obj.fncall || null;
                    obj = obj.testobj || obj;
                    objStringPrefix = objStringPrefix || '';
                    if (objStringPrefix == '') {
                        var frmobj = $('<form class="wrUiForm" id="' + formName + '" name="' + formName + '" resourceName="' + resourceName + '"></form>');
                        $(jqueryElement).append(frmobj);
                        jqueryElement = frmobj;
                    }
                    for (var key in obj) {
                        switch (typeof (obj[key])) {
                            case "object":
                                var childElement = $('<ul class="formGroup"></ul>');
                                writeToDom(that.uiElType.section, key, null, null, jqueryElement);
                                that.buildObjUi(obj[key], childElement, objStringPrefix + key + '.', formName);
                                $(jqueryElement).append(childElement);
                                break;
                            case "string":
                            case "number":
                                writeToDom(that.uiElType.textBox, key, objStringPrefix + key, obj[key], jqueryElement);
                                break;
                            case "boolean":
                                writeToDom(that.uiElType.checkBox, key, objStringPrefix + key, obj[key], jqueryElement);
                                break;
                            default:
                                break;
                        }
                    }
                    if (objStringPrefix == '') {
                        $(jqueryElement).append('<div style="clear:both;"><input id="testFormBtn" type="button" value="Test Form"/></div>');
                        $('#testFormBtn', $(jqueryElement)).click(formFnCall);
                    }
                }
                catch (ex) {
                    console.log(ex.Message + '\nline: ' + ex.Line);
                }
            };

            // Public
            this.container = contentElement;
            this.formName = contentFormName;
            this.uiElType = {
                section: 0,
                textBox: 1,
                selectMenu: 2,
                checkBox: 3,
                radioButtonGroup: 4
            };
            this.buildObjUi(test, contentElement, null, contentFormName, resourceName);
        }

    }

};
