package com.assimilate.alltrans.common

import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.hardware.display.DisplayManager
import android.hardware.display.VirtualDisplay
import android.media.ImageReader
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
import android.os.Handler
import android.os.Looper
import android.util.DisplayMetrics
import android.util.Log
import android.view.WindowManager

class ScreenCaptureManager(private val context: Context) {
    private lateinit var mediaProjectionManager: MediaProjectionManager
    private var mediaProjection: MediaProjection? = null
    private lateinit var imageReader: ImageReader
    private var virtualDisplay: VirtualDisplay? = null
    private var displayMetrics: DisplayMetrics = DisplayMetrics()
    private var mResultCode = 0
    private var mResultData: Intent? = null

    init {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowManager.defaultDisplay.getMetrics(displayMetrics)
    }

    fun setProjectionParams(resultCode: Int, resultData: Intent) {
        mResultCode = resultCode
        mResultData = resultData
    }

    fun startProjection() {
        if (mResultData == null) {
            Log.e("ScreenCaptureManager", "mResultData is null, cannot start projection")
            return
        }

        if (mediaProjection == null) {
            mediaProjectionManager =
                context.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            mediaProjection =
                mediaProjectionManager.getMediaProjection(mResultCode, mResultData!!)
            mediaProjection?.registerCallback(object : MediaProjection.Callback() {}, null)

            Handler(Looper.getMainLooper()).postDelayed({
                try {
                    val density = displayMetrics.densityDpi
                    val width = displayMetrics.widthPixels
                    val height = displayMetrics.heightPixels

                    imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)

                    virtualDisplay = mediaProjection?.createVirtualDisplay(
                        "ScreenCapture",
                        width,
                        height,
                        density,
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                        imageReader.surface,
                        null,
                        null
                    )
                } catch (e: Exception) {
                    Log.e("ScreenCaptureManager", "Error starting projection", e)
                }
            }, 666)
        }
    }

    fun stopProjection() {
        try {
            virtualDisplay?.release()
            virtualDisplay = null
        } catch (e: Exception) {
            Log.e("ScreenCaptureManager", "Error releasing virtual display", e)
        }

        try {
            mediaProjection?.stop()
            mediaProjection = null
        } catch (e: Exception) {
            Log.e("ScreenCaptureManager", "Error stopping media projection", e)
        }
    }

    fun getImageReader(): ImageReader {
        return imageReader
    }
}
