package com.pixelized.desktop.lwa.ui.screen.admin.map.edit

import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextField
import com.pixelized.desktop.lwa.ui.composable.textfield.createLwaTextFieldFlow
import com.pixelized.desktop.lwa.utils.extention.unpack
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_camera_offset_x
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_camera_offset_y
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_camera_zoom
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_id
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_name
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_size_height
import lwacharactersheet.composeapp.generated.resources.game_master__map__edit_size_width
import lwacharactersheet.composeapp.generated.resources.game_master__map__layer_id
import lwacharactersheet.composeapp.generated.resources.game_master__map__layer_name
import lwacharactersheet.composeapp.generated.resources.game_master__map__layer_uri
import org.jetbrains.compose.resources.getString
import com.pixelized.shared.lwa.model.map.Map as MiniMap

class GMMapEditFactory {

    suspend fun createForm(
        item: MiniMap?,
    ): GMMapEditViewModel.GMMapEditPageForm {
        val id = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_id),
            value = item?.id ?: ""
        )
        val label = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_name),
            value = item?.name ?: ""
        )
        val cameraZoom = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_camera_zoom),
            value = "${item?.camera?.zoom ?: 0}",
        )
        val cameraOffsetX = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_camera_offset_x),
            value = "${item?.camera?.offsetX ?: 0}",
        )
        val cameraOffsetY = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_camera_offset_y),
            value = "${item?.camera?.offsetY ?: 0}",
        )
        val sizeWidth = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_size_width),
            value = "${item?.size?.width ?: 0}",
        )
        val sizeHeight = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__edit_size_height),
            value = "${item?.size?.height ?: 0}",
        )
        val rootId = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_id),
            value = "${item?.resources?.getOrNull(0)?.id ?: 0}",
        )
        val rootName = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_name),
            value = "${item?.resources?.getOrNull(0)?.name ?: 0}",
        )
        val rootUri = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_uri),
            value = "${item?.resources?.getOrNull(0)?.uri ?: 0}",
        )
        val layerId = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_id),
            value = "${item?.resources?.getOrNull(1)?.id ?: 0}",
        )
        val layerName = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_name),
            value = "${item?.resources?.getOrNull(1)?.name ?: 0}",
        )
        val layerUri = createLwaTextFieldFlow(
            label = getString(Res.string.game_master__map__layer_uri),
            value = "${item?.resources?.getOrNull(1)?.uri ?: 0}",
        )
        return GMMapEditViewModel.GMMapEditPageForm(
            id = id,
            label = label,
            cameraZoom = cameraZoom,
            cameraOffsetX = cameraOffsetX,
            cameraOffsetY = cameraOffsetY,
            sizeWidth = sizeWidth,
            sizeHeight = sizeHeight,
            rootId = rootId,
            rootName = rootName,
            rootUri = rootUri,
            layerId = layerId,
            layerName = layerName,
            layerUri = layerUri,
        )
    }

    fun createPage(
        id: String?,
        form: GMMapEditViewModel.GMMapEditPageForm,
    ): GMMapEditPageUio {
        return GMMapEditPageUio(
            id = form.id.createLwaTextField(enable = id == null),
            label = form.label.createLwaTextField(),
            cameraZoom = form.cameraZoom.createLwaTextField(),
            cameraOffsetX = form.cameraOffsetX.createLwaTextField(),
            cameraOffsetY = form.cameraOffsetY.createLwaTextField(),
            sizeWidth = form.sizeWidth.createLwaTextField(),
            sizeHeight = form.sizeHeight.createLwaTextField(),
            rootId = form.rootId.createLwaTextField(),
            rootName = form.rootName.createLwaTextField(),
            rootUri = form.rootUri.createLwaTextField(),
            layerId = form.layerId.createLwaTextField(),
            layerName = form.layerName.createLwaTextField(),
            layerUri = form.layerUri.createLwaTextField(),
        )
    }

    fun createItem(
        form: GMMapEditPageUio,
    ): MiniMap? {
        return try {
            MiniMap(
                id = form.id.unpack() ?: "",
                name = form.label.unpack() ?: "",
                camera = MiniMap.Camera(
                    zoom = form.cameraZoom.unpack() ?: 1f,
                    offsetX = form.cameraOffsetX.unpack() ?: 0,
                    offsetY = form.cameraOffsetY.unpack() ?: 0,
                ),
                size = MiniMap.Size(
                    width = form.sizeWidth.unpack() ?: 1,
                    height = form.sizeHeight.unpack() ?: 1,
                ),
                resources = listOf(
                    MiniMap.Resource(
                        id = form.rootId.unpack() ?: "",
                        name = form.rootName.unpack() ?: "",
                        uri = form.rootUri.unpack() ?: "",
                    ),
                    MiniMap.Resource(
                        id = form.layerId.unpack() ?: "",
                        name = form.layerName.unpack() ?: "",
                        uri = form.layerUri.unpack() ?: "",
                    ),
                ),
            )
        } catch (_: Exception) {
            null
        }
    }
}