package com.miya.fastcashier.ui.adapter

import androidx.recyclerview.widget.RecyclerView
import com.miya.fastcashier.ui.adapter.PathAdapter.PathViewHolder
import android.view.ViewGroup
import com.miya.fastcashier.R
import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.*
import com.miya.fastcashier.util.FileUtils
import java.io.File
import java.io.FileFilter

class PathAdapter(
    private var mListData: List<File>,
    private val mContext: Context,
    private val mFileFilter: FileFilter,
    private val mMutilyMode: Boolean,
    private val mIsGreater: Boolean,
    private val mFileSize: Long
) : RecyclerView.Adapter<PathViewHolder>() {
    interface OnItemClickListener {
        fun click(position: Int)
    }

    interface OnCancelChoosedListener {
        fun cancelChoosed(checkBox: CheckBox?)
    }

    private val TAG = "FilePickerLeon"
    var onItemClickListener: OnItemClickListener? = null
    private var mCheckedFlags: BooleanArray
    private var mIconStyle = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PathViewHolder {
        val view =
            View.inflate(mContext, R.layout.layout_lfile_list_item, null)
        return PathViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mListData.size
    }

    override fun onBindViewHolder(
        holder: PathViewHolder,
        @SuppressLint("RecyclerView") position: Int
    ) {
        val file = mListData[position]
        if (file.isFile) {
            updateFileIconStyle(holder.ivType)
            holder.tvName.text = file.name
            holder.tvDetail.text =
                mContext.getString(R.string.lfile_FileSize) + " " + FileUtils.getReadableFileSize(
                    file.length()
                )
            holder.cbChoose.visibility = View.VISIBLE
        } else {
            updateFloaderIconStyle(holder.ivType)
            holder.tvName.text = file.name
            //文件大小过滤
            val files: List<*>? =
                FileUtils.getFileList(file.absolutePath, mFileFilter, mIsGreater, mFileSize)
            if (files == null) {
                holder.tvDetail.text = "0 " + mContext.getString(R.string.lfile_LItem)
            } else {
                holder.tvDetail.text =
                    files.size.toString() + " " + mContext.getString(R.string.lfile_LItem)
            }
            holder.cbChoose.visibility = View.GONE
        }
        if (!mMutilyMode) {
            holder.cbChoose.visibility = View.GONE
        }
        holder.layoutRoot.setOnClickListener {
            if (file.isFile) {
                holder.cbChoose.isChecked = !holder.cbChoose.isChecked
            }
            onItemClickListener!!.click(position)
        }
        holder.cbChoose.setOnClickListener { //同步复选框和外部布局点击的处理
            onItemClickListener!!.click(position)
        }
        holder.cbChoose.setOnCheckedChangeListener(null) //先设置一次CheckBox的选中监听器，传入参数null
        holder.cbChoose.isChecked = mCheckedFlags[position] //用数组中的值设置CheckBox的选中状态
        //再设置一次CheckBox的选中监听器，当CheckBox的选中状态发生改变时，把改变后的状态储存在数组中
        holder.cbChoose.setOnCheckedChangeListener { compoundButton, b ->
            mCheckedFlags[position] = b
        }
    }

    private fun updateFloaderIconStyle(imageView: ImageView) {
//        switch (mIconStyle) {
//            case Constant.ICON_STYLE_BLUE:
//                imageView.setBackgroundResource(R.mipmap.lfile_folder_style_blue);
//                break;
//            case Constant.ICON_STYLE_GREEN:
//                imageView.setBackgroundResource(R.mipmap.lfile_folder_style_green);
//                break;
//            case Constant.ICON_STYLE_YELLOW:
//                imageView.setBackgroundResource(R.mipmap.lfile_folder_style_yellow);
//                break;
//        }
    }

    private fun updateFileIconStyle(imageView: ImageView) {
//        switch (mIconStyle) {
//            case Constant.ICON_STYLE_BLUE:
//                imageView.setBackgroundResource(R.mipmap.lfile_file_style_blue);
//                break;
//            case Constant.ICON_STYLE_GREEN:
//                imageView.setBackgroundResource(R.mipmap.lfile_file_style_green);
//                break;
//            case Constant.ICON_STYLE_YELLOW:
//                imageView.setBackgroundResource(R.mipmap.lfile_file_style_yellow);
//                break;
//        }
    }

    /**
     * 设置监听器
     *
     * @param onItemClickListener
     */
    @JvmName("setOnItemClickListener1")
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        this.onItemClickListener = onItemClickListener
    }

    /**
     * 设置数据源
     *
     * @param mListData
     */
    fun setmListData(mListData: List<File>) {
        this.mListData = mListData
        mCheckedFlags = BooleanArray(mListData.size)
    }

    fun setmIconStyle(mIconStyle: Int) {
        this.mIconStyle = mIconStyle
    }

    /**
     * 设置是否全选
     *
     * @param isAllSelected
     */
    fun updateAllSelelcted(isAllSelected: Boolean) {
        for (i in mCheckedFlags.indices) {
            mCheckedFlags[i] = isAllSelected
        }
        notifyDataSetChanged()
    }

    inner class PathViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val layoutRoot: RelativeLayout
        val ivType: ImageView
        val tvName: TextView
        val tvDetail: TextView
        val cbChoose: CheckBox

        init {
            ivType = itemView.findViewById<View>(R.id.iv_type) as ImageView
            layoutRoot = itemView.findViewById<View>(R.id.layout_item_root) as RelativeLayout
            tvName = itemView.findViewById<View>(R.id.tv_name) as TextView
            tvDetail = itemView.findViewById<View>(R.id.tv_detail) as TextView
            cbChoose = itemView.findViewById<View>(R.id.cb_choose) as CheckBox
        }
    }

    init {
        mCheckedFlags = BooleanArray(mListData.size)
    }
}