package com.zxhyis.toolss.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.text.format.Formatter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.zxhyis.toolss.R
import com.zxhyis.toolss.logic.FileBean
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.OnItemClickListener
import com.zxhyis.toolss.logic.setOnClick

class FileAdapter(
    private val dataList: List<FileBean>,
    private val context: Context,
    private val itemClickListener: OnItemClickListener,
    private val logic: Logic
) : RecyclerView.Adapter<FileAdapter.FileVH>() {

    inner class FileVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val fileImage: AppCompatImageView = itemView.findViewById(R.id.file_image)
        val fileName: AppCompatTextView = itemView.findViewById(R.id.file_name)
        val fileTime: AppCompatTextView = itemView.findViewById(R.id.file_time)
        val itemCount: AppCompatTextView = itemView.findViewById(R.id.item_count)
        val icNext: AppCompatImageView = itemView.findViewById(R.id.ic_next)
        val fileSelect: AppCompatImageView = itemView.findViewById(R.id.file_select)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileVH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_file, parent, false)
        return FileVH(view)
    }

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

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: FileVH, position: Int) {
        val folder = dataList[position]
        if (folder.file.isDirectory) {
            holder.fileImage.setImageResource(R.drawable.ic_file)
        } else {
            if (folder.file.isFile) {
                when {
                    folder.file.name.endsWith(".apk") -> {
                        holder.fileImage.setImageResource(R.drawable.home_apk)
                    }

                    folder.file.name.endsWith(".png") or
                            folder.file.name.endsWith(".jpg") or
                            folder.file.name.endsWith(".jpeg") -> {
                        holder.fileImage.setImageResource(R.drawable.home_picture)
                    }

                    folder.file.name.endsWith(".zip") -> {
                        holder.fileImage.setImageResource(R.drawable.home_zip)
                    }

                    folder.file.name.endsWith(".mp4") or
                            folder.file.name.endsWith(".3gp") or
                            folder.file.name.endsWith(".mkv") or
                            folder.file.name.endsWith(".avi") -> {
                        holder.fileImage.setImageResource(R.drawable.home_video)
                    }

                    folder.file.name.endsWith(".m4a") or
                            folder.file.name.endsWith(".mp3") or
                            folder.file.name.endsWith(".wav") -> {
                        holder.fileImage.setImageResource(R.drawable.home_audio)
                    }

                    else -> {
                        holder.fileImage.setImageResource(R.drawable.ic_file)
                    }
                }

            }
        }
        holder.fileName.text = folder.fileName
        holder.fileTime.text = folder.fileTime
        if (folder.isShow) {
            holder.fileSelect.visibility = View.VISIBLE
            holder.icNext.visibility = View.GONE
        } else {
            holder.fileSelect.visibility = View.GONE
            holder.icNext.visibility = View.GONE
        }

        if (folder.file.isDirectory) {
            if (folder.file.listFiles() == null) {
                holder.itemCount.text = " -- 0 item"
            } else {
                holder.itemCount.text = " -- ${folder.file.list().size} item"
            }
        } else {
            holder.itemCount.text = " -- ${Formatter.formatFileSize(context, folder.fileSize)}"
        }


        holder.fileSelect.setOnClick(javaClass.simpleName) {
            dataList[position].isSelected = !dataList[position].isSelected
            logic.doLogic()
            notifyItemChanged(position)
        }

        holder.itemView.setOnClickListener {
            when (position) {
                position -> {
                    if (!folder.isShow) {
                        if (folder.file.isDirectory) {
                            itemClickListener.onItemClick(folder.file)
                        }
                    }
                }
            }
        }
    }


        fun setShow() {
            dataList.forEach {
                it.isShow = true
            }
            notifyDataSetChanged()
        }

        fun setNoShow() {
            dataList.forEach {
                it.isShow = false
            }
            notifyDataSetChanged()
        }

        fun setAllSelected() {
            dataList.forEach {
                it.isSelected = true
            }
            notifyDataSetChanged()
        }

        fun setEmptySelected() {
            dataList.forEach {
                it.isSelected = false
            }
            notifyDataSetChanged()
        }

}