package com.zxhyis.tools.ui.adapter

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.tools.R
import com.zxhyis.tools.logic.FileBean
import com.zxhyis.tools.logic.Logic
import com.zxhyis.tools.logic.OnItemClickListener
import com.zxhyis.tools.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
    }

    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
        }
        holder.itemCount.text =
            if (folder.file.isFile) " -- ${Formatter.formatFileSize(context, folder.fileSize)}"
            else " -- ${folder.file.list().size} item"

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

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

    }

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

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

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


}