package com.develop.food.base.utils

import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import java.lang.RuntimeException


object ThreadUtils {

    private val handler = Handler(Looper.getMainLooper())
    private val workThread = HandlerThread("ThreadUtil")
    private val workHandler: Handler
    init {
        workThread.start()
        workHandler = Handler(workThread.looper)
    }

    fun runOnWorkThread(runnable: Runnable, delayMs: Long = 0L) {
        workHandler.postDelayed(runnable, delayMs)
    }

    fun runOnMainThread(runnable: Runnable) {
        handler.post(runnable)
    }

    fun runOnMainThread(runnable: Runnable, delayMs: Long) {
        handler.postDelayed(runnable, delayMs)
    }

    fun ensureOnMainThread() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw RuntimeException("not in ui-thread!")
        }
    }
}