package com.xty.base.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.xty.base.R


/**
 * 仿微信发送语音消息弹窗
 */
class ChartDialogManager(context: Context) {
    private var mDialog: Dialog? = null
    private var mIcon: ImageView? = null
    private var mVoice: ImageView? = null
    private var mLable: TextView? = null
    private var mContext: Context? = null

    init {
        mContext = context
        initView()
    }

    fun initView() {
        val inflater = LayoutInflater.from(mContext)
        val view: View = inflater.inflate(R.layout.dialog_recorder, null)
        mDialog = mContext?.let { Dialog(it, R.style.Theme_ChartDialogManager) }
        mDialog!!.setContentView(view)
        mIcon = view.findViewById(R.id.iv_recorder_dialog_icon)
        mVoice = view.findViewById(R.id.iv_recorder_dialog_voice)
        mLable = view.findViewById(R.id.tv_recorder_dialog_label)
    }

    fun showRecordingDialog() {
        if(null == mDialog){
            initView()
        }
        mDialog!!.show()
    }

    //正在播放时的状态
    fun recording() {
        if (mDialog != null && mDialog!!.isShowing) {
            mIcon!!.visibility = View.VISIBLE
            mVoice!!.visibility = View.VISIBLE
            mLable!!.visibility = View.VISIBLE
            mIcon!!.setImageResource(R.mipmap.audio_recorder)
            mLable!!.text = mContext!!.getText(R.string.slide_up_cancel)
        }
    }

    //想要取消
    fun wantToCancel() {
        if (mDialog != null && mDialog!!.isShowing) {
            mIcon!!.visibility = View.VISIBLE
            mVoice!!.visibility = View.GONE
            mLable!!.visibility = View.VISIBLE
            mIcon!!.setImageResource(R.mipmap.send_voice_cancel)
            mLable!!.text = mContext!!.getText(R.string.want_cancel)
        }
    }

    //录音时间太短
    fun tooShort() {
        if (mDialog != null && mDialog!!.isShowing) {
            mIcon!!.visibility = View.VISIBLE
            mVoice!!.visibility = View.GONE
            mLable!!.visibility = View.VISIBLE
            mIcon!!.setImageResource(R.mipmap.voice_to_short)
            mLable!!.text = mContext!!.getText(R.string.audio_too_short)
        }
    }

    //关闭dialog
    fun dimissDialog() {
        if (mDialog != null && mDialog!!.isShowing) {
            mDialog!!.dismiss()
            mDialog = null
        }
    }

    /**
     * 通过level更新voice上的图片
     *
     * @param level
     */
    fun updateVoiceLevel(level: Int) {
        if (mDialog != null && mDialog!!.isShowing) {
            val resId = mContext!!.resources.getIdentifier(
                "talk_voice_level_v$level", "mipmap", mContext!!.packageName
            )
            mVoice!!.setImageResource(resId)
        }
    }
}