package com.test.mydemo3.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 android.widget.TextView
import com.airbnb.lottie.utils.Utils
import com.test.mydemo3.R
import com.test.mydemo3.base.BaseActivity
import com.test.mydemo3.model.FileBean
import com.test.mydemo3.util.FileUtil

class FilesItemViewAdapter(
    mItemFileList: List<FileBean>,mContext: Context,
    stateCheckedMap: SparseBooleanArray
    ): BaseAdapter() {
    private var mFileBeanList: List<FileBean>
    private var mInflater: LayoutInflater
    private var mContext: Context
    private var mStateCheckedMap: SparseBooleanArray
    private var isEditBtnClick = false
    private var isFileShow = true
    init {
        this.mFileBeanList = mItemFileList
        this.mContext = mContext
        this.mInflater = LayoutInflater.from(mContext)
        this.mStateCheckedMap = stateCheckedMap
    }

    fun setData(fileList: List<FileBean>){
        this.mFileBeanList = fileList
        this.notifyDataSetChanged()
    }
    override fun getCount(): Int {

        return mFileBeanList.size
    }

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

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var viewHolder: ViewHolder? = null
        var view : View? = null

        if (convertView != null){
            view = convertView
            viewHolder = view.tag as ViewHolder
        }else{
            view = mInflater.inflate(R.layout.files_item_layout, null)
            viewHolder = ViewHolder()
            viewHolder.fileIcon = view.findViewById(R.id.files_item_icon)
            viewHolder.fileJoinTo = view.findViewById(R.id.file_join_to_img)
            viewHolder.fileName = view.findViewById(R.id.file_item_name)
            viewHolder.fileNone = view.findViewById(R.id.file_item_none)
            viewHolder.selectSwitch = view.findViewById(R.id.file_select_switch)
            view.tag = viewHolder
        }
        val filesItemBean = mFileBeanList[position]

        viewHolder.fileName.text = filesItemBean.fileName
        if (filesItemBean.isDir){
            viewHolder.fileIcon.setImageResource(R.drawable.ic_files)
            viewHolder.fileJoinTo.visibility = if(isEditBtnClick) View.GONE else View.VISIBLE
            viewHolder.selectSwitch.visibility = if(isEditBtnClick) View.VISIBLE else View.GONE
            val (year, month, day) = FileUtil.getFileCreateTime(filesItemBean.filePath)
            viewHolder.fileNone.text = "${filesItemBean.childList.size} item | $month-$day, $year"
        }else {
            val icon = when(filesItemBean.fileType){
                "png","jpg"->{R.drawable.ic_image}
                "mp4", "3gp", "avi"->{R.drawable.ic_video}
                "mp3","aac","wav"->{R.drawable.ic_audio}
                "zip"->{R.drawable.ic_zip}
                "apk"->{R.drawable.ic_apk}
                "log"->{R.drawable.ic_log_file}
                "doc","docm","docx"->{R.drawable.ic_word}
                "xlsx", "xlsm", "xlsb", "xltx"->{R.drawable.ic_excel}
                "ppt"->{R.drawable.ic_ppt}
                "pdf"->{R.drawable.ic_pdf}
                "txt"->{R.drawable.ic_txt}
                else -> {R.drawable.ic_copy}
            }
            viewHolder.fileIcon.setImageResource(icon)
            viewHolder.fileJoinTo.visibility = View.GONE
            viewHolder.selectSwitch.visibility = if(isEditBtnClick) View.VISIBLE else View.GONE
            var sizeStr : String = ""
            sizeStr = if (filesItemBean.fileSize > 1024 * 1024 * 1024){
                "size ${filesItemBean.fileSize / 1024 / 1024 / 1024} GB"
            }else if (filesItemBean.fileSize > 1024 * 1024){
                "size ${filesItemBean.fileSize / 1024 / 1024} MB"
            }else if (filesItemBean.fileSize > 1024){
                "size ${filesItemBean.fileSize / 1024} KB"
            }else{
                "size ${filesItemBean.fileSize} B"
            }
            viewHolder.fileNone.text = "$sizeStr,type ${filesItemBean.fileType}"
        }

        viewHolder.selectSwitch.isChecked = mStateCheckedMap[position]
//        viewHolder.selectSwitch.visibility =
//            if (BaseActivity.isClickEditBtn) View.VISIBLE else View.GONE
//        viewHolder.fileJoinTo.visibility =
//            if (!BaseActivity.isClickEditBtn) View.VISIBLE else View.GONE

        return view!!
    }
    fun setEditClickState(isClick: Boolean){
        this.isEditBtnClick = isClick
    }
    fun setFileShow(isShow: Boolean){
        isFileShow = isShow
    }

    class ViewHolder{
        lateinit var fileIcon: ImageView
        lateinit var fileJoinTo: ImageView
        lateinit var fileName: TextView
        lateinit var fileNone: TextView
        lateinit var selectSwitch: CheckBox
    }
}