package com.yidianling.course.coursePlay.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import com.ydl.ydl_image.module.GlideApp
import com.yidianling.course.R
import com.yidianling.ydlcommon.adapter.CommonRecyclerAdapter
import com.yidianling.ydlcommon.view.BaseViewHolder


/**
 * 课程播放列表dialog
 * Created by harvie on 2017/6/29 0029.
 */
class CoursePlayListDialog : Dialog {

    var mContext: Context? = null
    var recyList: RecyclerView? = null
    var textClose: TextView? = null

    var index = 0 //当前播放下标

    var itemListener: ItemClickListener? = null

    var adapter: CommonRecyclerAdapter<CoursePlayListBean>? = null

    constructor(context: Context?) : super(context, R.style.commonDialog) {
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.dialog_course_playlist)

        val m = window.windowManager
        val d = m.defaultDisplay
        val p = window.attributes
        p.width = d.width //设置dialog的宽度为当前手机屏幕的宽度
        p.height = (d.height * 5 / 10)
        window.attributes = p
        window.setGravity(Gravity.BOTTOM)

        initView()
    }

    //跟新当前播放下标
    fun updateIndex(inde: Int) {
        index = inde
        adapter?.notifyDataSetChanged()
    }

    fun initView() {
        recyList = findViewById<RecyclerView>(R.id.recy_list)
        textClose = findViewById<TextView>(R.id.text_close)
        textClose?.setOnClickListener {
            dismiss()
        }

        recyList?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        adapter = CommonRecyclerAdapter<CoursePlayListBean>(recyList, R.layout.dialog_course_playlist_item)
                .setBindDataCallback(object : CommonRecyclerAdapter.BindDataCallback<CoursePlayListBean> {
                    override fun bindDatabindData(holder: BaseViewHolder, t: CoursePlayListBean, position: Int) {
                        var gifView: ImageView = holder.getView(R.id.gifView)

                        if (index == position) {
                            GlideApp.with(context).load(R.drawable.music).into(gifView)
                            gifView.visibility = View.VISIBLE

                            holder.setTextColor(R.id.text_title, context.resources.getColor(R.color.google_green))
                        } else {
                            gifView.visibility = View.GONE
                            holder.setTextColor(R.id.text_title, context.resources.getColor(R.color.black_deep))
                        }

                        var test: TextView = holder.getView(R.id.text_test)
                        if (t.isTestPlay) {
                            test.visibility = View.VISIBLE
                        } else {
                            test.visibility = View.GONE
                        }

                        holder.setText(R.id.text_title, t.title)
                        holder.setText(R.id.text_time, "" + t.playTime)

                        holder.setOnClickListener(R.id.lin_body) {
                            //item点击事件
                            var boo = itemListener?.itemClick(position) ?: false
                            if (boo) {
                                index = position
                                adapter?.notifyDataSetChanged()
                            }
                        }
                    }

                })
        recyList?.adapter = adapter
    }

    fun scrollToPosition(i: Int) {
        recyList?.scrollToPosition(i)
    }

    //设置item点击事件
    fun setItemClickListener(itemListener: ItemClickListener): CoursePlayListDialog {
        this.itemListener = itemListener
        return this
    }

    //设置列表数据
    fun setList(playIndex: Int, list: List<CoursePlayListBean>?) {
        this.index = playIndex
        if (list != null) {
            for (i in list.indices) {
                list[i].isPlay = i == playIndex
            }
            adapter?.update(list)
        }

    }

    //跟新列表状态
    fun updateAdapter() {
        adapter?.notifyDataSetChanged()
    }

    //列表点击事件
    interface ItemClickListener {
        fun itemClick(indec: Int): Boolean
    }

    override fun show() {
        if (null == mContext) {
            return
        }
        if (mContext is Activity) {
            if ((mContext as Activity).isFinishing) {
                return
            }
        }
        super.show()
    }
}