package com.ama.core.architecture.widget

import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import com.ama.core.architecture.R
import com.ama.core.architecture.databinding.CustomTitleBarBinding
import com.ama.core.architecture.util.ResUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.common.util.dp


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

    private var mBinding: CustomTitleBarBinding


    init {
        mBinding = CustomTitleBarBinding.inflate(LayoutInflater.from(context), this, true)

        mBinding.run {
            setOnClickBatch(ivBack, tvCenterTitle) {
                when(this) {
                    ivBack -> {
                        (context as? Activity)?.let { activity ->
                            activity.finish()
                        }
                    }

                    tvCenterTitle -> {

                    }
                }
            }

        }
    }

    fun setTitleText(textRes: Int, textColor: Int = R.color.white) {
        mBinding.tvCenterTitle.text = ResUtil.getString(textRes)
        mBinding.tvCenterTitle.setTextColor(ResUtil.getColor(textColor))
    }

    fun setTitleTextSize(textSize: Float) {
        mBinding.tvCenterTitle.textSize = textSize
    }

    fun hideBack() {
        mBinding.ivBack.isVisible = false
    }

    fun setBackIconColor(color: Int) {
        mBinding.ivBack.imageTintList = ColorStateList.valueOf(ResUtil.getColor(color))
    }




    fun addLeftIcon(iconRes: Int, onClicked: ()->Unit) {
        val newImageView = AppCompatImageView(context)
        newImageView.setImageResource(iconRes)

        var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        layoutParam.marginStart = 15.dp
        layoutParam.gravity = Gravity.CENTER_VERTICAL

        mBinding.llLeftRoot.addView(newImageView, layoutParam)
    }

    fun addLeftText(textRes: Int, onClicked: ()->Unit) {
        val newImageView = AppCompatTextView(context)
        newImageView.text = ResUtil.getString(textRes)

        var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        layoutParam.marginStart = 15.dp
        layoutParam.gravity = Gravity.CENTER_VERTICAL

        mBinding.llLeftRoot.addView(newImageView, layoutParam)
    }

    fun addRightIcon(iconRes: Int, onClicked: ()->Unit) {
        val newImageView = AppCompatImageView(context)
        newImageView.setImageResource(iconRes)

        var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        layoutParam.marginEnd = 15.dp
        layoutParam.gravity = Gravity.CENTER_VERTICAL

        newImageView.isClickable = true
        newImageView.setOnClickListener { onClicked.invoke() }

        mBinding.llRightRoot.addView(newImageView, layoutParam)
    }

    fun addRightText(textRes: Int, onClicked: ()->Unit) {
        val newTextView = AppCompatTextView(context)
        newTextView.text = ResUtil.getString(textRes)

        var layoutParam = LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        layoutParam.marginEnd = 15.dp
        layoutParam.gravity = Gravity.CENTER_VERTICAL
        newTextView.isClickable = true
        newTextView.setOnClickListener { onClicked.invoke() }

        mBinding.llRightRoot.addView(newTextView, layoutParam)
    }


}

