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 android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.zxhyis.toolss.R
import com.zxhyis.toolss.base.TopActivity
import com.zxhyis.toolss.cx.FileBean
import com.zxhyis.toolss.cx.FileHelps
import com.zxhyis.toolss.databinding.ActivityLayoutSimilarPhotosBinding
import com.zxhyis.toolss.logic.setOnClick

class SimilarPhotosActivity : TopActivity<ActivityLayoutSimilarPhotosBinding>() {
    override fun getViewBinding1(): ActivityLayoutSimilarPhotosBinding {
        return ActivityLayoutSimilarPhotosBinding.inflate(layoutInflater)
    }

    override fun onStart() {
        super.onStart()
        checkPermission()
    }

    override fun onPermissionsResult(isGranted: Boolean) {
        if (isGranted) {
            initList()
        } else {
            finishToMain()
        }
    }

    override fun init() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        vb.root.updatePadding(top = BarUtils.getStatusBarHeight())
        initListener()

    }

    private fun initListener() {
        vb.idBackSimilarPhoto.setOnClick(javaClass.simpleName) {
            onBackPressedDispatcher.onBackPressed()
        }
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                finishToMain()
            }
        })
        vb.idTopDelete.setOnClick(javaClass.simpleName) {

        }
    }

    val selectedList = mutableListOf<String>()
    private fun initList() {
        FileHelps.getTypeFiles {
            runOnUiThread {
                val dateList = FileHelps.typeDateList.toList()
                vb.idFlLottie.isVisible = false
                vb.idRlSimilar.run {
                    layoutManager = LinearLayoutManager(context)
                    adapter = object : RecyclerView.Adapter<DateViewHolder>() {
                        override fun onCreateViewHolder(
                            parent: ViewGroup, viewType: Int
                        ): DateViewHolder {
                            val view = LayoutInflater.from(context)
                                .inflate(R.layout.item_layout_similar, parent, false)
                            return DateViewHolder(view)
                        }

                        override fun onBindViewHolder(holder: DateViewHolder, position: Int) {
                            val pair = dateList[position]
                            holder.idDate.text = pair.first
                            holder.idRecyclerView.run {
                                layoutManager = GridLayoutManager(context, 3)
                                adapter = object : RecyclerView.Adapter<ItemViewHolder>() {
                                    override fun onCreateViewHolder(
                                        parent: ViewGroup, viewType: Int
                                    ): ItemViewHolder {
                                        val view = LayoutInflater.from(context)
                                            .inflate(R.layout.item_layout_photo_two, parent, false)
                                        return ItemViewHolder(view)
                                    }

                                    override fun onBindViewHolder(
                                        holder: ItemViewHolder,
                                        position: Int
                                    ) {
                                        val item = pair.second[position]

                                        Glide.with(context).load(item.path).apply(
                                            RequestOptions().transform(
                                                CenterCrop(), RoundedCorners(SizeUtils.dp2px(7f))
                                            )
                                        ).into(holder.idImage)
                                        holder.idSelect.setOnClickListener {
                                            item.isSelect = !item.isSelect
                                            notifyItemChanged(position, 1)
                                            onSelect(item)
                                        }
                                        holder.itemView.setOnClickListener {

                                        }
                                    }

                                    override fun onBindViewHolder(
                                        holder: ItemViewHolder,
                                        position: Int,
                                        payloads: MutableList<Any>
                                    ) {
                                        super.onBindViewHolder(holder, position, payloads)
                                        holder.itemView.isSelected = pair.second[position].isSelect
                                    }

                                    override fun getItemCount() = pair.second.size
                                }
                            }
                        }

                        override fun getItemCount() = dateList.size

                        private inner class ItemViewHolder(view: View) :
                            RecyclerView.ViewHolder(view) {
                            val idImage: ImageView
                            val idSelect: View

                            init {
                                idImage = view.findViewById(R.id.id_image)
                                idSelect = view.findViewById(R.id.id_select)
                            }
                        }
                    }
                }
            }

        }
    }


    private fun onSelect(bean: FileBean) {
        if (bean.isSelect) {
            selectedList.add(bean.path)
        } else {
            selectedList.remove(bean.path)
        }

    }

    private inner class DateViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val idDate: TextView
        val idRemoveSelected: TextView
        val idRecyclerView: RecyclerView

        init {
            idDate = view.findViewById(R.id.id_tv_date)
            idRemoveSelected = view.findViewById(R.id.id_tv_remove_selected)
            idRecyclerView = view.findViewById(R.id.id_item_rl_photo)
        }
    }
}