package com.remax.visualnovel.widget.toolbar


import android.content.Context
import android.graphics.drawable.Drawable
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.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 {
        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)


        setOnChildClickListener { view ->
            when (view.id) {
                R.id.iv_left -> {
                    // 默认返回按钮处理
                    performBackAction()
                }
                // TODO - 其他子view
            }
        }
    }

    /**
     * 设置标题文本
     */
    fun setTitle(text: String) {
        ivTitle.text = text
    }

    /**
     * 设置标题文本
     */
    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
            layoutParams = LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
            ).apply {
                setMargins(16, 0, 16, 0)
                setOnClickListener {
                    onClick.invoke()
                }
            }
            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 setBackButtonVisible(visible: Boolean) {
        ivBack.visibility = if (visible) View.VISIBLE else View.GONE
    }

    /**
     * 设置返回按钮图标
     */
    fun setBackButtonIcon(@DrawableRes resId: Int) {
        ivBack.setImageResource(resId)
    }

    /**
     * 设置返回按钮图标
     */
    fun setBackButtonIcon(drawable: Drawable) {
        ivBack.setImageDrawable(drawable)
    }

    /**
     * 获取右侧指定tag的视图
     */
    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()
    }
}