import { saveUser } from './api/users'


const validateUsername = username => username.match(/^[a-zа-я ]{1,80}$/ig)
const validateCity = city => city.match(/^[a-zа-я]{1,60}$/ig)
const validateTwitter = twitter => twitter.match(/^[a-z]{1,60}$/ig)

const STEPS = {
    1: {
        question: 'Как тебя зовут?',
        dataModel: 'firstName',
        validateFunction: validateUsername
    },
    2: {
        question: 'Из какого ты города?',
        dataModel: 'city',
        validateFunction: validateCity
    },
    3: {
        question: 'Введи свой юзернейм в Твиттере',
        dataModel: 'twiiter',
        validateFunction: validateTwitter
    },
}

const dataStorage = {}

const getCurrentStep = telegramId => dataStorage[telegramId].step
const getCurrentQuestion = telegramId => STEPS[getCurrentStep(telegramId)].question
const getValidateFunction = telegramId => STEPS[getCurrentStep(telegramId)].validateFunction

function updateData (telegramId, data) {
    const newUserData = Object.assign(dataStorage[telegramId].data, {
        [STEPS[getCurrentStep(telegramId)].dataModel]: data,
    })
    dataStorage[telegramId] = Object.assign(
        dataStorage[telegramId], {
            data: newUserData,
        }
    )
    dataStorage[telegramId].step = getCurrentStep(telegramId) + 1

    console.log(dataStorage)
    return dataStorage.telegramId
}

function initializeSignup (telegramId) {
    dataStorage[telegramId] = {
        step: 1,
        data: {
            id: telegramId,
        },
    }
}

function sendMessageToBot (_telegramId) {
    const telegramId = _telegramId
    return (message) => {
        if (!message || !telegramId) {
            return false
        }

        const status = getCurrentStep(telegramId) > 3

        return {
            status,
            message,
        }
    }
}

/**
 * Use this to get to know,
 * if data sent by user is valid
 * @param _telegram {String}
 * @return true/false {Boolean}
*/
const dataValidator = _telegramId => {
    const telegramId = _telegramId
    return data => getValidateFunction(telegramId)(data)
}

export default function signup (telegramId, message) {
    const sendMessage = sendMessageToBot(telegramId)
    const isValidData = dataValidator(telegramId)
    message = message.trim()

    if (message === 'Зарегистрироваться') {
        initializeSignup(telegramId)
        return sendMessage(getCurrentQuestion(telegramId))
    }

    if (!isValidData(message)) {
        return sendMessage('Кажется, введены неправильные данные. Попробуй ещё раз')
    }

    updateData(telegramId, message.trim())

    if (getCurrentStep(telegramId) === 4) {
        try {
            saveUser(dataStorage[telegramId].data)
            return sendMessage('Регистрация успешно завершена!')
        } catch (e) {
            return sendMessage('Ошибка при регистрации')
        }
    }

    return sendMessage(getCurrentQuestion(telegramId))
}

@@ -33,17 +33,16 @@
 users'%0A%0A
-%0A
 const va
@@ -225,16 +225,19 @@
 h(/%5E%5Ba-z
+0-9
 %5D%7B1,60%7D$
@@ -378,16 +378,17 @@
 Username
+,
 %0A    %7D,%0A
@@ -503,16 +503,17 @@
 dateCity
+,
 %0A    %7D,%0A
@@ -603,16 +603,16 @@
 iiter',%0A
-
         
@@ -644,16 +644,17 @@
 eTwitter
+,
 %0A    %7D,%0A

Edit twitter validation funciton