package com.remax.visualnovel.widget.toolbar


import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.annotation.DrawableRes
import com.remax.visualnovel.R
import com.remax.visualnovel.utils.StatusBarUtil3
import com.remax.visualnovel.widget.uitoken.view.UITokenFrameLayout
import com.remax.visualnovel.widget.uitoken.view.UITokenImageView
import com.remax.visualnovel.widget.uitoken.view.UITokenLinearLayout
import com.remax.visualnovel.widget.uitoken.view.UITokenTextView

class CommonToolbar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : UITokenFrameLayout(context, attrs, defStyleAttr) {

    private val ivBack: UITokenImageView
    private val llCenter: UITokenLinearLayout
    private val ivTitle: UITokenTextView
    private val llRight: UITokenLinearLayout


    private var onChildClickListener: ((View) -> Unit)? = null

    init {
        val rootView = LayoutInflater.from(context).inflate(R.layout.common_toolbar_layout, this, true)
        ivBack = findViewById(R.id.iv_left)
        llCenter = findViewById(R.id.ll_center)
        ivTitle = findViewById(R.id.iv_title)
        llRight = findViewById(R.id.ll_right)

        with(rootView) {
            setPadding(
                paddingLeft,
                paddingTop + StatusBarUtil3.getStatusBarHeight(context),
                paddingRight,
                paddingBottom
            )
        }

        setOnChildClickListener { view ->
            when (view.id) {
                R.id.iv_left -> {
                    performBackAction()
                }
                // TODO - 其他子view
            }
        }
    }

    fun setTitle(resId: Int) {
        ivTitle.setText(resId)
    }

    fun addCenterView(view: View) {
        llCenter.removeAllViews()
        llCenter.addView(view)
        setupClickListener(view)
    }

    fun addRightIcon(@DrawableRes resId: Int, tag: String? = null): ImageView {
        val imageView = ImageView(context).apply {
            setImageResource(resId)
            scaleType = ImageView.ScaleType.CENTER_INSIDE
            layoutParams = LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(16, 0, 16, 0)
            }
            this.tag = tag
        }
        llRight.addView(imageView)
        setupClickListener(imageView)
        return imageView
    }

    fun addRightIcon(@DrawableRes resId: Int, onClick: ()->Unit) {
        val imageView = ImageView(context).apply {
            setImageResource(resId)
            scaleType = ImageView.ScaleType.CENTER_INSIDE
            setOnClickListener {
                onClick.invoke()
            }
            layoutParams = LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(16, 0, 16, 0)
            }
            this.tag = tag
        }
        llRight.addView(imageView)
        setupClickListener(imageView)
    }

    fun addRightView(view: View, tag: String? = null) {
        view.tag = tag
        llRight.addView(view)
        setupClickListener(view)
    }

    fun clearRightViews() {
        llRight.removeAllViews()
    }

    fun setOnChildClickListener(listener: (View) -> Unit) {
        this.onChildClickListener = listener
        setupClickListeners()
    }

    fun setBackVisible(visible: Boolean) {
        ivBack.visibility = if (visible) View.VISIBLE else View.GONE
    }

    fun setBackIcon(@DrawableRes resId: Int) {
        ivBack.setImageResource(resId)
    }

    // 通过tag查询右侧cotnainer中View
    fun findRightViewByTag(tag: String): View? {
        for (i in 0 until llRight.childCount) {
            val child = llRight.getChildAt(i)
            if (child.tag == tag) {
                return child
            }
        }
        return null
    }

    private fun setupClickListeners() {
        setupClickListener(ivBack)
        setupClickListener(llCenter)
        setupClickListener(ivTitle)

        for (i in 0 until llRight.childCount) {
            setupClickListener(llRight.getChildAt(i))
        }
    }

    private fun setupClickListener(view: View) {
        view.setOnClickListener { v ->
            onChildClickListener?.invoke(v)
        }
    }

    private fun performBackAction() {
        (context as? android.app.Activity)?.onBackPressed()
    }
}