package com.nexlab.openclaw.node.service

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.content.Intent
import android.graphics.Path
import android.graphics.PixelFormat
import android.os.Build
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent

/**
 * AccessibilityService for performing touch gestures
 */
class TouchService : AccessibilityService() {

    companion object {
        var instance: TouchService? = null
            private set
        
        var isServiceRunning = false
            private set
    }

    override fun onServiceConnected() {
        super.onServiceConnected()
        instance = this
        isServiceRunning = true
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        // Could forward accessibility events to gateway here
    }

    override fun onInterrupt() {
        // Handle interruption
    }

    override fun onDestroy() {
        super.onDestroy()
        instance = null
        isServiceRunning = false
    }

    override fun onUnbind(intent: Intent?): Boolean {
        instance = null
        isServiceRunning = false
        return super.onUnbind(intent)
    }

    /**
     * Perform a tap gesture at coordinates
     */
    fun tap(x: Int, y: Int, callback: ((Boolean) -> Unit)? = null): Boolean {
        if (!isServiceRunning) {
            callback?.invoke(false)
            return false
        }

        val path = Path().apply {
            moveTo(x.toFloat(), y.toFloat())
        }

        val gesture = GestureDescription.Builder()
            .addStroke(GestureDescription.StrokeDescription(path, 0, 100))
            .build()

        return dispatchGesture(gesture, object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                callback?.invoke(true)
            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                callback?.invoke(false)
            }
        }, null)
    }

    /**
     * Perform a swipe gesture from (x1,y1) to (x2,y2)
     */
    fun swipe(x1: Int, y1: Int, x2: Int, y2: Int, duration: Int = 300, callback: ((Boolean) -> Unit)? = null): Boolean {
        if (!isServiceRunning) {
            callback?.invoke(false)
            return false
        }

        val path = Path().apply {
            moveTo(x1.toFloat(), y1.toFloat())
            lineTo(x2.toFloat(), y2.toFloat())
        }

        val gesture = GestureDescription.Builder()
            .addStroke(GestureDescription.StrokeDescription(path, 0, duration.toLong()))
            .build()

        return dispatchGesture(gesture, object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                callback?.invoke(true)
            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                callback?.invoke(false)
            }
        }, null)
    }

    /**
     * Perform a long press gesture
     */
    fun longPress(x: Int, y: Int, duration: Int = 500, callback: ((Boolean) -> Unit)? = null): Boolean {
        if (!isServiceRunning) {
            callback?.invoke(false)
            return false
        }

        val path = Path().apply {
            moveTo(x.toFloat(), y.toFloat())
        }

        val gesture = GestureDescription.Builder()
            .addStroke(GestureDescription.StrokeDescription(path, 0, duration.toLong()))
            .build()

        return dispatchGesture(gesture, object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                callback?.invoke(true)
            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                callback?.invoke(false)
            }
        }, null)
    }

    /**
     * Perform a pinch zoom gesture (stretch goal)
     */
    fun pinchZoom(centerX: Int, centerY: Int, scale: Float, duration: Int = 500): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            return false
        }

        // Simplified pinch - just a two-finger gesture
        val offset = 100
        val path1 = Path().apply {
            moveTo((centerX - offset).toFloat(), centerY.toFloat())
            lineTo((centerX - offset - (offset * scale)).toFloat(), centerY.toFloat())
        }
        
        val path2 = Path().apply {
            moveTo((centerX + offset).toFloat(), centerY.toFloat())
            lineTo((centerX + offset + (offset * scale)).toFloat(), centerY.toFloat())
        }

        val builder = GestureDescription.Builder()
        builder.addStroke(GestureDescription.StrokeDescription(path1, 0, duration.toLong()))
        builder.addStroke(GestureDescription.StrokeDescription(path2, 0, duration.toLong()))

        return dispatchGesture(builder.build(), null, null)
    }

    /**
     * Perform a double tap gesture
     */
    fun doubleTap(x: Int, y: Int, callback: ((Boolean) -> Unit)? = null): Boolean {
        // Double tap is two quick taps
        val path = Path().apply {
            moveTo(x.toFloat(), y.toFloat())
        }

        val gesture = GestureDescription.Builder()
            .addStroke(GestureDescription.StrokeDescription(path, 0, 50))
            .addStroke(GestureDescription.StrokeDescription(path, 100, 50))
            .build()

        return dispatchGesture(gesture, object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                callback?.invoke(true)
            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                callback?.invoke(false)
            }
        }, null)
    }
}
