package com.zj365.health.adapter

import android.app.Service
import android.content.Context
import android.content.Intent
import android.text.Html
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xty.base.adapter.BaseAdapter
import com.xty.common.Const
import com.xty.common.URLImageParser
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.xty.network.model.MusicBean
import com.xty.network.model.ProposalBean

class ProposalAdapter(
    var list: MutableList<Int>,
    var musicList: MutableList<MusicBean>? = mutableListOf()
) : BaseAdapter<MutableList<ProposalBean>>(R.layout.item_health_proposal) {

    var isMusicLoading = false //音乐是否正在加载中
    val mAdapter by lazy { MusicAdapter() }
    var recycle: RecyclerView? = null
    val icon by lazy {
        arrayListOf(
            R.mipmap.ic_guid_1, R.mipmap.ic_guid_2, R.mipmap.ic_guid_3,
            R.mipmap.ic_guid_4, R.mipmap.ic_guid_5, R.mipmap.ic_guid_6
        )
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recycle = recyclerView
    }

    val title by lazy { context.resources.getStringArray(R.array.guid_array) }
    override fun convert(holder: BaseViewHolder, item: MutableList<ProposalBean>) {
        val view = holder.getView<TextView>(R.id.mTitle)
//        setLeftIcon(view, , icon[list[holder.layoutPosition]], context)
        view.text = title[list[holder.layoutPosition]]

        if (holder.layoutPosition == data.size - 1) {
            holder.itemView.post {
                recycle?.let {
                    val needHeight = it.height
                    if (holder.itemView.height < needHeight) {
                        val layoutParams = holder.itemView.layoutParams as ViewGroup.LayoutParams
                        layoutParams.height = needHeight
                        holder.itemView.layoutParams = layoutParams
                    }
                }
            }
        }

        if (item.isNullOrEmpty()) {
            holder.setText(R.id.mContent, "暂无数据")
        } else {
            val sb = StringBuffer()
            item.forEach {
                var bean = it as ProposalBean
                sb.append(bean.name).append("\n\n")
                sb.append(bean.content).append("\n\n")
            }

            val tv = holder.getView<TextView>(R.id.mContent)
            val getter = URLImageParser(tv, context)
            tv.text = Html.fromHtml(sb.toString().trim(), getter, null)
        }

        holder.setGone(R.id.mRecycle, list[holder.layoutPosition] != 4)
        if (list[holder.layoutPosition] == 4) {
            //音乐
            if (item.isNotEmpty()) {
                val view = holder.getView<RecyclerView>(R.id.mRecycle)
                initAdapter(view, musicList!!)
            }
        }
    }

    private fun initAdapter(recyclerView: RecyclerView, list: MutableList<MusicBean>) {
        recyclerView.adapter = mAdapter
        recyclerView.layoutManager = LinearLayoutManager(context)
        mAdapter.addChildClickViewIds(R.id.mImagePlay)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (Const.musicLoading) {
                CommonToastUtils.showToast("正在加载中")
            } else {
                Const.musicLoading = true
                val bean = adapter.getItem(position) as MusicBean
                val service = RouteManager.getAny(ARouterUrl.MUSIC_SERVICE) as Service
                val intent = Intent(context, service::class.java)
                if (bean.isPlayer) {
                    intent.putExtra("close", "")
                    context.startService(intent)
                    mAdapter.data[position].isPlayer = false
                    mAdapter.notifyItemChanged(position)
                } else {
                    adapter.data.map { (it as MusicBean).isPlayer = false }
                    mAdapter.notifyDataSetChanged()
                    intent.putExtra("music", bean)
                    context.startService(intent)

                }
            }
        }
        mAdapter.setNewInstance(list)
        /*  mAdapter.removeAllHeaderView()
          var view=LayoutInflater.from(context).inflate(R.layout.child_item_sleep_music_txt,null,false)
          var tvContent=view.findViewById<TextView>(R.id.mContent)
          var  getter = URLImageParser(tvContent, context)
          tvContent.setText(Html.fromHtml(textStr, getter, null))
          mAdapter.addHeaderView(view)*/
    }

    private fun setLeftIcon(text: TextView, txt: String, icon: Int, context: Context) {
        text.text = txt
        val drawable = ContextCompat.getDrawable(context, icon)
        drawable!!.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        text.setCompoundDrawables(drawable, null, null, null)
    }
}