package com.swago.room.hongbao

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.RoomTimer
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.model.RedEnvelope
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.TimeUtil
import com.swago.loadWebp
import com.swago.room.R
import com.swago.room.databinding.ViewRedEnvelopeBinding
import java.util.concurrent.CopyOnWriteArrayList

class RedEnvelopeView : FrameLayout, IRoomActiveListener, RoomTimer.TimeTickListener {

    private var  binding:ViewRedEnvelopeBinding? = null

    private var time = 0

    private val redEnvelopeList by lazy {
        CopyOnWriteArrayList<RedEnvelope>()
    }

    var openRedEnvelope:((redEnvelope:RedEnvelope) ->Unit)? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView()
    }

    private fun initView() {
        SwagoRoomManager.addListener(this)
        RoomTimer.timeTickList.add(this)
        binding = ViewRedEnvelopeBinding.inflate(LayoutInflater.from(context), this, true)

        binding?.ivRedEnvelope?.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                if (redEnvelopeList.isNotEmpty()&&redEnvelopeList[0].coldTime == 0){
                    openRedEnvelope?.invoke(redEnvelopeList[0])
                    redEnvelopeList.removeAt(0)
                    if (redEnvelopeList.isEmpty()){
                        visibility = View.GONE
                    }else{
                        binding?.tvCount?.text = "${redEnvelopeList.size}"
                    }
                }
            }
        })
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {

    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        redEnvelopeList.clear()
        visibility = View.GONE
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {

    }

    override fun closeRoomed() {
        RoomTimer.timeTickList.remove(this)
        SwagoRoomManager.removeListener(this)
    }

    override fun onTimeCount(mill: Int) {
        if (redEnvelopeList.size != 0) {
            redEnvelopeList.forEachIndexed { index, it ->
                if (it.coldTime != 0) {
                    time = it.coldTime - 1
                    it.coldTime = time
                }
                if (it.receiveTime != 0) {
                    time = it.receiveTime - 1
                    it.receiveTime = time
                } else {
                    if (redEnvelopeList.size - 1 >= index) {
                        redEnvelopeList.removeAt(index)
                        binding?.tvCount?.text = "${redEnvelopeList.size}"
                    }
                }

                if (redEnvelopeList.size != 0) {
                    if (index==0){
                        setRedEnvelopStatus()
                    }
                } else {
                    visibility = View.GONE
                }
            }
        }else{
            visibility = View.GONE
        }
    }


    /**
     * 接口获取
     */
    fun addRedEnvelopList(data:ArrayList<RedEnvelope>){
        if (data.isNotEmpty()){
            redEnvelopeList.addAll(data)
            visibility = View.VISIBLE
            binding?.tvCount?.visibility = View.VISIBLE
            binding?.tvCount?.text = "${data.size}"
            setRedEnvelopStatus()
        }
    }

    /**
     * IM获取
     */
    fun addIMRedEnvelope(redEnvelope: RedEnvelope){
        redEnvelopeList.add(redEnvelope)
        binding?.tvCount?.text = "${redEnvelopeList.size}"
        visibility = View.VISIBLE
        setRedEnvelopStatus()
    }

    private fun setRedEnvelopStatus() {
        if (redEnvelopeList[0].receiveTime < 30) {
            //可以领取状态
            binding?.ivRedEnvelope?.loadWebp(context,R.mipmap.icon_red_envelope_receive)
            binding?.tvTime?.visibility = View.GONE
        } else {
            //倒计时状态
            binding?.ivRedEnvelope?.setImageResource(R.mipmap.icon_wait_receive)
            binding?.tvTime?.visibility = View.VISIBLE
            binding?.tvTime?.text = TimeUtil.convertMinSecond(redEnvelopeList[0].coldTime.toLong())
        }
    }

}