package com.xm.test.myfilemaster.adapter

import android.content.Context
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.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.activity.MediaActivity
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.FileUtil

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)
            Glide.with(mContext).load(mListData[position].filePath).centerCrop().into(mViewHolder.image)
            mViewHolder.image.layoutParams.height = dpToPx

        }else{
//            mViewHolder.image.setImageResource(R.drawable.ic_video)
            Glide.with(mContext).load(mListData[position].filePath).centerCrop().into(mViewHolder.image)
            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
    }
}