<commit_before>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

const DialogBox = require('../DialogBox');
const storageManager = require('../../StorageManager');
const socketManager = require('../../SocketManager');
const eventCentral = require('../../EventCentral');
const soundLibrary = require('../../audio/SoundLibrary');

class LoginBox extends DialogBox {
  constructor({ description, extraDescription, parentElement, closeFunc }) {
    const buttons = {
      left: {
        text: 'Registrera',
        eventFunc: () => {
          const reenterPasswordInput = this.inputs.find(({ inputName }) => inputName === 'reenterPassword');

          if (!reenterPasswordInput) {
            this.addInput({
              placeholder: 'Re-enter your password',
              inputName: 'reenterPassword',
              type: 'password',
              required: true,
              extraClass: 'markedInput',
            });
            this.markEmptyFields();
            this.focusInput('reenterPassword');

            return;
          }

          const emptyFields = this.markEmptyFields();

          if (emptyFields) {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['You cannot leave obligatory fields empty!'] });

            return;
          }

          if (reenterPasswordInput.inputElement.value === this.inputs.find(({ inputName }) => inputName === 'password').inputElement.value) {
            socketManager.emitEvent('register', {
              user: {
                userName: this.inputs.find(({ inputName }) => inputName === 'userName').inputElement.value,
                password: <PASSWORD>,
                registerDevice: storageManager.getDeviceId(),
              },
            }, ({ error, data }) => {
              if (error) {
                soundLibrary.playSound('fail');
                this.changeExtraDescription({ text: ['Something went wrong. Failed to register user'] });

                return;
              }

              const text = [];

              if (data.requiresVerification) {
                text.push('Your user has been registered, but your account is not yet active. You need to contact an administrator to get your account verified!');
              } else {
                text.push('Your user has been registered! You may now access O3C');
              }

              this.changeExtraDescription({ text });
              this.clearInput('userName');
              this.clearInput('password');
              this.removeInput('reenterPassword');
              this.focusInput('userName');
            });
          } else {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['Passwords do not match. Try again'] });
            this.clearInput('password');
            this.clearInput('reenterPassword');
            this.focusInput('password');
          }
        },
      },
      right: {
        text: 'Logga in',
        eventFunc: () => {
          const emptyFields = this.markEmptyFields();

          if (emptyFields) {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['You cannot leave obligatory fields empty!'] });

            return;
          }

          socketManager.emitEvent('login', {
            user: {
              userName: this.inputs.find(({ inputName }) => inputName === 'userName').inputElement.value,
              password: this.inputs.find(({ inputName }) => inputName === 'password').inputElement.value,
            },
          }, ({ error, data }) => {
            if (error) {
              soundLibrary.playSound('fail');

              switch (error.type) {
                case 'needs verification': {
                  this.changeExtraDescription({ text: ['Your account may need to be verified. Please contact an administrator.', 'Unable to login'] });

                  break;
                }
                case 'does not exist': {
                  this.changeExtraDescription({ text: ['Incorrect user name and/or password'] });

                  break;
                }
                default: {
                  this.changeExtraDescription({ text: ['Something went wrong. Failed to log in'] });

                  break;
                }
              }

              return;
            }

            storageManager.setUserName(data.user.userName);
            storageManager.setAccessLevel(data.user.accessLevel);
            storageManager.setTeam(data.user.team);
            eventCentral.triggerEvent({ event: eventCentral.Events.ALIAS, params: { aliases: data.user.aliases } });
            eventCentral.triggerEvent({ event: eventCentral.Events.LOGIN });
            this.removeView();
          });
        },
      },
    };
    const inputs = [{
      placeholder: '<NAME>',
      inputName: 'userName',
      isRequired: true,
    }, {
      placeholder: 'Password',
      inputName: 'password',
      type: 'password',
      isRequired: true,
    }];

    super({
      buttons,
      description,
      extraDescription,
      parentElement,
      inputs,
      closeFunc,<commit_msg>User name max 10 chars<commit_after>/*
 Copyright 2017 <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

const DialogBox = require('../DialogBox');
const storageManager = require('../../StorageManager');
const socketManager = require('../../SocketManager');
const eventCentral = require('../../EventCentral');
const soundLibrary = require('../../audio/SoundLibrary');

class LoginBox extends DialogBox {
  constructor({ description, extraDescription, parentElement, closeFunc }) {
    const buttons = {
      left: {
        text: 'Registrera',
        eventFunc: () => {
          const reenterPasswordInput = this.inputs.find(({ inputName }) => inputName === 'reenterPassword');

          if (!reenterPasswordInput) {
            this.addInput({
              placeholder: 'Re-enter your password',
              inputName: 'reenterPassword',
              type: 'password',
              required: true,
              extraClass: 'markedInput',
            });
            this.markEmptyFields();
            this.focusInput('reenterPassword');

            return;
          }

          const emptyFields = this.markEmptyFields();

          if (emptyFields) {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['You cannot leave obligatory fields empty!'] });

            return;
          }

          if (reenterPasswordInput.inputElement.value === this.inputs.find(({ inputName }) => inputName === 'password').inputElement.value) {
            socketManager.emitEvent('register', {
              user: {
                userName: this.inputs.find(({ inputName }) => inputName === 'userName').inputElement.value,
                password: <PASSWORD>,
                registerDevice: storageManager.getDeviceId(),
              },
            }, ({ error, data }) => {
              if (error) {
                soundLibrary.playSound('fail');
                this.changeExtraDescription({ text: ['Something went wrong. Failed to register user'] });

                return;
              }

              const text = [];

              if (data.requiresVerification) {
                text.push('Your user has been registered, but your account is not yet active. You need to contact an administrator to get your account verified!');
              } else {
                text.push('Your user has been registered! You may now access O3C');
              }

              this.changeExtraDescription({ text });
              this.clearInput('userName');
              this.clearInput('password');
              this.removeInput('re<PASSWORD>Password');
              this.focusInput('userName');
            });
          } else {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['Passwords do not match. Try again'] });
            this.clearInput('password');
            this.clearInput('reenterPassword');
            this.focusInput('password');
          }
        },
      },
      right: {
        text: 'Logga in',
        eventFunc: () => {
          const emptyFields = this.markEmptyFields();

          if (emptyFields) {
            soundLibrary.playSound('fail');
            this.changeExtraDescription({ text: ['You cannot leave obligatory fields empty!'] });

            return;
          }

          socketManager.emitEvent('login', {
            user: {
              userName: this.inputs.find(({ inputName }) => inputName === 'userName').inputElement.value,
              password: this.inputs.find(({ inputName }) => inputName === 'password').inputElement.value,
            },
          }, ({ error, data }) => {
            if (error) {
              soundLibrary.playSound('fail');

              switch (error.type) {
                case 'needs verification': {
                  this.changeExtraDescription({ text: ['Your account may need to be verified. Please contact an administrator.', 'Unable to login'] });

                  break;
                }
                case 'does not exist': {
                  this.changeExtraDescription({ text: ['Incorrect user name and/or password'] });

                  break;
                }
                default: {
                  this.changeExtraDescription({ text: ['Something went wrong. Failed to log in'] });

                  break;
                }
              }

              return;
            }

            storageManager.setUserName(data.user.userName);
            storageManager.setAccessLevel(data.user.accessLevel);
            storageManager.setTeam(data.user.team);
            eventCentral.triggerEvent({ event: eventCentral.Events.ALIAS, params: { aliases: data.user.aliases } });
            eventCentral.triggerEvent({ event: eventCentral.Events.LOGIN });
            this.removeView();
          });
        },
      },
    };
    const inputs = [{
      placeholder: '<NAME>',
      inputName: 'userName',
      isRequired: true,
      maxLength: 10,
    }, {
      placeholder: 'Password',
      inputName: 'password',
      type: 'password',
      isRequired: true,
    }];

    super({
      buttons,
      description,
      extraDescription,
      parentElement,
      inputs,
      closeFunc,
{'fraction_non_alphanumeric': 0.11788510600862145, 'fraction_numerical': 0.001935427113574382, 'mean_word_length': 1.886011678090886, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '812881', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2632, 'n_words': 727}