package com.swago.baseswago.util

import android.app.Application
import android.content.Intent
import android.widget.Toast
import androidx.lifecycle.AndroidViewModel
import com.alibaba.android.arouter.launcher.ARouter
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.http.SwagoException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 *@date 2021/8/23 16:30
 *description:
 */
open class BaseViewModel(application: Application) : AndroidViewModel(application) {

    fun requestData(callback: suspend CoroutineScope.() -> Unit) {
        GlobalScope.launch(Dispatchers.Main) {
            try {
                callback.invoke(this)
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(AppContext.getContext(), e.message, Toast.LENGTH_SHORT).show()
                if (e is SwagoException) {
                    if (e.code == 403) {
                        if (UserInfo.getLoginModel()?.token?.isNotEmpty() == true){
                            UserInfo.clearUserInfo()
                            SpUtil.clearSp()
                            ARouter.getInstance().build(ARouteConstant.Login.login)
                                .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                                .navigation()
                        }
                    }
                }
            }
        }
    }


    class CoroutineScopeX {
        var requestData: (suspend CoroutineScope.() -> Unit) = {}
        var requestError: (e: Exception) -> Unit = {}
        var requestFinally: () -> Unit = {}

        fun requestData(call: suspend CoroutineScope.() -> Unit) {
            this.requestData = call
        }

        fun requestError(error: (e: Exception) -> Unit) {
            this.requestError = error
        }

        fun requestFinally(call: () -> Unit) {
            this.requestFinally = call
        }
    }


    fun requestData2(isShowToast:Boolean=true,callback: CoroutineScopeX.() -> Unit) {
        GlobalScope.launch(Dispatchers.Main) {
            val block = CoroutineScopeX()
            callback.invoke(block)
            try {
                block.requestData.invoke(this)
            } catch (e: Exception) {
                if (e is SwagoException) {
                    if (isShowToast){
                        Toast.makeText(AppContext.getContext(), e.message, Toast.LENGTH_SHORT).show()
                    }
                    if (e.code == 403) {
                        if (UserInfo.getLoginModel()?.token?.isNotEmpty() == true){
                            UserInfo.clearUserInfo()
                            SpUtil.clearSp()
                            ARouter.getInstance().build(ARouteConstant.Login.login)
                                .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
                                .navigation()
                        }
                    }
                    block.requestError.invoke(e)
                }else{
                    block.requestError.invoke(e)
                }
            } finally {
                block.requestFinally.invoke()
            }
        }
    }
}