(function(paw, utils, Popup, Login2FaFormComponent){
    function Login2Fa(settings, localization){
        var self = this;
        this.settings = paw.lang.append({
            $form: undefined,
            $username: undefined,
            $password: undefined,
            $domain: undefined,
            $submit: undefined,
            loginCheck: undefined,
            check2FaApiUrl: undefined,
            send2FaCodeApiUrl: undefined,
            submit2FaApiUrl: undefined
        }, settings);

        this.localization = paw.lang.append({
            loginMfa: '@Loc.LoginMfa',
            login: '@Loc.Login',
            phoneNumber: '@Loc.PhoneNumber',
            token: '@Loc.Token',
            sendCode: '@Loc.SendToken'
        }, localization || {});

        this.fa2Data = {};

        this.fa2Completed = false;
        this.popup = new Popup('2fa-login', {
            title: this.localization.loginMfa,
            maximizable: false,
            Component: Login2FaFormComponent,
            styleClass: 'paw-2fa-login-popup paw-popup-header-nodivider',
            bind: {
                fa2Data: this.fa2Data,
                localization: this.localization,
                settings: {
                    $form: this.settings.$form,
                    send2FaCodeApiUrl: this.settings.send2FaCodeApiUrl,
                    submit2FaApiUrl: this.settings.submit2FaApiUrl,
                    fa2Completed: function(){
                        self.fa2Completed = true;
                        self.settings.$form.submit();
                        self.popup.hide();
                    },
                    fa2Failed: function(){
                        self.fa2Completed = false;
                        self.settings.loginCheck(false);
                    },
                    loginModel: function(){
                        return self.loginModel();
                    }
                }
            },
            onClose: function(){
                self.destroy();
            }
        });
    }

    Login2Fa.prototype.init = function(){

    };

    Login2Fa.prototype.destroy = function(){
        if(!this.fa2Completed){
            this.settings.$submit.attr('disabled', false);
            this.settings.$submit.find('i').addClass('hidden');
        }
    };

    Login2Fa.prototype.check2Fa = function () {
        var self = this;

        if(this.fa2Completed){
            return !this.fa2Completed;
        }
        
        return utils.async.post(this.settings.check2FaApiUrl, this.loginModel()).then(function (resp) {
            if(!resp.IsSucess){
                self.fa2Completed = false;
                var popup = new Popup('2fa-error', {
                    type: 'error',
                    styleClass: 'paw-2fa-error-message',
                    maximizable: false,
                    markupDef: function(){
                        return ['<div class="paw-2fa-error-content"><span>' + resp.Message + '</span></div>']
                    },
                    onClose: function(){
                        self.settings.$submit.attr('disabled', false);
                        self.settings.$submit.find('i').addClass('hidden');
                    }
                });
                popup.show();
            }
            else if(resp.Required){
                self.fa2Data.phoneNumber = resp.PhoneNumber;
                self.fa2Data.resendLockDuration = resp.ResendLockDuration
                self.popup.show();
            }
            else{
                self.fa2Completed = true;
            }            

            return !self.fa2Completed;
        });
    };

    Login2Fa.prototype.reset = function () {
        this.fa2Completed = false;
    };
    
    Login2Fa.prototype.loginModel = function () {
        return {
            Username: this.settings.$username.val(),
            Password: this.settings.$password.val(),
            DomainName: this.settings.$domain.val()
        };
    };

    paw.com.login.fa2.Login2Fa = Login2Fa;
})(paw, paw.utils, paw.com.Popup, paw.com.login.fa2.Login2FaFormComponent);