package com.adealink.weparty.account

import androidx.fragment.app.FragmentManager
import com.adealink.frame.spi.RegisterService
import com.adealink.weparty.account.login.LoginDialog
import com.adealink.weparty.account.login.manager.loginManager
import com.adealink.weparty.module.account.Account
import com.adealink.weparty.module.account.IAccountService
import com.adealink.weparty.module.account.ILoginListener

@RegisterService(value = IAccountService::class)
class AccountServiceImpl : IAccountService {
    override val uid: String
        get() = loginManager.uid

    override val token: String
        get() = loginManager.token

    override fun showLoginDialog(fm: FragmentManager?) {
        fm ?: return
        val fragment = fm.findFragmentByTag(Account.LoginDialog.PATH)
        if (fragment != null && fragment.isAdded) {
            return
        }
        LoginDialog().show(fm, Account.LoginDialog.PATH)
    }

    override fun logout(toLogin: Boolean?) {
        loginManager.logout(toLogin)
    }

    override fun isLogin(): Boolean {
        return loginManager.isLogin
    }

    override fun registerListener(l: ILoginListener) {
        loginManager.addListener(l)
    }

    override fun unregisterListener(l: ILoginListener) {
        loginManager.removeListener(l)
    }

    override fun deleteAccount() {
        loginManager.deleteAccount()
    }

    override fun isNewRegister(): Boolean {
        return loginManager.isNewRegister()
    }

    override fun refreshToken() {
        loginManager.refreshToken()
    }

    override fun checkRemoteVirtualAppConfig() {
        loginManager.checkRemoteVirtualAppConfig()
    }

    override fun onTokenExpire() {
        loginManager.onTokenExpire()
    }

    override fun getService(): IAccountService {
        return this
    }

    override fun logout() {

    }
}