package com.swago.room.audio.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.swago.loadUrl
import com.swago.room.R
import com.swago.room.databinding.ViewAudioGiftBinding

class AudioGiftView :ConstraintLayout {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    private var binding : ViewAudioGiftBinding? = null
    private var dataImageView = ArrayList<ImageView>()


    private fun initView(context: Context) {
        binding = ViewAudioGiftBinding.inflate(LayoutInflater.from(context), this, true)
    }

    fun clearImageView(){
        binding?.ll?.removeAllViews()
    }


    fun setViewData(imageUrl:String,giftNum:String){
        binding?.apply {
            ivGiftIcon.loadUrl(context,imageUrl)
            val length = giftNum.length+1
            for(i in 0 until length){
                val imageView = ImageView(context)
                dataImageView.add(imageView)
            }

            dataImageView[0].setImageResource(R.mipmap.audio_icon_x)
            ll.addView(dataImageView[0])
            giftNum.toCharArray().forEachIndexed { index, c ->
                when(c){
                    '0' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_zero)
                    }
                    '1' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_one)
                    }
                    '2' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_two)
                    }
                    '3' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_three)
                    }
                    '4' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_four)
                    }
                    '5' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_five)
                    }
                    '6' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_six)
                    }
                    '7' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_seven)
                    }
                    '8' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_eight)
                    }
                    '9' -> {
                        dataImageView[index+1].setImageResource(R.mipmap.audio_icon_nine)
                    }
                }
                ll.addView(dataImageView[index+1])
            }
        }
    }

}