package com.ccwangluo.accelerator.utils

import android.content.Context
import androidx.lifecycle.lifecycleScope
import com.ccwangluo.accelerator.model.LoginRes
import com.ccwangluo.accelerator.ui.login.PhoneLoginFragment
import com.ccwangluo.accelerator.utils.http.HttpGo
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.core.PageOption
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.*


object LoginUtils {

    var loginRes: LoginRes? = null
    var timeNumber = 60

    private var timer: Timer? = null
    private var timerTask: TimerTask? = null

    private var timerCallBack: (Int) -> Unit? = {}

    fun registerTimerCallBack(callBack: (Int) -> Unit) {
        timerCallBack = callBack
    }

    fun unRegisterTimerCallBack() {
        timerCallBack = {}
    }


    fun startTimer() {
        timer?.cancel()
        timerTask = object : TimerTask() {
            override fun run() {
                timeNumber--
                timerCallBack(timeNumber)
                if (timeNumber == 0) {
                    timer?.cancel()
                }
            }
        }
        timer = Timer();
        timer?.schedule(timerTask, 0, 1000);
    }

    fun login(xPageFragment: XPageFragment, callBack: (Boolean) -> Unit) {
        PhoneUtils.openLoginAuth(xPageFragment) { b, s ->
            if (b) {
                val map = mutableMapOf<String, String>()
                map.put("token", s)
                xPageFragment.lifecycleScope.launch(Dispatchers.IO) {
                    val res = HttpGo.postSync<LoginRes>("/user/flash/login", map)
                    res?.let {
                        if (it.code == 200) {
                            token = it.data?.token
                            loginRes = it.data
                            ToastUtils.show("登录成功")
                            OneKeyLoginManager.getInstance().finishAuthActivity()
                            withContext(Dispatchers.Main) {
                                callBack(true)
                            }
                        } else {
                            it.error?.let {
                                ToastUtils.show(it)
                            }
                            withContext(Dispatchers.Main) {
                                callBack(false)
                            }
                        }
                    }
                }
            } else {
                PageOption.to(PhoneLoginFragment::class.java).open(xPageFragment)
            }
        }
    }

    private var sToken: String? = null
    private const val KEY_TOKEN = "com.ccwangluo.KEY_TOKEN"

    /**
     * 初始化Token信息
     */
    fun init(context: Context?) {
        sToken = MMKV.defaultMMKV().decodeString(KEY_TOKEN, "")
    }

    fun clearToken() {
        sToken = null
        MMKV.defaultMMKV().remove(KEY_TOKEN)
    }

    var token: String?
        get() = sToken
        set(token) {
            sToken = token
            MMKV.defaultMMKV().putString(KEY_TOKEN, token)
        }

    fun hasToken(): Boolean {
        return MMKV.defaultMMKV().containsKey(KEY_TOKEN)
    }

    fun logout() {

    }


}