package com.dlc.exam.ui.widget

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.dlc.exam.R
import com.dlc.exam.databinding.WidgetTitleBarBinding
import java.lang.ref.WeakReference
import java.text.SimpleDateFormat
import java.util.*

/**
 * info
 *
 * @author ReiChin_
 */
class TitleBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    LinearLayout(context, attrs, defStyleAttr) {
    private val weakHandler by lazy { WeakReferenceHandler(this) }
    private val viewBinding =
        WidgetTitleBarBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        val typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar)
        val showHome = typeArray.getBoolean(R.styleable.TitleBar_home, false)
        if (showHome) {
            viewBinding.home.visibility = GONE
            viewBinding.logout.visibility = GONE
            viewBinding.back.visibility = GONE
        } else {
            viewBinding.home.visibility = VISIBLE
            viewBinding.logout.visibility = VISIBLE
            viewBinding.back.visibility = VISIBLE
        }
        typeArray.recycle()

        viewBinding.home.setOnClickListener { mTitleListener?.onHomeViewClicked(it) }
        viewBinding.logout.setOnClickListener { mTitleListener?.onLogoutViewClicked(it) }
        viewBinding.back.setOnClickListener { mTitleListener?.onBackViewClicked(it) }
        viewBinding.user.setOnClickListener { mTitleListener?.onUserViewClicked(it) }


        weakHandler.sendEmptyMessage(1)
//        object : Handler(Looper.getMainLooper()) {
//            override fun handleMessage(msg: Message) {
//                val format = SimpleDateFormat("yyyy年MM月dd日 EEEE HH:mm:ss", Locale.getDefault())
//                viewBinding.nowTime.text =
//                    format.format(Calendar.getInstance(Locale.getDefault()).time)
//                sendEmptyMessageDelayed(1, 1000)
//            }
//        }.sendEmptyMessage(1)
    }

    /**
     * 显示退出View
     */
    fun showLogoutView(show: Boolean) {
        viewBinding.logout.visibility = if (show) VISIBLE else GONE
    }

    /**
     * 显示返回View
     */
    fun showBackView(show: Boolean) {
        viewBinding.back.visibility = if (show) VISIBLE else GONE
    }

    private var mTitleListener: TitleListener? = null

    fun setUsername(username: String?) {
        viewBinding.username.text = username
    }

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

    interface TitleListener {
        fun onHomeViewClicked(view: View)
        fun onLogoutViewClicked(view: View)
        fun onBackViewClicked(view: View)
        fun onUserViewClicked(view: View)
    }

    // static + 弱引用  防止内存泄露
    class WeakReferenceHandler(obj: TitleBar) : Handler(Looper.getMainLooper()) {
        private val mRef: WeakReference<TitleBar> = WeakReference(obj)
        override fun handleMessage(msg: Message) {
            mRef.get()?.run {
                val format = SimpleDateFormat("yyyy年MM月dd日 EEEE HH:mm:ss", Locale.getDefault())
                viewBinding.nowTime.text =
                    format.format(Calendar.getInstance(Locale.getDefault()).time)
                sendEmptyMessageDelayed(1, 1000)
            }
        }
    }

}