package com.yidian.bcommon.widget.blank

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.yidian.bcommon.R
import com.yidian.nightmode.widget.YdLinearLayout
import com.yidian.nightmode.widget.YdTextView
import com.yidian.bcommon.utils.YdUiUtils

/**
 * 缺省页面
 */
class BlankPageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
  FrameLayout(context, attrs, defStyle) {

  companion object {
    const val Error = "error"
    const val Blank = "blank"
  }

  private lateinit var llyError: YdLinearLayout
  private lateinit var llyBlank: YdLinearLayout
  private lateinit var tvLoadAgain: YdTextView
  private lateinit var loadAgainCallback: IBlankViewLoadAgainCallback

  init {
    inflate(context, R.layout.blank_page_view, this)
  }

  fun initBlankPageView(loadAgainCallback: IBlankViewLoadAgainCallback, position: Int) {
    this.loadAgainCallback = loadAgainCallback
    val lp = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    lp.topMargin = YdUiUtils.dip2px(position, context)
    lp.gravity = Gravity.CENTER_HORIZONTAL
    llyError.layoutParams = lp
    llyBlank.layoutParams = lp
  }

  override fun onFinishInflate() {
    super.onFinishInflate()
    llyError = findViewById(R.id.lly_error)
    llyBlank = findViewById(R.id.lly_blank)
    tvLoadAgain = findViewById(R.id.tv_loadAgain)
    setClickListener()
  }

  private fun setClickListener() {
    tvLoadAgain.setOnClickListener {
      loadAgainCallback.loadAgainCallback()
    }
  }

  fun setBlankStatus(status: String) {
    when (status) {
      Error -> {
        llyError.visibility = View.VISIBLE
        llyBlank.visibility = View.GONE
      }
      Blank -> {
        llyError.visibility = View.GONE
        llyBlank.visibility = View.VISIBLE
      }
    }
  }
}
