import { EventEmitter } from "events";
import dispatcher from "../dispatcher";

class Store extends EventEmitter {
    constructor() {
        super()
        this.access = { status: -91, msg: '' }
        this.users = []
        this.groups = []
        this.friends = []
        this.series = []
        this.gmestats = []
        this.serstats = {}
        this.hand = []
        this.msg = 'welcome'
        this.players = []
        this.color = ['grey', 'red', 'green', 'blue']
    }

    getAccess() { return this.access }
    getStatus() { return this.access.status }
    getGroups() { return this.groups }
    getFriends() { return this.friends }
    getUsers() { return this.users }
    getSeries() { return this.series }
    getGameStats() { return this.gmestats }
    getSeriesStats() { return this.serstats }
    getHand() { return this.hand }
    getPlayers() { return this.players }
    getMsg() { return this.msg }
    getToken() { return this.access.token }
    getCoin() { return this.color[this.coin] }

    updAccToken(data) {
        this.access = data
        this.emit("authChange")
    }

    updGroups(data) {
        this.groups = data.groups || []
        this.emit("groups")
    }

    updUsers(data) {
        this.users = data.users || []
        this.emit("users")
    }

    updFriends(data) {
        this.friends = data.friends || []
        this.emit("friends")
    }

    updGameStats(data) {
        this.gmestats = data.gmestats || []
        this.hand = data.crds || []
        this.emit("gmestats")
    }

    updSeries(data) {
        this.series = data.series || []
        this.emit("series")
    }

    updSeriesStats(data) {
        this.serstats = data.serstats || {}
        this.emit("serstats")
    }

    updPlayers(data) {
        this.players = data.players || []
        this.emit("playlist")
    }

    setError(error) {
        this.access = { auth: -5, msg: error, token: '' }
    }

    handleActions(action) {
        switch (action.type) {
            case "ACCTKN": {
                this.updAccToken(action.data)
                this.updSeries(action.data)
                this.updGroups(action.data)
                break
            }
            case "GRPNEW": 
            case "GRPDLT": {
                this.updGroups(action.data)
                break;
            }
            case "GRFRND": {
                this.updFriends(action.data)
                break
            }
            case "USRLST": {
                this.updUsers(action.data)
                break
            }
            case "NEWSER": {
                this.updSeries(action.data)
                break
            }
            //case "NEWGAM": {
            //    this.updPlayers(action.data)
            //    break
            //}
            case "SERDLT":
            case "SERRFH": {
                this.updSeries(action.data)
                break
            }
            case "LOGOUT": {
                this.updAccToken(action.data)
                break
            }
            case "GMSTAT": {
                this.updSeriesStats(action.data)
                this.updGameStats(action.data)
                break
            }
            case "SRSTAT": {
                this.updSeriesStats(action.data)
                break
            }
            case "JOINGM": {
                this.updPlayers(action.data)
                this.updSeriesStats(action.data)
                break
            }
            case "API_ERR": {
                this.setError(action.error)
                break
            }
        }
    }
}

const store = new Store()
dispatcher.register(store.handleActions.bind(store))
export default store