package com.omixlab.mosis

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.content.res.AssetManager
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.MotionEvent
import android.view.Surface
import android.view.SurfaceView
import android.view.SurfaceHolder
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.viewinterop.AndroidView

class MainActivity : ComponentActivity() {
    init {
        System.loadLibrary("mosis-test")
    }
    var remote_service: IMosisService? = null
    var statusText = mutableStateOf("Status: idle")
    var buttonText = mutableStateOf("Init OS")
    val connection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName, service: IBinder) {
            Log.d("MosisTest", "Service Connected")
            statusText.value = "Service Connected"
            remote_service = IMosisService.Stub.asInterface(service)
            serviceConnected(service)
            initOS()
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
            Log.d("MosisTest", "Service Disconnected")
            statusText.value = "Service Disconnected"
        }
    }
    val viewport = object : SurfaceHolder.Callback {
        override fun surfaceCreated(holder: SurfaceHolder) {
            Log.d("MosisTest", "surfaceCreated")
            setSurface(holder.surface)
        }
        override fun surfaceChanged(
            holder: SurfaceHolder,
            format: Int,
            width: Int,
            height: Int
        ) {
            // Optional: Pass resize events to native if needed
            // resizeSurface(width, height)
            Log.d("MosisTest", "surfaceResized")

        }
        override fun surfaceDestroyed(holder: SurfaceHolder) {
            destroySurface()
            Log.d("MosisTest", "surfaceDestroyed")
        }
    }
    fun startRemoteService() {
        val intent = Intent("com.omixlab.mosis.SERVICE")
        intent.setPackage("com.omixlab.mosis")
        try {
            startForegroundService(intent)
            val result = bindService(intent, connection, BIND_AUTO_CREATE)
            Log.d("ServiceTester", "Bind result: $result")
        } catch (e: Exception) {
            Log.e("ServiceTester", "Bind failed", e)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setAssetManager(assets)
        startRemoteService()
        enableEdgeToEdge()
        setContent {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Text("Welcome to Mosis OS")
                Button(onClick = { initOS() }) {
                    Text(buttonText.value)
                }
                Text(statusText.value)
                NativeViewport(
                    modifier = Modifier
                        .fillMaxSize(0.8f)
                )
            }
        }
    }
    @SuppressLint("ClickableViewAccessibility")
    @Composable
    fun NativeViewport(modifier: Modifier = Modifier) {
        AndroidView(
            modifier = modifier,
            factory = { context ->
                SurfaceView(context).apply {
                    holder.addCallback(viewport)
                    setOnTouchListener { v, event ->
                        when (event.action) {
                            MotionEvent.ACTION_DOWN -> {
                                Log.d("ServiceTester", "ACTION_DOWN ${event.x} - ${event.y}")
                                remote_service?.onTouchDown(event.x /v.width, event.y / v.height)
                                true
                            }
                            MotionEvent.ACTION_MOVE -> {
                                Log.d("ServiceTester", "ACTION_MOVE ${event.x} - ${event.y}")
                                remote_service?.onTouchMove(event.x /v.width, event.y / v.height)
                                true
                            }
                            MotionEvent.ACTION_UP -> {
                                Log.d("ServiceTester", "ACTION_ ${event.x} - ${event.y}")
                                remote_service?.onTouchUp(event.x /v.width, event.y / v.height)
                                true
                            }
                            else -> false
                        }
                    }
                }
            }
        )
    }
    external fun setAssetManager(assetManager: AssetManager)
    external fun serviceConnected(binder: IBinder)
    external fun setSurface(surface: Surface)
    external fun destroySurface()
    external fun initOS()
}