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 android.widget.LinearLayout
import android.widget.ListView
import android.widget.RelativeLayout
import android.widget.TextView
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.model.CleanBean
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.FileUtil

class CleanAdapter(
    fileBeanList: ArrayList<CleanBean>,
    context: Context,
) : BaseAdapter(){
    private var dataBeanList: ArrayList<CleanBean>
    private var context: Context
    private var inflater: LayoutInflater
    var mFileCheckedItemList = ArrayList<ArrayList<FileBean>>()

    private var sparseBooleanArray = ArrayList<SparseBooleanArray>()

    private var itemAdapters = ArrayList<ItemAdapter>()

    private lateinit var viewHolder: ViewHolder
    var mAllSizes = ArrayList<Long>()

    companion object{
        var isCleanOver = false
        var isClickAlls = ArrayList<Boolean>()
    }

    init {
        this.dataBeanList = fileBeanList
        this.context = context
        this.inflater = LayoutInflater.from(context)
    }
    override fun getCount(): Int {
        return dataBeanList.size
    }

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

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

    fun setData(fileBeanList: ArrayList<CleanBean>){
        this.dataBeanList = fileBeanList
        notifyDataSetChanged()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var view: View? = null

        if (convertView != null){
            view = convertView
            viewHolder = view.tag as ViewHolder
        }else{
            viewHolder = ViewHolder()
            view = inflater.inflate(R.layout.clean_list_item_layout, null)

            viewHolder.listView = view.findViewById(R.id.clean_item_list)
            viewHolder.itemNameTv = view.findViewById(R.id.clean_item_name)
            viewHolder.unfoldImg = view.findViewById(R.id.clean_item_unfold)
            viewHolder.moreImg = view.findViewById(R.id.clean_item_more)
            viewHolder.sizeTv = view.findViewById(R.id.file_item_size)
            viewHolder.checkBox = view.findViewById(R.id.clean_item_check_box)
            viewHolder.itemBar = view.findViewById(R.id.clean_item_bar)
            view.tag = viewHolder
        }
        viewHolder.itemNameTv?.text = dataBeanList[position].typeName
        sparseBooleanArray.add(SparseBooleanArray())
        mFileCheckedItemList.add(ArrayList())


        val layoutParams = viewHolder.listView?.layoutParams as LinearLayout.LayoutParams

        itemAdapters.add(ItemAdapter(context, dataBeanList[position].fileBeans, sparseBooleanArray[position], position))

        viewHolder.listView?.visibility = if (!isCleanOver) View.GONE else View.VISIBLE
        viewHolder.unfoldImg?.visibility = if (!isCleanOver) View.GONE else View.VISIBLE
        viewHolder.sizeTv?.visibility = if (!isCleanOver) View.GONE else View.VISIBLE
        viewHolder.checkBox?.visibility = if (!isCleanOver) View.GONE else View.VISIBLE
        viewHolder.moreImg?.visibility = if (isCleanOver) View.GONE else View.VISIBLE
        var allSize: Long = 0
        for (fileBean in dataBeanList[position].fileBeans){
            allSize += fileBean.fileSize
        }
        viewHolder.sizeTv?.text =  if (allSize > 1024 * 1024 * 1024){
            "${allSize / 1024 / 1024 / 1024} GB"
        }else if (allSize > 1024 * 1024){
            "${allSize / 1024 / 1024} MB"
        }else if (allSize > 1024){
            "${allSize / 1024} KB"
        }else{
            "${allSize} B"
        }
        mAllSizes.add(allSize)

        layoutParams.height = FileUtil.dpToPx(62*itemAdapters[position]?.count!!, context)
        viewHolder.listView?.adapter = itemAdapters[position]
        viewHolder.itemBar?.setOnClickListener {
//            Log.e("yanglin", "setViewOnclickListener: 11111111", )
            dataBeanList[position].isListViewShow = !dataBeanList[position].isListViewShow
//            viewHolder.listView?.visibility = if (dataBeanList[position].isListViewShow) View.GONE else View.VISIBLE
            notifyDataSetChanged()
        }
//        viewHolder.listView?.visibility = if (!dataBeanList[position].isListViewShow) View.GONE else View.VISIBLE
        viewHolder.listView?.setOnItemClickListener { parent, view, itemPosition, id ->
            if (isCleanOver)
                updateCheckBoxStatus(position, view, itemPosition, viewHolder.listView!!, dataBeanList[position].fileBeans)

        }

        viewHolder.checkBox?.setOnClickListener {
            viewHolder.checkBox?.isChecked = !viewHolder.checkBox?.isChecked!!
            selectAll(dataBeanList[position].fileBeans, viewHolder.checkBox?.isChecked!!, position)
            notifyDataSetChanged()

        }


        return view!!

    }

    fun selectAll(fileBeanList: ArrayList<FileBean>, isChecked: Boolean, position: Int){
        mFileCheckedItemList[position].clear()
        setStateCheckedMap(position, isChecked)
        mFileCheckedItemList[position].addAll(fileBeanList)
    }
    fun setStateCheckedMap(position: Int,isChecked: Boolean){
        for(i in dataBeanList[position].fileBeans.indices){
            sparseBooleanArray[position].put(i, isChecked)
            viewHolder.listView?.setItemChecked(i, isChecked)
        }
    }
    fun updateCheckBoxStatus(position: Int, view: View, itemPosition: Int, lv: ListView, fileBeanList: ArrayList<FileBean>){
        val viewHolder = view.tag as ItemAdapter.ItemViewHolder
        viewHolder.selectSwitch.toggle()
        lv.setItemChecked(itemPosition, viewHolder.selectSwitch.isChecked)
        sparseBooleanArray[position].put(itemPosition, viewHolder.selectSwitch.isChecked)
        if (viewHolder.selectSwitch.isChecked){
            mFileCheckedItemList[position].add(fileBeanList[itemPosition])
        }else{
            mFileCheckedItemList[position].remove(fileBeanList[itemPosition])
        }
        itemAdapters[position]?.notifyDataSetChanged()
    }


    class ViewHolder{
        var itemNameTv: TextView? = null
        var unfoldImg: ImageView? = null
        var moreImg: ImageView? = null
        var sizeTv: TextView? = null
        var checkBox: CheckBox? = null
        var listView: ListView? = null
        var itemBar: View? = null
    }

    private class ItemAdapter(
        context: Context,
        dataList: ArrayList<FileBean>,
        sparseBooleanArray: SparseBooleanArray,
        itemPosition: Int
    ): BaseAdapter(){
        private var context: Context
        private var inflater: LayoutInflater
        private var dataList: ArrayList<FileBean>
        private var sparseBooleanArray: SparseBooleanArray
        private var itemPosition: Int
        init {
            this.context = context
            this.dataList = dataList
            this.inflater = LayoutInflater.from(context)
            this.sparseBooleanArray = sparseBooleanArray
            this.itemPosition = itemPosition
        }
        override fun getCount(): Int {
            return dataList.size
        }

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

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

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var itemViewHolder: ItemViewHolder
            var view: View
            if (convertView != null){
                view = convertView
                itemViewHolder = view.tag as ItemViewHolder
            }else{
                itemViewHolder = ItemViewHolder()
                view = inflater.inflate(R.layout.files_item_layout, null)
                itemViewHolder.fileIcon = view.findViewById(R.id.files_item_icon)
                itemViewHolder.fileJoinTo = view.findViewById(R.id.file_join_to_img)
                itemViewHolder.fileNone = view.findViewById(R.id.file_item_none)
                itemViewHolder.fileName = view.findViewById(R.id.file_item_name)
                itemViewHolder.selectSwitch = view.findViewById(R.id.file_select_switch)
                itemViewHolder.itemLayout = view.findViewById(R.id.item_layout)
                view.tag = itemViewHolder
            }

            itemViewHolder.fileJoinTo.visibility = View.GONE
            itemViewHolder.selectSwitch.visibility = if (!isCleanOver) View.GONE else View.VISIBLE

            itemViewHolder.selectSwitch.isChecked = sparseBooleanArray[position]
            when(itemPosition){
                0 ->{
                    itemViewHolder.fileName.text = dataList[position].fileName
                    itemViewHolder.fileNone.text = dataList[position].filePath
                }
                1 ->{
                    itemViewHolder.fileIcon.setImageDrawable(dataList[position].drawable)
                    itemViewHolder.fileName.text = dataList[position].fileName
                    itemViewHolder.fileNone.text = dataList[position].filePath

                }
                3 ->{
//                    itemViewHolder.fileIcon.setImageDrawable(dataList[position].drawable)
                    itemViewHolder.fileIcon.setImageResource(R.drawable.ic_log_file)
                    itemViewHolder.fileName.text = dataList[position].fileName
                    itemViewHolder.fileNone.text = dataList[position].hint
                }
                4 ->{
                    itemViewHolder.fileIcon.setImageResource(R.drawable.ic_log_file)
                    itemViewHolder.fileName.text = dataList[position].fileName
                    itemViewHolder.fileNone.text = dataList[position].filePath
                }
            }
            itemViewHolder.fileIcon
            itemViewHolder.fileName
            itemViewHolder.fileNone

            if (isCleanOver) View.GONE else View.VISIBLE
            return view
        }

        class ItemViewHolder{
            lateinit var fileIcon: ImageView
            lateinit var fileJoinTo: ImageView
            lateinit var fileName: TextView
            lateinit var fileNone: TextView
            lateinit var selectSwitch: CheckBox
            lateinit var itemLayout: RelativeLayout
        }

    }

}