package com.zxhyis.toolss.ui.page

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.zxhyis.toolss.FileApp
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.AdmobUtils
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.bean.ImageDataBean
import com.zxhyis.toolss.cx.FileHelps
import com.zxhyis.toolss.databinding.ActivityLayoutRepeaterdPhotoBinding

class RepeaterdPhotoActivity:TopActivity<ActivityLayoutRepeaterdPhotoBinding>() {
    private var isSelectAll = false
    override fun getViewBinding1(): ActivityLayoutRepeaterdPhotoBinding {
        return ActivityLayoutRepeaterdPhotoBinding.inflate(layoutInflater)
    }

    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        vb.idTvDelete.isEnabled = false
        initListener()
    }
    private fun initListener(){
        vb.idBack.setOnClickListener {
            finish()
        }
        vb.idImgSelect.setOnClickListener {
            if (isSelectAll) {
                isSelectAll = false
                vb.idTvAll.text = "All"
                vb.idImgSelect.setImageResource(R.drawable.cfzps2)
            } else {
                isSelectAll = true
                vb.idTvAll.text = "Cancel"
                vb.idImgSelect.setImageResource(R.drawable.xuanzhong2232)
            }
            fileList.forEach { it.isSelect = isSelectAll }
            mAdapter.notifyItemRangeChanged(0, fileList.size, 1)
            setTvStatus()
        }
        vb.idTvDelete.setOnClickListener {
            AdmobUtils.showInterstitialAd(this){
                val list = fileList.toList().filter { it.isSelect }
                if (list.isNotEmpty() && list.all { FileUtils.delete(it.path) }) {
                    fileList.removeAll(list)
                    FileHelps.similarImageList.removeAll(list)
                    mAdapter.notifyDataSetChanged()
                    setTvStatus()
                    ToastUtils.showShort("Delete Successful")
                } else {
                    ToastUtils.showShort("Delete failed.")
                }
            }

        }
    }
    private var fileList = mutableListOf<ImageDataBean>()
    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    override fun onPermissionsResult(isGranted: Boolean) {
        if(isGranted){
            FileHelps.getImageFiles {
                AdmobUtils.showInterstitialAd(this)
                vb.idLlScaning.isVisible = false
                vb.idLlResult.isVisible = true
                fileList = FileHelps.similarImageList
                vb.idTvNoData.isVisible = fileList.size == 0
                vb.idTvDelete.isVisible = fileList.size > 0
                vb.idTvAll.isVisible = fileList.size > 0
                vb.idImgSelect.isVisible = fileList.size > 0
                vb.idRlList.layoutManager = GridLayoutManager(this, 3)
                vb.idRlList.adapter = mAdapter
            }
        }else{
            finishToMain()
        }
    }
    private fun setTvStatus() {
        val it = fileList.filter { it.isSelect }
        vb.idTvDelete.isEnabled = it.isNotEmpty()
    }
    private val mAdapter by lazy {
        class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val ivImage: ImageView
            val ivSelect: ImageView

            init {
                ivImage = view.findViewById(R.id.iv_image)
                ivSelect = view.findViewById(R.id.id_img_sel)
            }
        }

        object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
                val view = LayoutInflater.from(this@RepeaterdPhotoActivity).inflate(
                    R.layout.item_layout_repeater_photo, parent, false
                )
                return ViewHolder(view)
            }

            override fun getItemCount() = fileList.size

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = fileList[position]
                Glide.with(FileApp.fContext).load(item.path).into(holder.ivImage)
                holder.ivSelect.isVisible = true
                holder.itemView.setOnClickListener {

                    item.isSelect = !item.isSelect
                    notifyItemChanged(position, 1)

                    if (fileList.all { it.isSelect }) {
                        isSelectAll = true
                        vb.idTvAll.text = "Cancel"
                        vb.idImgSelect.setImageResource(R.drawable.xuanzhong2232)
                    } else {
                        isSelectAll = false
                        vb.idTvAll.text = "All"
                        vb.idImgSelect.setImageResource(R.drawable.cfzps2)
                    }
                    setTvStatus()
                }
            }

            override fun onBindViewHolder(
                holder: ViewHolder,
                position: Int,
                payloads: MutableList<Any>
            ) {
                super.onBindViewHolder(holder, position, payloads)
                val item = fileList[position]
                holder.ivSelect.setImageResource(
                    if (item.isSelect) {
                        R.drawable.xuanzhong2232
                    } else {
                        R.drawable.cfzps2
                    }
                )
            }
        }
    }
}