package com.yidianling.course.courseNew.courseList.widget

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import com.ydl.ydlcommon.view.SpaceItemDecorator
import com.yidianling.common.tools.RxImageTool
import com.yidianling.course.R
import com.yidianling.course.courseNew.courseList.widget.adapter.CourseGoodAdapter
import com.yidianling.course.model.CourseCategoryBean
import kotlinx.android.synthetic.main.course_good_popup_window.view.*


/**
 * @author yuanwai
 * @描述:课程列表页-分类popWindow   目前逻辑只支持单选
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/9/22
 */
class CourseGoodPopupWindow(context: Context, allCourseList: ArrayList<CourseCategoryBean>, selectedCourseList: ArrayList<CourseCategoryBean>,
                            var onGoodSelectedListener: OnGoodSelectedListener)
    : PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
    //擅长方向列表数据
    private var goodsList: ArrayList<CourseCategoryBean>? = null
    //已选择的擅长方向数据
    private var selectedGoodsList: ArrayList<CourseCategoryBean>? = null

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.course_good_popup_window, null)
        this.contentView = view
        this.isFocusable = true
        @Suppress("DEPRECATION")
        this.setBackgroundDrawable(BitmapDrawable())
        this.isOutsideTouchable = true
        val rvGood = view.rvGood
        val tvTitle = view.tv_title
        tvTitle.text = "课程类型"
        if (null == this.goodsList) {
            this.goodsList = ArrayList()
        } else {
            (this.goodsList as ArrayList).clear()
        }
        this.goodsList!!.addAll(allCourseList)
        if (null == this.selectedGoodsList) {
            this.selectedGoodsList = ArrayList()
        } else {
            (this.selectedGoodsList as ArrayList).clear()
        }
        this.selectedGoodsList!!.addAll(selectedCourseList)
        var goodAdapter: CourseGoodAdapter? = null
        if (null == rvGood.adapter) {
            goodAdapter = CourseGoodAdapter(context, this.goodsList!!, this.selectedGoodsList!!)
            rvGood.adapter = goodAdapter
            rvGood.layoutManager =
                GridLayoutManager(
                    context,
                    3,
                    LinearLayoutManager.VERTICAL,
                    false
                )
            rvGood.addItemDecoration(SpaceItemDecorator(RxImageTool.dp2px(8f), 3))
        }
        inputMethodMode = PopupWindow.INPUT_METHOD_NEEDED
        view.btnConfirm.setOnClickListener {
            onGoodSelectedListener?.onGoodSelected(this.selectedGoodsList!!)
            dismiss()
        }
        view.btnReset.setOnClickListener {
            goodAdapter?.cleanSelectedGoods()
        }
        view.v_zhezhao.setOnClickListener {
            dismiss()
        }
    }

    interface OnGoodSelectedListener {
        fun onGoodSelected(selectedGoodsList: ArrayList<CourseCategoryBean>)
    }
}