package com.atmob.keyboard_android.util

import com.atmob.keyboard_android.component.ComponentMediator
import com.atmob.keyboard_android.component.root.IKeyboardRootComponent
import com.blankj.utilcode.util.ToastUtils

/**
 * 用户信息工具类
 */
object UserInfoHelper {
    /**
     * 是否已登录
     */
    fun isLogin(callback: (isLogin: Boolean) -> Unit) {
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
            ?.isLogin(onSuccess = {
                callback.invoke(it)
            }, onFail = {
                ToastUtils.showShort(it)
            })
    }

    /**
     * 是否为VIP
     */
    fun isVip(callback: (isVip: Boolean) -> Unit) {
        isLogin { isLogin ->
            // 已登录，再检查是否VIP
            if (isLogin) {
                KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
                    ?.isMember(onSuccess = {
                        callback.invoke(it)
                    }, onFail = {
                        ToastUtils.showShort(it)
                    })
            } else {
                // 未登录，则直接返回false
                callback.invoke(false)
            }
        }
    }

    /**
     * 已登录，才允许执行下一步
     */
    fun checkLogin(nextAction: () -> Unit) {
        isLogin {
            // 已登录，直接执行
            if (it) {
                nextAction.invoke()
            } else {
                // 未登录，切换到登录页
                val keyboardRootComponent =
                    ComponentMediator.findComponent(IKeyboardRootComponent::class.java)
                keyboardRootComponent?.switchLoginPage()
            }
        }
    }

    /**
     * 已经是VIP，才允许执行下一步
     */
    fun checkVip(nextAction: () -> Unit) {
        // 先检查是否登录
        checkLogin {
            // 再检查，是否是VIP
            isVip {
                // 已经是VIP，直接执行
                if (it) {
                    nextAction.invoke()
                } else {
                    // 未登录，切换到VIP页
                    val keyboardRootComponent =
                        ComponentMediator.findComponent(IKeyboardRootComponent::class.java)
                    keyboardRootComponent?.switchVipPage()
                }
            }
        }
    }
}