package com.renrenliao.im.im

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.MapUtils
import com.renrenliao.im.utils.RxTimerUtils
import kotlinx.coroutines.*
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

/**
 **@ClassName OffLineService
 **@Description
 **@Author gan
 **@Date 2022/6/7 14:57
 **/
class OffLineMsgService :Service(),KoinComponent{

    val remoteDataSource by inject<RemoteDataSource>()

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        LogUtils.error("OffLineMsgService onCreate")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        //return super.onStartCommand(intent, flags, startId)
        RxTimerUtils.interval(20000){
            getOffLineMsg()
        }
        return START_STICKY
    }

    @OptIn(DelicateCoroutinesApi::class)
    private fun getOffLineMsg()
    {
        val map = MapUtils.getComParam(HashMap<String,String>())
        GlobalScope.launch{
            //请求离线消息
            val result = withContext(Dispatchers.IO){
           //     remoteDataSource.getFriendList(map)
            }

            LogUtils.error("请求离线消息 $result")
            //写入数据库
            withContext(Dispatchers.IO){

            }
        }
    }
    override fun onDestroy() {
        LogUtils.error("OffLineMsgService onDestroy")
        RxTimerUtils.cancel()
        super.onDestroy()
    }
}