package com.yzx.escreen.config

import android.content.Intent
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.yzx.escreen.App.Companion.getContext
import com.yzx.escreen.MainActivity
import com.yzx.escreen.activity.LoginActivity
import com.yzx.escreen.config.Config.OA_URL
import com.yzx.escreen.model.ReToken
import com.yzx.escreen.model.RefreshTokenEvent
import com.yzx.escreen.model.User
import com.yzx.escreen.presenter.base.JsonCallBack
import com.yzx.escreen.presenter.base.YzxResponse
import org.greenrobot.eventbus.EventBus
import java.util.Date

object RefreshToken {
    fun refresh(callBack:RefreshCallback) {
        val user = User.getUser()
        val lastLoginTime = user.loginTime
        val currTime = Date().time
        if (currTime - lastLoginTime > 12L * 60 * 60 * 1000 && currTime - lastLoginTime < 365L * 24 * 60 * 60 * 1000) {
            if (user.user_id > 0) {
                val url = Config.BASE_URL + YzxInterface.INTERFACE_REFRESH_TOKEN
                OkGo.post<YzxResponse<ReToken>>(url)
                    .tag(this)
                    .params("old_token", User.getUser().token)
                    .params("type", 1)
                    .params("source", "201")
                    .execute(object : JsonCallBack<YzxResponse<ReToken>>() {
                        override fun onSuccess(response: Response<YzxResponse<ReToken>>?) {
                            LogUtils.d(response)
                            val token = response?.body()?.data?.token ?: ""
                            if (token.isNotBlank()) {
                                user.token = token
                                user.loginTime = Date().time
                                user.save()
                                callBack.onSuccess()
                                EventBus.getDefault().post(RefreshTokenEvent(token))
                            }
                        }

                        override fun onError(response: Response<YzxResponse<ReToken>>?) {
                            super.onError(response)
                            LogUtils.d(response)
                            User.clearUserInfo()
//                            val intent = Intent(getContext(), WebActivity::class.java)
                            val intent = Intent(getContext(), LoginActivity::class.java)
                            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                            intent.putExtra("link", OA_URL + "/login?logout=1")
                            ActivityUtils.startActivity(intent)
                            ActivityUtils.finishActivity(MainActivity::class.java)
                            callBack.onError()
                        }
                    })
            }else{
                User.clearUserInfo()
//                val intent = Intent(getContext(), WebActivity::class.java)
                val intent = Intent(getContext(), LoginActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("link", OA_URL + "/login?logout=1")
                ActivityUtils.startActivity(intent)
                ActivityUtils.finishActivity(MainActivity::class.java)
                callBack.onError()
            }
        }else{
            if (currTime - lastLoginTime > 365L * 24 * 60 * 60 * 1000){
                User.clearUserInfo()
//                val intent = Intent(getContext(), WebActivity::class.java)
                val intent = Intent(getContext(), LoginActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("link", OA_URL + "/login?logout=1")
                ActivityUtils.startActivity(intent)
                ActivityUtils.finishActivity(MainActivity::class.java)
                callBack.onError()
            }else{
                callBack.onSuccess()
            }
        }
    }

    interface RefreshCallback{
        fun onSuccess()
        fun onError()
    }
}