package com.zj365.health.weight

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.HORIZONTAL
import com.zj365.health.R
import com.zj365.health.adapter.HealthStatusAdapter
import com.xty.network.model.ReportBean

class PopWindowUserInfo {

    private var recycleView: RecyclerView? = null
    private var mAdapter: HealthStatusAdapter? = null
    private var popupWindow: PopupWindow? = null
    var context: Context? = null
    fun myPopWindow(context: Context, @LayoutRes layout: Int) {
        this.context = context
        var popView = LayoutInflater.from(context).inflate(layout, null)


        changeTopShape(context, R.color.col_009, popView.findViewById(R.id.status_1))
        changeTopShape(context, R.color.col_e0b, popView.findViewById(R.id.status_2))
        changeTopShape(context, R.color.col_ea7, popView.findViewById(R.id.status_3))
        changeTopShape(context, R.color.col_c3d, popView.findViewById(R.id.status_4))

        recycleView = popView.findViewById(R.id.mRecycle)




        popupWindow = PopupWindow(
            popView,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            false
        )
        popupWindow!!.isOutsideTouchable = true
        popupWindow!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        popupWindow!!.isTouchable = true
    }

    /**
     * dayStaus   1 日 2 周 3 月 4 年
     */
    fun showAction(view: View, list: List<ReportBean.ChildStatus>, dayStatus: Int) {
        popupWindow?.let {
            it.setOnDismissListener {
                view.isSelected = false
            }
            it.showAsDropDown(view, Gravity.TOP, 0, 0)

            recycleView?.post {
                mAdapter = HealthStatusAdapter(recycleView!!.width,dayStatus)
                recycleView!!.adapter = mAdapter
                recycleView!!.layoutManager = LinearLayoutManager(context, HORIZONTAL, false)
                if(list.isEmpty()){
                    mAdapter!!.setNewInstance(mutableListOf(ReportBean.ChildStatus()))
                }
                else{
                    mAdapter!!.setNewInstance(list as MutableList)
                }
            }


        }
    }

    private fun changeTopShape(context: Context, color: Int, textView: TextView) {
        val drawable = ContextCompat.getDrawable(context, R.drawable.shape_status)
        val gradientDrawable = drawable as GradientDrawable
        gradientDrawable.setColor(ContextCompat.getColor(context, color))
        drawable.mutate()
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        textView.setCompoundDrawables(null, drawable, null, null)
    }


}