package net.taehui.twilight.system

import net.taehui.twilight.Avatar
import net.taehui.twilight.Logger
import java.util.concurrent.ConcurrentHashMap

object AvatarHandler {
    private val avatars = ConcurrentHashMap<String, Avatar>()

    fun justNotify(text: String) {
        avatars.values.forEach { avatar ->
            Translator.translate(avatar.language, text, avatar).thenAccept {
                avatar.send(EventOuterClass.Event.EventID.NOTIFY_INFO, it)
            }
        }
    }

    fun doInfo(logger: Logger) {
        avatars.values.filter { !it.isAwilight }.forEach { logger.logInfo(it.toString()) }
    }

    fun getAvatar(avatarID: String): Avatar? {
        return avatars[avatarID]
    }

    fun establish(avatar: Avatar, avatarID: String, qwilightName: String, isValve: Boolean, isPause: Boolean) {
        if (!isPause) {
            avatar.setEstablished(avatarID, qwilightName, isValve)
        }
        avatars[avatar.avatarID] = avatar
    }

    fun handleSignIn(
        avatar: Avatar,
        totem: String,
        avatarID: String,
        avatarName: String,
        avatarCompetence: Int
    ) {
        synchronized(avatarID.intern()) {
            handleNotSignIn(avatarID)
            val avatarIDBefore = avatar.avatarID
            avatar.handleSignIn(totem, avatarID, avatarName, avatarCompetence)
            avatars.remove(avatarIDBefore)?.let { avatars.put(avatarID, it) }
        }
    }

    fun handleNotSignIn(avatarID: String) {
        avatars.remove(avatarID)?.let {
            it.handleNotSignIn()
            avatars[it.avatarID] = it
        }
    }

    fun quitAvatar(avatarID: String) {
        avatars.remove(avatarID)?.let {
            it.doIfAvailable {
                if (it.isSignedIn) {
                    it.handleNotSignIn()
                } else {
                    SiteHandler.quitAvatar(it)
                }
                it.isAvailable = false
            }
        }
    }

    fun pause(text: String) {
        avatars.values.forEach { avatar ->
            Translator.translate(avatar.language, text, avatar).thenAccept {
                avatar.send(EventOuterClass.Event.EventID.CLOSE, it)
                avatar.close()
            }
        }
    }

    fun sendInvalidateAvatarTitle(avatarID: String) {
        avatars.values.forEach {
            it.send(
                EventOuterClass.Event.EventID.INVALIDATE_AVATAR_TITLE,
                avatarID
            )
        }
    }

    fun sendInvalidateAvatarDrawing(avatarID: String) {
        avatars.values.forEach {
            it.send(
                EventOuterClass.Event.EventID.INVALIDATE_AVATAR_DRAWING,
                avatarID
            )
        }
    }

    fun sendInvalidateAvatarEdge(avatarID: String) {
        avatars.values.forEach {
            it.send(
                EventOuterClass.Event.EventID.INVALIDATE_AVATAR_EDGE,
                avatarID
            )
        }
    }

    fun sendClose(text: String) {
        avatars.values.forEach {
            it.send(
                EventOuterClass.Event.EventID.CLOSE,
                text
            )
        }
    }
}