package com.yidian.shenghuoquan.newscontent.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.audio.IAudioControl
import com.yidian.shenghuoquan.newscontent.audio.IAudioView
import java.lang.ref.WeakReference

class FloatAudioView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    FrameLayout(context, attrs) , IAudioView {
    private  var close: (() -> Unit)?=null
    private  val TAG = "FloatAudioView"
    private lateinit var ivPower:ImageView
    private lateinit var ivPrev:ImageView
    private lateinit var ivNext:ImageView
    private lateinit var ivPlay:ImageView
    private lateinit var tvContent:TextView
    private var audioControl:WeakReference<IAudioControl>?=null
    init {
        inflate(context, R.layout.float_audio_layout,this)
        initView()
        initListener()
    }

    private fun initListener() {
        ivPower.setOnClickListener {
            Log.e(TAG, "initListener: ivPower ==" )
            visibility = View.GONE
            audioControl?.get()?.release()
            close?.invoke()
        }
        ivPrev.setOnClickListener {
            Log.e(TAG, "initListener: iv ivPrev" )
            audioControl?.get()?.previous()
        }
        ivNext.setOnClickListener {
            Log.e(TAG, "initListener: iv next" )
            audioControl?.get()?.next()

        }
        ivPlay.setOnClickListener {
            Log.e(TAG, "initListener: iv play" )

            if (audioControl?.get()?.isPlaying() ==true){
                audioControl?.get()?.pause()
                updatePlayUi(false)
            }else{
                audioControl?.get()?.start()
                updatePlayUi(true)
            }

        }

    }

    private fun initView() {
        ivPower = findViewById(R.id.iv_power)
        ivPlay = findViewById(R.id.iv_audio_play)
        ivPrev = findViewById(R.id.iv_prev)
        ivNext = findViewById(R.id.iv_next)
        tvContent = findViewById(R.id.tv_content)
    }

    override fun bindAudioControl(control: IAudioControl) {
        audioControl = WeakReference(control)

    }
    override fun setPrevEnable(enable: Boolean) {
        ivPrev.isEnabled = enable
        if (enable){
            ivPrev.setImageResource(R.drawable.skip_previous)
        }else{
            ivPrev.setImageResource(R.drawable.skip_previous_gray)
        }
    }

    override fun setPlayEnable(enable: Boolean) {
        ivPlay.isEnabled = enable
    }
    override fun setNextEnable(enable:Boolean){
        ivNext.isEnabled = enable
        if (enable){
            ivNext.setImageResource(R.drawable.skip_next)
        }else{
            ivNext.setImageResource(R.drawable.skip_next_gray)
        }
    }

    override fun setContent(content: String) {
        if (::tvContent.isInitialized){
            tvContent.text = content
        }
    }

    override fun updatePlayUi(isPlay: Boolean) {
        ivPlay.setImageResource(if (isPlay)R.drawable.audio_start else R.drawable.audio_pause)
    }
    fun setOnCloseListener(close:()->Unit){
        this.close = close
    }
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        audioControl?.clear()
        audioControl=null
    }

}