package com.secspace.lib.common.function

import android.content.Context
import com.secspace.lib.common.dialog.LoadingDialog
import com.secspace.lib.common.env.Packages
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.ArrayList

abstract class ISwitch {
    private suspend fun initStart(context: Context) = withContext(Dispatchers.Main) {
        LoadingDialog.getInstance(context).show("切换中")
    }

    private suspend fun initEnd(context: Context) = withContext(Dispatchers.Main) {
        LoadingDialog.getInstance(context).dismiss()
    }

    suspend fun onInit(context: Context) {
        initStart(context)
        init(context)
        initEnd(context)
    }

    suspend fun onExit(context: Context){
        init(context)
    }

    protected abstract suspend fun init(context: Context)

    protected fun getPersistentList(context: Context): List<String?>? {
        var list: MutableList<String?>? = null
        if (list == null) {
            list = ArrayList()
        }
        if (list.size > 0) {
            list.clear()
        }
        list.add(context.packageName)
        list.add(Packages.PkgSecSpace.PACKAGENAME_MIDDLEWARE)
        return list
    }
}