const api = require('../localUserAPI')

const load = async (senderID, keys, api) => {
    const oldState = await api.getState(senderID)
    //contact that user for the first time
    if (oldState === null) {
        await api.setState(senderID, { state: "initial", done: 0, round: 0, keysLeftForThatUser: keys, welcomed: false, userScore: 0 })
    }
    let state = await api.getState(senderID)

    return new User(senderID, state)
}

class User {
    constructor(senderID, state, api) {
        this.senderID = senderID
        this.state = state
        this.api = api
    }

    setState(updateState) {
        const newState = Object.assign({}, this.state, updateState)
        this.api.setState(this.senderID, newState)
        this.state = newState
    }

    startQuiz(questionKey) {
        this.setState({
            keysLeftForThatUser: this.state.keysLeftForThatUser,
            currentQuestionKey: questionKey,
            done: this.state.done,
            round: this.state.round,
            state: 'playing',
            userScore: this.state.userScore
        })
    }

    setStateWelcome(stateWelcome) {
        this.api.setStateWelcome(this.senderID, stateWelcome)
        this.stateWelcome = stateWelcome
    }

    setRound(round) {
        this.api.setRound(this.senderID, round)
        this.state.round = round
    }

    welcome() {
        this.api.setStateWelcome(this.senderID, true)
        this.state.welcomed = true
    }

    playing(keysLeftForThatUser) {
        this.setState(this.senderID,
            { state: 'playing', keysLeftForThatUser: keysLeftForThatUser, 
            welcomed: true }
        )
        // this.state.state = 'playing'
        // this.state.welcomed = true
        // this.state.keysLeftForThatUser = keysLeftForThatUser
    }

    pause() {
        this.setState(this.senderID,
            { state: 'pause', welcomed: true }
        )
        // this.state.state = 'pause'
        // this.state.welcomed = true
    }

    resume() {
        this.setState({ state: 'playing' })
        // api.setState(this.senderID,
        //     { state: 'playing', done: this.state.done, round: this.state.round, keysLeftForThatUser: this.state.keysLeftForThatUser, 
        //     welcomed: this.state.welcomed, userScore: this.state.userScore }
        // )
        // this.state.state = 'playing'
    }

    finish() {
        this.setState({ state: 'finish', welcomed: true, done: 0, userScore: 0 })
        // api.setState(this.senderID,
        //     { state: 'finish', done: 0, round: this.state.round, keysLeftForThatUser: this.state.keysLeftForThatUser, 
        //     welcomed: this.state.welcomed, userScore: 0 }
        // )
        // this.state.state = 'finish'
        // this.state.welcomed = true
    }

    nextRound(keysLeftForThatUser) {
        this.setState({state: "playing", done :0, userScore: 0, keysLeftForThatUser: keysLeftForThatUser})
        // api.setState(this.senderID,
        //     { state: 'playing', done: 0, round: this.state.round, keysLeftForThatUser: keysLeftForThatUser, 
        //     welcomed: this.state.welcomed, userScore: 0 }
        // )
        // this.state.keysLeftForThatUser = keysLeftForThatUser
        // this.state.state = 'playing'
        // this.state.welcomed = true
    }

}

module.exports = { load }


@@ -1078,32 +1078,35 @@
    %7D)%0A    %7D%0A%0A   
+ //
  setStateWelcome
@@ -1121,24 +1121,27 @@
 lcome) %7B%0A   
+ //
      this.ap
@@ -1186,24 +1186,27 @@
 Welcome)%0A   
+ //
      this.st
@@ -1230,24 +1230,27 @@
 eWelcome%0A   
+ //
  %7D%0A%0A    setR

test state