package com.pixelized.desktop.lwa.ui.composable.scene.utils

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.IntOffset
import com.pixelized.desktop.lwa.ui.composable.scene.camera.Camera

/**
 * Convert local positon to global one.
 * Global position are agnostic from camera, and therefor should be use to position stuff on the map.
 * A common use case is to share players cursor.
 */
fun Offset.global(
    camera: Camera,
): Offset = Offset(
    x = this.x * camera.zoom + camera.offset.x,
    y = this.y * camera.zoom + camera.offset.y,
)

/**
 * Convert global positon to local one.
 * Local position take into account the camera and are use to display stuff on the Scene composable.
 */
fun Offset.local(
    camera: Camera,
): Offset = Offset(
    x = (this.x - camera.offset.x) / camera.zoom,
    y = (this.y - camera.offset.y) / camera.zoom,
)

/**
 * Convert global positon to local one.
 * Local position take into account the camera and are use to display stuff on the Scene composable.
 */
fun IntOffset.local(
    camera: Camera,
): Offset = Offset(
    x = (this.x.toFloat() - camera.offset.x) / camera.zoom,
    y = (this.y.toFloat() - camera.offset.y) / camera.zoom,
)