package com.test.mydemo3.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.os.Looper
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.CheckBox
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.test.mydemo3.R
import com.test.mydemo3.activity.MediaActivity
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.util.FileUtil
import com.test.mydemo3.util.MediaUtil
import java.util.logging.Handler

class MediaAdapter(
    mListData: ArrayList<FileBean>,
    mContext: Context,
    mStateCheckedMap: SparseBooleanArray,
    fileType: String,
    mItemHeight: Int
): BaseAdapter() {

    private var mListData: ArrayList<FileBean>
    private var mContext: Context
    private var mInflater: LayoutInflater
    private var mStateCheckedMap: SparseBooleanArray
    private var mFileType: String
    private var mItemHeight: Int = 0
    private var dpToPx = 0
    init {
        this.mListData = mListData
        this.mContext = mContext
        this.mStateCheckedMap = mStateCheckedMap
        this.mInflater = LayoutInflater.from(mContext)
        this.mFileType = fileType
        this.mItemHeight =mItemHeight
        this.dpToPx = FileUtil.dpToPx(mItemHeight, mContext)
    }
    override fun getCount(): Int {

        return mListData.size
    }

    override fun getItem(position: Int): Any {
        return mListData[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var mViewHolder: ViewHolder
        var view:View? = convertView
        if (view != null){
            mViewHolder = view.tag as ViewHolder
        }else{
            mViewHolder = ViewHolder()
            view = mInflater.inflate(R.layout.item_layout,null)

            mViewHolder.image = view.findViewById(R.id.item_image)
            mViewHolder.checkedBox = view.findViewById(R.id.check_item_image_switch)
            view.tag = mViewHolder
        }

        if (mFileType.equals("image")){
//            mViewHolder.image.setImageDrawable(mListData[position].drawable)
            mViewHolder.image.layoutParams.height = dpToPx

        }else{
            mViewHolder.image.setImageResource(R.drawable.ic_video)
            mViewHolder.image.layoutParams.height = dpToPx
        }

        mViewHolder.checkedBox.visibility = if (MediaActivity.isClickEditBtn) View.VISIBLE else View.GONE

        mViewHolder.checkedBox.isChecked = mStateCheckedMap[position]

        return view!!
    }
    fun setItemHeight(height: Int){
        this.mItemHeight = height
    }
    fun setData(data: ArrayList<FileBean>) {
        this.mListData = data
        this.notifyDataSetChanged()
    }

    class ViewHolder{
        lateinit var image: ImageView
        lateinit var checkedBox: CheckBox
    }
}