package com.zxhyis.toolss.ui.adapter

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.core.view.isVisible
import com.zxhyis.toolss.R
import com.zxhyis.toolss.ad.ConfigHelper
import com.zxhyis.toolss.cx.FileHelps.toFormatSize
import com.zxhyis.toolss.databinding.ItemChildBinding
import com.zxhyis.toolss.databinding.ItemParentBinding
import com.zxhyis.toolss.logic.Logic
import com.zxhyis.toolss.logic.ParentBean
import com.zxhyis.toolss.logic.setOnClick
import pokercc.android.expandablerecyclerview.ExpandableAdapter


class JunkAdapter(
    private val parentList: List<ParentBean>,
    private val context: Context,
    private val logic: Logic
) : ExpandableAdapter<ExpandableAdapter.ViewHolder>() {

    internal class ParentViewHolder(val binding: ItemParentBinding) :
        ViewHolder(binding.root)

    internal class ChildViewHolder(val binding: ItemChildBinding) :
        ViewHolder(binding.root)

    @SuppressLint("NotifyDataSetChanged")
    fun UpdateData() {
        notifyDataSetChanged()
    }

    override fun getChildCount(groupPosition: Int): Int {
        return parentList[groupPosition].childItem.size
    }

    override fun getGroupCount(): Int {
        return parentList.size
    }

    override fun onCreateChildViewHolder(
        viewGroup: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val binding = ItemChildBinding.inflate(inflater, viewGroup, false)
        return ChildViewHolder(binding)
    }

    override fun onCreateGroupViewHolder(
        viewGroup: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        val binding = ItemParentBinding.inflate(inflater, viewGroup, false)
        return ParentViewHolder(binding)
    }

    override fun onGroupViewHolderExpandChange(
        holder: ViewHolder,
        groupPosition: Int,
        animDuration: Long,
        expand: Boolean
    ) {

    }

    @SuppressLint("NotifyDataSetChanged", "SuspiciousIndentation")
    override fun onBindGroupViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        expand: Boolean,
        payloads: List<Any>
    ) {

        val viewHolder = holder as? ParentViewHolder ?: return
        val parentType = parentList[groupPosition]
        viewHolder.binding.idTypeName.text = parentType.title
        parentType.expanded = expand
        if (parentType.expanded) {
            viewHolder.binding.idXiala.setImageResource(R.drawable.xiala_n)
        } else {
            viewHolder.binding.idXiala.setImageResource(R.drawable.xiala_s)
        }
        if (parentType.isfinish) {
            viewHolder.binding.idImgLoad.isVisible = false
            viewHolder.binding.idImgChoose.isVisible = true
        } else {
            ValueAnimator.ofFloat(0f, 360f).run {
                duration = 1000
                repeatMode = ValueAnimator.RESTART
                repeatCount = ValueAnimator.INFINITE
                interpolator = LinearInterpolator()
                addUpdateListener {
                    viewHolder.binding.idImgLoad.rotation = it.animatedValue as Float
                }
                start()
            }
            viewHolder.binding.idImgLoad.isVisible = true
            viewHolder.binding.idImgChoose.isVisible = false
        }
        viewHolder.binding.idTypeSize.text = parentType.parentSize.toFormatSize()
        viewHolder.binding.idXiala.isVisible = parentType.childItem.isNotEmpty()
        viewHolder.binding.idImgChoose.isSelected = parentType.isParentSelected

        viewHolder.binding.idImgChoose.setOnClick(javaClass.simpleName) {
            viewHolder.binding.idImgChoose.isSelected =
                !viewHolder.binding.idImgChoose.isSelected
            parentType.isParentSelected = !parentType.isParentSelected
            if (parentType.isParentSelected) {
                for (app in parentType.childItem) {
                    app.isChildSelected = true
                    notifyDataSetChanged()
                }
            } else {
                for (app in parentType.childItem) {
                    app.isChildSelected = false
                    notifyDataSetChanged()
                }
            }
            logic.doLogic()
        }
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindChildViewHolder(
        holder: ViewHolder,
        groupPosition: Int,
        childPosition: Int,
        payloads: List<Any>
    ) {
        val viewHolder = holder as? ChildViewHolder ?: return
        val childType = parentList[groupPosition].childItem[childPosition]
        when (groupPosition) {
            0 -> {
                val c = ConfigHelper.appList?.firstOrNull {
                    it.name == childType.childname
                }
                viewHolder.binding.idImgIcon.setImageDrawable(c?.icon)
            }

            1 -> {
                viewHolder.binding.idImgIcon.setImageResource(R.drawable.adicon)
            }

            2 -> {
                val packageManager: PackageManager = context.packageManager
                val apkFilePath = childType.chilepath // 替换成您的APK文件路径
                val packageInfo =
                    packageManager.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES)
                if (packageInfo != null) {
                    val applicationInfo = packageInfo.applicationInfo
                    val appIcon = packageManager.getApplicationIcon(applicationInfo)
                    viewHolder.binding.idImgIcon.setImageDrawable(appIcon)
                }
            }
        }

        viewHolder.binding.idTvAppName.text = childType.childname
        viewHolder.binding.idImgSelect.isSelected = childType.isChildSelected
        viewHolder.binding.idTvSize.text = "${childType.childSize.toFormatSize()}"
        viewHolder.itemView.setOnClickListener {
            when (childPosition) {
                childPosition -> {
                    viewHolder.binding.idImgSelect.isSelected =
                        !viewHolder.binding.idImgSelect.isSelected
                    childType.isChildSelected = !childType.isChildSelected
                    if (!childType.isChildSelected) {
                        parentList[groupPosition].isParentSelected = false
                        notifyDataSetChanged()
                    }
                    logic.doLogic()
                }
            }
        }

    }


}