package com.adealink.weparty.commonui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.DisplayUtil
import com.adealink.weparty.databinding.LayoutCommonEmptyErrorBinding

class CommonEmptyErrorView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {
    val binding = LayoutCommonEmptyErrorBinding.inflate(LayoutInflater.from(context), this)

    fun setSubTitleColor(@ColorInt color: Int) {
        binding.tvEmptyErrorSubTitle.setTextColor(color)
    }

    fun setTitleColor(@ColorInt color: Int) {
        binding.tvEmptyErrorTitle.setTextColor(color)
    }

    fun show(
        @DrawableRes errorEmptyResId: Int? = null,
        @StringRes title: Int? = null,
        @StringRes subTitle: Int? = null,
        @StringRes buttonText: Int? = null,
        buttonClick: (() -> Unit)? = null,
        isAllPage: Boolean = true
    ) {
        visibility = VISIBLE
        binding.ivErrorEmpty.visibility = GONE
        binding.tvEmptyErrorTitle.visibility = GONE
        binding.tvEmptyErrorSubTitle.visibility = GONE
        binding.btnEmptyError.visibility = GONE

        errorEmptyResId?.let {
            binding.ivErrorEmpty.visibility = VISIBLE
            binding.ivErrorEmpty.setImageResource(errorEmptyResId)
        }
        title?.let {
            binding.tvEmptyErrorTitle.visibility = VISIBLE
            binding.tvEmptyErrorTitle.text = getCompatString(it)
        }
        subTitle?.let {
            binding.tvEmptyErrorSubTitle.visibility = VISIBLE
            binding.tvEmptyErrorSubTitle.text = getCompatString(it)
        }
        buttonText?.let {
            binding.btnEmptyError.visibility = VISIBLE
            binding.btnEmptyError.text = getCompatString(it)
            binding.btnEmptyError.setOnClickListener {
                buttonClick?.invoke()
            }
        }
        val lp = binding.ivErrorEmpty.layoutParams
        if (isAllPage) {
            lp.width = DisplayUtil.dp2px(120f)
        } else {
            lp.width = DisplayUtil.dp2px(200f)
        }
        binding.ivErrorEmpty.layoutParams = lp
    }

    fun show(
        @DrawableRes errorEmptyResId: Int? = null,
        title: String? = null,
        subTitle: String? = null,
        buttonText: String? = null,
        buttonClick: (() -> Unit)? = null,
        isAllPage: Boolean = true
    ) {
        visibility = VISIBLE
        binding.ivErrorEmpty.visibility = GONE
        binding.tvEmptyErrorTitle.visibility = GONE
        binding.tvEmptyErrorSubTitle.visibility = GONE
        binding.btnEmptyError.visibility = GONE

        errorEmptyResId?.let {
            binding.ivErrorEmpty.visibility = VISIBLE
            binding.ivErrorEmpty.setImageResource(errorEmptyResId)
        }
        title?.let {
            binding.tvEmptyErrorTitle.visibility = VISIBLE
            binding.tvEmptyErrorTitle.text = it
        }
        subTitle?.let {
            binding.tvEmptyErrorSubTitle.visibility = VISIBLE
            binding.tvEmptyErrorSubTitle.text = it
        }
        buttonText?.let {
            binding.btnEmptyError.visibility = VISIBLE
            binding.btnEmptyError.text = it
            binding.btnEmptyError.setOnClickListener {
                buttonClick?.invoke()
            }
        }
        val lp = binding.ivErrorEmpty.layoutParams
        if (isAllPage) {
            lp.width = DisplayUtil.dp2px(120f)
        } else {
            lp.width = DisplayUtil.dp2px(200f)
        }
        binding.ivErrorEmpty.layoutParams = lp
    }

    /**
     * 设置图片大小
     */
    fun setImageSize(size: Int) {
        binding.ivErrorEmpty.updateLayoutParams<LayoutParams> {
            width = size
        }
    }

    fun hide() {
        visibility = GONE
    }
}