package xn.xxp.widget

import android.content.Context
import android.os.Message
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import core.util.OnWeakListener
import core.util.WeakHandler
import xn.xxp.R
import xn.xxp.databinding.WidgetTitleBarBinding
import xn.xxp.app.LabApp
import xn.xxp.room.bean.NoticeSummary
import java.text.SimpleDateFormat
import java.util.*

/**
 * info
 *
 * @author ReiChin_
 */
class TitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    ConstraintLayout(context, attrs, defStyleAttr),
    ITitleBar,
    OnWeakListener {

    private val weakHandler by lazy { WeakHandler(this) }
    private val viewBinding =
        WidgetTitleBarBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar)
        val signed = typeArray.getBoolean(R.styleable.TitleBar_signed, false)
        val showNotice = typeArray.getBoolean(R.styleable.TitleBar_showNotice, true)
        typeArray.recycle()

        setSigned(signed)
        setShowNotice(showNotice)
        viewBinding.noticeLayout.setOnClickListener { mTitleListener?.onNoticeViewClicked() }
    }

    override fun setSigned(signed: Boolean) {
        if (signed) {
            viewBinding.userGroup.visibility = VISIBLE
            val layoutParams = viewBinding.nowDate.layoutParams as LayoutParams
            layoutParams.horizontalBias = 0.52f
            viewBinding.nowDate.layoutParams = layoutParams
        } else {
            viewBinding.userGroup.visibility = GONE
            val layoutParams = viewBinding.nowDate.layoutParams as LayoutParams
            layoutParams.horizontalBias = 0.58f
            viewBinding.nowDate.layoutParams = layoutParams
        }
    }

    override fun setShowNotice(showNotice: Boolean) {
        viewBinding.noticeLayout.visibility = if (showNotice) VISIBLE else GONE
    }

    override fun setTitleInfoFromSp() {
        val laboratory = LabApp.laboratory
        if (null != laboratory) {
            Glide.with(this)
                .load(laboratory.rectangleLogo)
                .into(viewBinding.logo)

            viewBinding.subName.text = laboratory.subName
        }
        weakHandler.sendEmptyMessage(1)

        val userVo = LabApp.userVo
        if (null != userVo) {
            viewBinding.person.text = userVo.userName
        }
    }

    override fun updateNotice(data: List<NoticeSummary>?) {
        if (VISIBLE == viewBinding.noticeLayout.visibility) {
            val simpleData = data?.map { it.title }?.toMutableList()
            viewBinding.noticeFlipper.adapter = SimpleFlipperAdapter(data = simpleData)
            viewBinding.noticeFlipper.flipInterval = 3000
            viewBinding.noticeFlipper.setInAnimation(context, R.animator.anim_in)
            viewBinding.noticeFlipper.setOutAnimation(context, R.animator.anim_out)
            viewBinding.noticeFlipper.startFlipping()
        }
    }

    override fun refreshCountDownView(countDown: Int) {
        if (VISIBLE == viewBinding.countDown.visibility) {
            viewBinding.countDown.text = "${countDown}S"
        }
    }

    private var mTitleListener: ITitleBar.TitleListener? = null

    override fun setTitleListener(listener: ITitleBar.TitleListener) {
        this.mTitleListener = listener
    }

    override fun onWeakHandleMessage(msg: Message) {
        // 3月13日 星期一 14:36
        val format = SimpleDateFormat("MM月dd日 EEEE HH:mm", Locale.getDefault())
        viewBinding.nowDate.text =
            format.format(Calendar.getInstance(Locale.getDefault()).time)
        // 下一分钟更新日期显示
        val calendar = Calendar.getInstance()
        val nowTime = calendar.timeInMillis
        calendar.set(Calendar.SECOND, 0) // 秒
        calendar.set(Calendar.MILLISECOND, 0) // 毫秒
        calendar.add(Calendar.MINUTE, 1) // 分+1
        val nextTime = calendar.timeInMillis
        msg.target.sendEmptyMessageDelayed(1, nextTime - nowTime)
    }

}