Ext.define('AuthorLoginWindow', {
    constructor:function (onClosedHandler, message, prompt, length, validationType) {
        this.OnClosedHandler = onClosedHandler;
        this.LoginMessage = message;
        this.LoginPrompt = prompt;
        this.LoginLength = length;
        this.ValidationType = validationType;
        this.IsLoginValid = false;

        this.LoginPanel = Ext.create('Ext.container.Container', {
            id:'LoginPanel',
            layout:{
                type:'vbox',
                align:'stretch'
            },
            items:[
                {
                    itemId:'loginMessage',
                    xtype:'label',
                    html:this.LoginMessage,
                    flex:1
                },
                {
                    itemId:'promptContainer',
                    xtype:'container',
                    layout:{
                        type:'hbox',
                        align:'middle'
                    },
                    items:[
                        {
                            itemId:'loginPrompt',
                            xtype:'label',
                            html:this.LoginPrompt
                        },
                        {
                            itemId:'loginInput',
                            xtype:'textfield',
                            style:{
                                "margin-left":'5px',
                                "margin-right":'5px'
                            },
                            flex:1
                        }
                    ]
                } ,
                {
                    itemId:'validationMessage',
                    xtype:'label'
                }
            ],
            flex:1
        });
        this.ButtonPanel = Ext.create('Ext.container.Container', {
            id:'ButtonPanel',
            style:'padding:5px;',
            layout:{
                type:'hbox',
                align:'middle'
            },
            items:[
                {
                    xtype:'label',
                    itemId:'validMessage',
                    style:{
                        color:'#f00',
                        "font-size":'8pt'
                    },
                    flex:1
                },
                {
                    xtype:'button',
                    style:"padding-right:3px",
                    text:'OK',
                    width:70,
                    handler:this.onOkClicked,
                    scope:this
                },
                {
                    xtype:'button',
                    text:'Cancel',
                    width:70,
                    handler:this.onCancelClicked,
                    scope:this
                }
            ]

        });
        this.LoginWindow = Ext.create('Ext.Window', {
            title:'Login',
            id:'LoginWindow',
            closeAction:'hide',
            layout:{
                type:'vbox',
                align:'stretch'
            },
            minWidth:350,
            minHeight:120,
            width:400,
            height:130,
            items:[this.LoginPanel, this.ButtonPanel],
            listeners:{
                close:this.onClosed,
                show:this.onShown,
                scope:this
            }
        });
    },
    setValidationMessage:function (message) {
        this.ButtonPanel.getComponent('validMessage').setText(message);
    },
    setLoginMessage:function (message) {
        this.LoginPanel.getComponent('loginMessage').setText(message);
    },
    setPrompt:function (message) {
        this.LoginPanel.getComponent('promptContainer').getComponent('loginPrompt').setText(message);
    },
    validateLogin:function (regex, text) {
        var re = new RegExp(regex);
      //  alert(regex + "\n" + text + "\n" + re.test(text));
        return re.test(text);
    },
    checkLogin:function () {
        var textInput = this.LoginPanel.getComponent('promptContainer').getComponent('loginInput');
        var text = textInput.getValue();

        var valid = false;
        var length = (this.LoginLength == 0 ? "*" : this.LoginLength);
        var prompt = "";
        switch (this.ValidationType) {
            case LoginValidationTypes.AnyChar:
                var pattern = "^." + (length == "*" ? "*" : ("{" + length + "}")) + "$";
                prompt = "Please enter " + (length == "*" ? "" : length + " characters");
                break;
            case LoginValidationTypes.Alphanumeric:
                var pattern = "^[\\w\\d]" + (length == "*" ? "*" : ("{" + length + "}")) + "$";
                prompt = "Please enter " + (length == "*" ? "alphanumeric characters" : length + " alphanumeric characters") + "."
                break;
            case LoginValidationTypes.Numeric:
                var pattern = "^\\d" + (length == "*" ? "*" : ("{" + length + "}")) + "$";
                prompt = "Please enter " + (length == "*" ? "numeric characters" : length + " numeric characters") + "."
                break;
            case LoginValidationTypes.Alphabetic:
                var length = (Author.login.idLength == 0 ? "*" : Author.login.idLength);
                var pattern = "^\\w" + (length == "*" ? "*" : ("{" + length + "}")) + "$";
                prompt = "Please enter " + (length == "*" ? "alphabetic characters" : length + " alphabetic characters") + "."
                break;
            case LoginValidationTypes.Email:
                var pattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$";
                prompt = "Please enter a valid Email address."
                break;
        }
        valid = this.validateLogin(pattern, text);
        if (valid) {
            return true;
        }
        else {
            this.setValidationMessage(prompt);
            textInput.focus(true, 100);
        }
    },
    onOkClicked:function () {
        if (this.checkLogin()) {
            var textInput = this.LoginPanel.getComponent('promptContainer').getComponent('loginInput');
            var userInput = textInput.getValue();
            Author.VariableManager.setSystemVariableValue(AuthorSystemVariables.USERID, userInput);

            this.IsLoginValid = true;
            this.LoginWindow.close();
        }
    },
    onCancelClicked:function () {
        this.LoginWindow.close();
    },
    onShown:function () {
        this.LoginWindow.center();
    },
    onClosed:function () {
        this.OnClosedHandler.call(this, this.IsLoginValid);
    },
    show:function () {
        this.LoginWindow.show();
    },
    isVisible:function () {
        return (this.LogWindow.isVisible());
    }
});
