package com.ux.video.file.filerecovery.utils

import android.content.Context
import android.content.Intent
import android.icu.util.Calendar
import android.os.Build
import android.os.Parcelable
import android.util.TypedValue
import androidx.recyclerview.widget.RecyclerView
import com.ux.video.file.filerecovery.photo.ResultPhotosFiles

object ExtendFunctions {


    fun Int.dpToPx(context: Context): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            this.toFloat(),
            context.resources.displayMetrics
        ).toInt()
    }

    inline fun <reified T : Parcelable> Intent.getParcelableArrayListExtraCompat(key: String): ArrayList<T>? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            getParcelableArrayListExtra(key, T::class.java)
        } else {
            @Suppress("DEPRECATION")
            getParcelableArrayListExtra<T>(key)
        }
    }

    fun RecyclerView.resetItemDecorationOnce(decoration: RecyclerView.ItemDecoration) {
        while (itemDecorationCount > 0) {
            removeItemDecorationAt(0)
        }
        addItemDecoration(decoration)
    }


    /**
     * 按时间筛选：最近 N 个月
     */
    fun List<ResultPhotosFiles>.filterWithinMonthsList(months: Int): List<ResultPhotosFiles> {
        if (months == -1) return this
        val today = Calendar.getInstance()
        val monthsAgo = Calendar.getInstance().apply {
            add(Calendar.MONTH, -months)
        }
        return this.filter {
            val cal = Calendar.getInstance().apply { timeInMillis = it.lastModified }
            !cal.before(monthsAgo) && !cal.after(today)
        }
    }

    /**
     * 按文件大小筛选：区间 [minSize, maxSize]
     */
    fun List<ResultPhotosFiles>.filterBySizeList(
        minSize: Long,
        maxSize: Long
    ): List<ResultPhotosFiles> {
        if (minSize == -1L) return this
        return this.filter { it.size in minSize..maxSize }
    }

    /**
     * 按时间筛选：最近 N 个月
     */
    fun List<Pair<String, List<ResultPhotosFiles>>>.filterWithinMonths(months: Int): List<Pair<String, List<ResultPhotosFiles>>> {
        if (months == -1) return this
        val sdf = Common.dateFormat
        val today = Calendar.getInstance()
        val monthsAgo = Calendar.getInstance().apply {
            add(Calendar.MONTH, -months)
        }
        return this.filter { (dayStr, _) ->
            val day = sdf.parse(dayStr)
            day != null && !day.before(monthsAgo.time) && !day.after(today.time)
        }
    }

    /**
     * 分组数据：按大小筛选
     */
    fun List<Pair<String, List<ResultPhotosFiles>>>.filterBySize(
        minSize: Long,
        maxSize: Long
    ): List<Pair<String, List<ResultPhotosFiles>>> {
        if (minSize == -1L) return this
        return this.mapNotNull { (date, files) ->
            val filtered = files.filter { it.size in minSize..maxSize }
            if (filtered.isNotEmpty()) date to filtered else null
        }
    }
    fun Int.mbToBytes(): Long {
        return this * 1024L * 1024L
    }


}