package com.adealink.weparty.commonui.widget

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.removeUiCallbacks
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.R

class AutoMarqueeTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null,
) : AppCompatTextView(context, attrs) {

    private val isLeft: Boolean = !DisplayUtil.isRtlLayout()
    private var focused = true
    private var delayMarqueeTime = 0
    private val startMarqueeRunnable = Runnable {
        focused = true
        isSelected = true
    }

    init {
        val typedArray: TypedArray = context.obtainStyledAttributes(
            attrs,
            R.styleable.AutoMarqueeTextView
        )
        delayMarqueeTime = typedArray.getInt(
            R.styleable.AutoMarqueeTextView_delayMarqueeTime,
            0
        )
        focused = delayMarqueeTime == 0
        typedArray.recycle()
        if (!focused) {
            runOnUiThread(startMarqueeRunnable, delayMarqueeTime.toLong())
        }
    }

    override fun isFocused(): Boolean {
        return focused
    }

    override fun onFocusChanged(
        focused: Boolean, direction: Int,
        previouslyFocusedRect: Rect?,
    ) {
        if (focused) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect)
        }
    }

    @SuppressLint("WrongConstant")
    override fun getLayoutDirection(): Int {
        return if (isLeft) View.LAYOUT_DIRECTION_LTR else View.LAYOUT_DIRECTION_RTL
    }

    override fun getTextDirection(): Int {
        return if (isLeft) View.TEXT_DIRECTION_LTR else View.TEXT_DIRECTION_RTL
    }

    override fun onWindowFocusChanged(focused: Boolean) {
        if (focused) {
            super.onWindowFocusChanged(focused)
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        val lifecycleOwner = findViewTreeLifecycleOwner() ?: return
        lifecycleOwner.lifecycle.addObserver(
            object : androidx.lifecycle.DefaultLifecycleObserver {
                override fun onResume(owner: LifecycleOwner) {
                    runOnUiThread(startMarqueeRunnable, delayMarqueeTime.toLong())
                }

                override fun onPause(owner: LifecycleOwner) {
                    removeUiCallbacks(startMarqueeRunnable)
                }

                override fun onDestroy(owner: LifecycleOwner) {
                    super.onDestroy(owner)
                    lifecycleOwner.lifecycle.removeObserver(this)
                }
            }
        )
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        removeUiCallbacks(startMarqueeRunnable)
    }
}
