package com.gamedog.vididin.youtubestatistic

import android.os.Handler
import android.os.Looper
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.youtubestatistic.RewardConst.Companion.Check_Interval_MS


class TickerTimer() {

    private var mTotalMs = 0L
    private var mHasFinishCurVideo = false
    private var mVideoId: String? = null
    private var mVideoDurationMs: Long = 0


    private val mCountHandler: Handler by lazy {
        Handler(Looper.getMainLooper())
    }
    private val mCounterRunnable by lazy {
        object : Runnable {
            override fun run() {
                if (isRunning) {
                    handleNewTick()
                    mCountHandler.postDelayed(this, Check_Interval_MS)
                }
            }
        }
    }

    @Volatile
    private var isRunning = false



    fun setVideoInfo(videoId: String, videoDurationMs: Long) {
        mVideoId = videoId
        mVideoDurationMs = videoDurationMs
    }

    fun start() {
        isRunning = true
        mCountHandler.postDelayed(mCounterRunnable, Check_Interval_MS)
    }


    private fun handleNewTick() {
        NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null)

        if (!mHasFinishCurVideo && mVideoDurationMs > 0L) {
            mTotalMs += Check_Interval_MS
            if (mTotalMs >= RewardConst.FINISH_RATIO * mVideoDurationMs) {
                mHasFinishCurVideo = true
                NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_One_Video,
                    NotifyMan.NotifyData(Pair(mVideoId, System.currentTimeMillis())))
            }
        }
    }

    fun pause() {
        if (!isRunning) return
        isRunning = false
        mCountHandler.removeCallbacks(mCounterRunnable)
    }


    fun release() {
        isRunning = false
        mCounterRunnable.let { mCountHandler.removeCallbacks(it) }
    }




    /*private var mStartMs = 0L
    private var mTotalMs = 0L
    private var mHasFinishCurVideo = false
    private var mVideoId: String? = null
    private var mVideoDurationMs: Long = 0


    private val mCountHandler: Handler by lazy {
        Handler(Looper.getMainLooper())
    }
    private val mCounterRunnable by lazy {
        object : Runnable {
            override fun run() {
                if (isRunning) {
                    if (mVideoDurationMs > 0L && mStartMs <= 0) {
                        mStartMs = SystemClock.elapsedRealtime()
                    }

                    handleNewTick()
                    mCountHandler.postDelayed(this, Check_Interval_MS)
                }
            }
        }
    }

    @Volatile
    private var isRunning = false



    fun setVideoInfo(videoId: String, videoDurationMs: Long) {
        mVideoId = videoId
        mVideoDurationMs = videoDurationMs
    }

    fun start() {
        isRunning = true
        if (mVideoDurationMs > 0L && mStartMs <= 0) {
            mStartMs = SystemClock.elapsedRealtime()
        }
        mCountHandler.postDelayed(mCounterRunnable, Check_Interval_MS)
    }


    private fun handleNewTick() {
        NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null)

        if (!mHasFinishCurVideo && mStartMs > 0L && mVideoDurationMs > 0L) {
            mTotalMs += (SystemClock.elapsedRealtime() - mStartMs)
            if (mTotalMs >= RewardConst.FINISH_RATIO * mVideoDurationMs) {
                mHasFinishCurVideo = true
                NotifyMan.instance().sendEvent(VididinEvents.Event_Finish_Video,
                    NotifyMan.NotifyData(Pair(mVideoId, System.currentTimeMillis())))
            }
        }
    }

    fun pause() {
        if (!isRunning) return
        isRunning = false
        mCountHandler.removeCallbacks(mCounterRunnable)
        mStartMs = 0L
    }


    fun release() {
        isRunning = false
        mCounterRunnable.let { mCountHandler.removeCallbacks(it) }
    }*/

    /*
    private val mHandlerThread = HandlerThread("HomeCountTimeThread")
    private val mCountHandler: Handler by lazy {
        Handler(mHandlerThread.looper)
    }
    private val mCounterRunnable by lazy {
        object : Runnable {
            override fun run() {
                if (isRunning) {
                    handleNewTick()
                    mCountHandler.postDelayed(this, Notify_Interval_MS)
                }
            }
        }
    }

    @Volatile
    private var isRunning = false




    companion object {
        const val HOME_WATCH_DURATION = 5 * 1000L
        const val Notify_Interval_MS = 50L
    }

    init {
        mHandlerThread.start()
    }


    @Synchronized
    fun start() {
        isRunning = true
        mCountHandler.postDelayed(mCounterRunnable, Notify_Interval_MS)
    }


    private fun handleNewTick() {
        NotifyMan.instance().sendEvent(VididinEvents.Event_HOME_WATCH_Time_TICK, null)
    }

    fun pause() {
        if (!isRunning) return
        isRunning = false
    }


    fun release() {
        isRunning = false
        mCounterRunnable.let { mCountHandler.removeCallbacks(it) }
        mHandlerThread.quitSafely()
    }*/
}