package com.img.rabbit.utils

import android.graphics.*
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.segmentation.Segmentation
import com.google.mlkit.vision.segmentation.selfie.SelfieSegmenterOptions
import androidx.core.graphics.createBitmap

import android.graphics.*
import androidx.compose.ui.geometry.Offset
import com.google.mlkit.vision.face.FaceContour
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
import androidx.core.graphics.get

// 变换状态模型
data class TransformState(
    val offset: Offset = Offset.Zero,
    val scale: Float = 1f,
    val rotation: Float = 0f
)



object PhotoCutter {

    fun cutPureHead(sourceBitmap: Bitmap, callback: (Bitmap?) -> Unit) {
        // 1. 配置分割器：单图模式
        val options = SelfieSegmenterOptions.Builder()
            .setDetectorMode(SelfieSegmenterOptions.SINGLE_IMAGE_MODE)
            .build()
        val segmenter = Segmentation.getClient(options)
        val image = InputImage.fromBitmap(sourceBitmap, 0)

        // 2. 开始处理
        segmenter.process(image)
            .addOnSuccessListener { mask ->
                val resultBitmap = generateTransparentBitmap(sourceBitmap, mask.buffer)
                callback(resultBitmap)
            }
            .addOnFailureListener {
                callback(null)
            }
    }

    private fun generateTransparentBitmap(
        source: Bitmap,
        maskBuffer: java.nio.ByteBuffer,
    ): Bitmap {
        val width = source.width
        val height = source.height
        val result = createBitmap(width, height)

        // 将掩码缓冲区重置
        maskBuffer.rewind()

        // 逐像素处理
        val pixels = IntArray(width * height)
        source.getPixels(pixels, 0, width, 0, 0, width, height)

        for (i in 0 until width * height) {
            // 获取当前像素的人像概率（0.0 ~ 1.0）
            val confidence = maskBuffer.float

            // 如果置信度低于 0.5，认为该像素是背景，设为完全透明
            if (confidence < 0.5) {
                pixels[i] = Color.TRANSPARENT
            }
        }

        result.setPixels(pixels, 0, width, 0, 0, width, height)
        return result
    }
}



//object PhotoCutter {
//
//    fun cutHeadFromImage(sourceBitmap: Bitmap, callback: (Bitmap?) -> Unit) {
//        // 1. 配置分割器：单图模式
//        val options = SelfieSegmenterOptions.Builder()
//            .setDetectorMode(SelfieSegmenterOptions.SINGLE_IMAGE_MODE)
//            .build()
//        val segmenter = Segmentation.getClient(options)
//        val image = InputImage.fromBitmap(sourceBitmap, 0)
//
//        // 2. 开始处理
//        segmenter.process(image)
//            .addOnSuccessListener { mask ->
//                val resultBitmap = generateTransparentBitmap(sourceBitmap, mask.buffer, mask.width, mask.height)
//                callback(resultBitmap)
//            }
//            .addOnFailureListener {
//                callback(null)
//            }
//    }
//
//    private fun generateTransparentBitmap(
//        source: Bitmap,
//        maskBuffer: java.nio.ByteBuffer,
//        maskWidth: Int,
//        maskHeight: Int
//    ): Bitmap {
//        val width = source.width
//        val height = source.height
//        val result = createBitmap(width, height)
//
//        // 将掩码缓冲区重置
//        maskBuffer.rewind()
//
//        // 逐像素处理
//        val pixels = IntArray(width * height)
//        source.getPixels(pixels, 0, width, 0, 0, width, height)
//
//        for (i in 0 until width * height) {
//            // 获取当前像素的人像概率（0.0 ~ 1.0）
//            val confidence = maskBuffer.float
//
//            // 如果置信度低于 0.5，认为该像素是背景，设为完全透明
//            if (confidence < 0.5) {
//                pixels[i] = Color.TRANSPARENT
//            }
//        }
//
//        result.setPixels(pixels, 0, width, 0, 0, width, height)
//        return result
//    }
//}
