package com.yidianling.course.widget

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.RecyclerView
import com.ydl.ydlcommon.base.BaseDialogFragment
import com.yidianling.course.R
import kotlinx.android.synthetic.main.dialog_course_speed.*
import kotlinx.android.synthetic.main.item_course_speed.view.*

/**
 * 课程倍速dialog
 */
class CourseSpeedDialog : BaseDialogFragment() {

    private val mSpeedList = floatArrayOf(0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 2.0f, 3.0f)
    var mListener: ((speed: Float) -> Unit)? = null
    private var mAdapter: CourseSpeedAdapter? = null
    private var mOldPosition = -1
    private var mCurrentPosition = 2

    override fun getLayoutResourceId(): Int = R.layout.dialog_course_speed
    override fun getGravity(): Int = Gravity.BOTTOM

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        context?.let {
            if (null == mAdapter) mAdapter = CourseSpeedAdapter(it)
            recycler_list.adapter = mAdapter
            tv_cancel.setOnClickListener {
                dismissAllowingStateLoss()
            }
        }
    }

    fun setSpeedPosition(speed: Float) {
        mCurrentPosition = mSpeedList.indexOf(speed)
    }

    inner class CourseSpeedAdapter(
        context: Context,
        private val inflater: LayoutInflater = LayoutInflater.from(context)
    ) : RecyclerView.Adapter<CourseSpeedAdapter.CourseSpeedVH>() {

        inner class CourseSpeedVH(view: View) : RecyclerView.ViewHolder(view)

        override fun onCreateViewHolder(viewGroup: ViewGroup, itemType: Int): CourseSpeedVH =
            CourseSpeedVH(inflater.inflate(R.layout.item_course_speed, viewGroup, false))

        override fun onBindViewHolder(vh: CourseSpeedVH, position: Int) {
            vh.itemView.let {
                mSpeedList.elementAtOrNull(position)?.let { speed ->
                    it.tv_speed.run {
                        setTextColor(resources.getColor(if (position == mCurrentPosition) R.color.color_1DA1F2 else R.color.color_1C1F28))
                        text = speed.toString()
                        setOnClickListener {
                            if (position == mCurrentPosition) {
                                dismissAllowingStateLoss()
                                return@setOnClickListener
                            }
                            mOldPosition = mCurrentPosition
                            mCurrentPosition = position
                            notifyItemChanged(mOldPosition)
                            notifyItemChanged(mCurrentPosition)
                            mListener?.invoke(speed)
                            dismissAllowingStateLoss()
                        }
                    }
                }
            }
        }

        override fun getItemCount(): Int = mSpeedList.size

    }

}