(function (paw, utils, Popup, Settings2FaFormComponent) {
  function Fa2Base(settings, localization) {
    var self = this;
    this.settings = paw.lang.append(
      {
        $tokenValidityInput: undefined,
        onFa2StatusChange: undefined,
        userData2FaApiUrl: undefined,
        send2FaCodeApiUrl: undefined,
        submit2FaApiUrl: undefined,
        onValid: undefined,
        onReset: undefined,
      },
      settings
    );

    this.localization = paw.lang.append(
      {
        loginMfa: "@Loc.LoginMfa",
        login: "@Loc.Login",
        phoneNumber: "@Loc.PhoneNumber",
        email: "@Loc.Email",
        token: "@Loc.Token",
        sendCode: "@Loc.SendToken",
        actionAuthValid: "@Loc.ActionAuthValid",
        mfaPaymentRequired: "@Loc.MfaPaymentRequired",
        order: "@Loc.OrderMfaEmessage",
        usiteUrl: "@Loc.UserSiteUrl",
        cancel: "@Loc.Cancel",
        noMfaContactData: "@Loc.NoMfaContactData",
        sendTo: "@Loc.SendTo",
        viber: "@Loc.Viber",
      },
      localization || {}
    );

    this.fa2Data = {};

    this.successMessageAfterCompleted = false;

    this.fa2Completed = false;
    this.fa2Failed = false;
    this.popup = new Popup("2fa-login", {
      title: this.localization.loginMfa,
      Component: Settings2FaFormComponent,
      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,
          encParamAuth: false,
          fa2Completed: function () {
            self.fa2Completed = true;
            if (self.settings.onFa2StatusChange) {
              self.settings.onFa2StatusChange(true);
            }
            self.popup.hide();
            self.settings.onValid();

            if (self.successMessageAfterCompleted) {
              self.showSuccessMessage();
            }
          },
          fa2Failed: function () {
            self.fa2Completed = false;
            self.fa2Failed = true;

            if (self.settings.onFa2StatusChange) {
              self.settings.onFa2StatusChange(false);
            }
          },
          authModel: function () {
            return self.authModel();
          },
        },
      },
      onClose: function () {
        self.destroy();
        if (!self.fa2Failed && !self.fa2Completed && self.settings.onReset) {
          self.settings.onReset();
        }
      },
    });
  }

  Fa2Base.prototype.init = function () {
    this.fa2Completed = false;
    this.fa2Failed = false;
  };

  Fa2Base.prototype.destroy = function () {
    this.fa2Data = {};
  };

  Fa2Base.prototype.checkFa2 = function () {
    var self = this;

    if (this.fa2Completed) {
      return !this.fa2Completed;
    }

    return this.requestFa2().then(function (resp) {
      if (!resp.IsSucess) {
        self.fa2Completed = false;
        self.showCheckedErrorMessage(resp.Message);
        return !self.fa2Completed;
      }
      if (self.onFa2Requested(resp)) {
        self.popup.show();
      }
      return !self.fa2Completed;
    });
  };

  Fa2Base.prototype.onFa2Requested = function () {
    throw "Must be implemented by child";
  };

  Fa2Base.prototype.requestFa2 = function () {
    throw "Must be implemented by child";
  };

  Fa2Base.prototype.onCheckedError = function () {};

  Fa2Base.prototype.showCheckedErrorMessage = function (message) {
    var self = this;
    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>' +
            self.translateErrorMessage(message) +
            "</span></div>",
        ];
      },
      onClose: function () {
        self.onCheckedError();

        if (!utils.value.isSet(self.settings.$submit)) {
          return;
        }
        self.settings.$submit.attr("disabled", false);
        self.settings.$submit.find("i").addClass("hidden");
      },
    });
    popup.show();
  };

  Fa2Base.prototype.showSuccessMessage = function () {
    var self = this;

    if (!this.fa2Completed) {
      return;
    }

    var popup = new Popup("2fa-success", {
      type: "success",
      styleClass: "paw-2fa-error-message",
      maximizable: false,
      markupDef: function () {
        return [
          '<div class="paw-2fa-error-content"><span>' +
            self.localization.actionAuthValid +
            "</span></div>",
        ];
      },
    });
    popup.show();
  };

  Fa2Base.prototype.reset = function () {
    this.fa2Completed = false;
  };

  Fa2Base.prototype.authModel = function () {
    return {};
  };

  Fa2Base.prototype.translateErrorMessage = function (msg) {
    switch (msg) {
      case "NO_MFA_CONTACT_DATA":
        return this.localization.noMfaContactData;
    }
    return msg;
  };

  paw.com.fa2.Fa2Base = Fa2Base;
})(paw, paw.utils, paw.com.Popup, paw.com.fa2.Fa2FormComponent);
