package com.xm.test.myfilemaster.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.util.Log
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.GridView
import android.widget.ImageView
import android.widget.TextView
import com.cloud.cleanjunksdk.similar.PicSimilarInfo
import com.xm.test.myfilemaster.R
import com.xm.test.myfilemaster.activity.SimilarImagesActivity
import com.xm.test.myfilemaster.model.FileBean
import com.xm.test.myfilemaster.util.FileUtil

class ImageItemAdapter(mContext: Context
                       , mDataList: ArrayList<ArrayList<FileBean>>): BaseAdapter() {

    private var mContext: Context

    private var mDataList: ArrayList<ArrayList<FileBean>>

    private var mInfler: LayoutInflater

    private var mItemCheckedArray =  ArrayList<SparseBooleanArray>()
    private lateinit var mImageViewHolder: ViewHolder
    var itemAdapter = ArrayList<ItemAdapter>()

    var mCheckedLists = ArrayList<FileBean>()
    init {
        this.mInfler = LayoutInflater.from(mContext)
        this.mContext = mContext
        this.mDataList = mDataList
    }

    override fun getCount(): Int {
        Log.e("yanglin", "getCount: mdatalist ${mDataList.size}", )
        return mDataList.size
    }

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

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var view = convertView
        if (convertView != null){
            mImageViewHolder = view?.tag as ViewHolder
        }else{
            mImageViewHolder = ViewHolder()
            view = mInfler.inflate(R.layout.image_item_layout, null)
            mImageViewHolder.similar = view.findViewById(R.id.similar_image_num_text)
            mImageViewHolder.itemGrid = view.findViewById(R.id.image_item_grid)
            view.tag = mImageViewHolder
        }

        if (mItemCheckedArray.size != mDataList.size){
            mItemCheckedArray.add(SparseBooleanArray())
//            itemAdapter.add(ItemAdapter(mDataList[position], mContext, mItemCheckedArray[position], position))
        }

        mImageViewHolder.similar.text =
            String.format(mContext.getString(R.string.similar, mDataList[position].size.toString()))

        val layoutParams = mImageViewHolder.itemGrid.layoutParams

        if (itemAdapter[position].count.toFloat() / 4.0 == 1.0){
            layoutParams.height = FileUtil.dpToPx(83*(itemAdapter[position].count / 4), mContext)
        }else{
            layoutParams.height = FileUtil.dpToPx(83*(itemAdapter[position].count / 4 + 1 ), mContext)

        }
        mImageViewHolder.itemGrid.setOnItemClickListener { itemParent, itemView, itemPosition, itemId ->

            if (SimilarImagesActivity.isClickEditBtn){
                println("yanglin positioin $position  itemPostion $itemPosition")
                updateCheckBoxStatus(position, itemView, itemPosition, mImageViewHolder.itemGrid, mDataList[position])
            }
        }
        mImageViewHolder.itemGrid.adapter = itemAdapter[position]

        return view!!
    }
    fun selectAll(isChecked: Boolean){
        Log.e("yanglin", "selectAll: $isChecked", )
        mCheckedLists.clear()
        if (!isChecked){
            for (i in 0..< mDataList.size){
                mItemCheckedArray[i].clear()
            }
        }else{
            for (i in 0..< mDataList.size){
                mCheckedLists.clear()
                setStateCheckedMap(i, isChecked)
                mCheckedLists.addAll(mDataList[i])
                itemAdapter[i].notifyDataSetChanged()
            }
        }
    }
    fun setStateCheckedMap(position: Int,isChecked: Boolean){
        for(i in mDataList[position].indices){
            mItemCheckedArray[position].put(i, isChecked)
            mImageViewHolder.itemGrid.setItemChecked(i, isChecked)
        }
    }
    fun updateCheckBoxStatus(position: Int, view: View, itemPosition: Int, lv: GridView, fileBeanList: ArrayList<FileBean>){
        val viewHolder = view.tag as ItemAdapter.ViewHolder
        viewHolder.checkBox.toggle()
        lv.setItemChecked(itemPosition, viewHolder.checkBox.isChecked)
        mItemCheckedArray[position].put(itemPosition, viewHolder.checkBox.isChecked)
        if (viewHolder.checkBox.isChecked){
            mCheckedLists.add(fileBeanList[itemPosition])
        }else{
            mCheckedLists.remove(fileBeanList[itemPosition])
        }
        itemAdapter[position].notifyDataSetChanged()
    }
    fun setData(datas: ArrayList<ArrayList<FileBean>>){
        this.mDataList = datas
        notifyDataSetChanged()
    }
    class ViewHolder{
        lateinit var similar: TextView
        lateinit var itemGrid: GridView
    }

    class ItemAdapter(list: ArrayList<PicSimilarInfo>, context: Context,
                              mStateCheckedMap: SparseBooleanArray, mPosition: Int): BaseAdapter(){

        private var context: Context
        private var dataList: ArrayList<PicSimilarInfo>
        private var stateCheckedMap: SparseBooleanArray
        private var inflater: LayoutInflater
        private var mPosition: Int
        init {
            this.context = context
            this.dataList = list
            this.stateCheckedMap = mStateCheckedMap
            this.inflater = LayoutInflater.from(context)
            this.mPosition = mPosition
        }

        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 view = convertView
            val itemHolder: ViewHolder
            if (convertView != null){
                itemHolder = view?.tag as ViewHolder
            }else{
                view = inflater.inflate(R.layout.item_layout, null)
                itemHolder = ViewHolder()
                itemHolder.imageView = view.findViewById(R.id.item_image)
                itemHolder.checkBox = view.findViewById(R.id.check_item_image_switch)
                view.tag = itemHolder
            }
//            BitmapDrawable(context.resources, BitmapFactory.decodeFile(dataList[position].filePath))
            itemHolder.checkBox.visibility = if (SimilarImagesActivity.isClickEditBtn) View.VISIBLE else View.GONE
            itemHolder.imageView.setImageDrawable(BitmapDrawable(context.resources, BitmapFactory.decodeFile(dataList[mPosition].getmList()[position].path)))
            itemHolder.imageView.layoutParams.height = FileUtil.dpToPx(83, context)

//            if (DuplicateImagesActivity.isClickCheckBoxAllBtn){
//                itemHolder.checkBox.isChecked = true
//            }else{
            itemHolder.checkBox.isChecked = stateCheckedMap[position]
//            }

            return view!!

        }

        fun setData(datas: ArrayList<PicSimilarInfo>){
            this.dataList = datas
        }

        class ViewHolder{
            lateinit var checkBox: CheckBox
            lateinit var imageView: ImageView
        }
    }

}