package com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.links

import androidx.lifecycle.ViewModel
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_canva_24dp
import lwacharactersheet.composeapp.generated.resources.ic_link_24dp
import lwacharactersheet.composeapp.generated.resources.ic_tiddlywiki_24dp
import lwacharactersheet.composeapp.generated.resources.resources__map_title
import lwacharactersheet.composeapp.generated.resources.resources__rules__title
import lwacharactersheet.composeapp.generated.resources.resources__virtual_table__title
import java.awt.Desktop
import java.net.URI


class ResourcesViewModel : ViewModel() {

    val dialog = ResourcesDialogUio(
        resources = createResources()
    )

    private fun createResources(): List<ResourcesDialogUio.LinkResource> {
        return try {
            if (Desktop.isDesktopSupported()) {
                val desktop = Desktop.getDesktop()
                if (desktop.isSupported(Desktop.Action.BROWSE)) {
                    listOf(
                        ResourcesDialogUio.LinkResource(
                            icon = Res.drawable.ic_canva_24dp,
                            name = Res.string.resources__virtual_table__title,
                            action = { desktop.browse(URI.create(CANVA)) },
                        ),
                        ResourcesDialogUio.LinkResource(
                            icon = Res.drawable.ic_tiddlywiki_24dp,
                            name = Res.string.resources__rules__title,
                            action = { desktop.browse(URI.create(TIDDLY)) },
                        ),
                        ResourcesDialogUio.LinkResource(
                            icon = Res.drawable.ic_link_24dp,
                            tint = true,
                            name = Res.string.resources__map_title,
                            action = { desktop.browse(URI.create(MAP)) },
                        ),
                    )
                } else {
                    error("Desktop.Action.BROWSE is not supported")
                }
            } else {
                error("Desktop.isDesktopSupported return false")
            }
        } catch (exception: Exception) {
            emptyList()
        }
    }

    companion object {
        private const val CANVA =
            "https://www.canva.com/design/DAGSzwpIAAY/1YOfnIiazVWzGbKtzKdu5g/edit"
        private const val TIDDLY =
            "https://lwa-manuel-pj.tiddlyhost.com/"
        private const val MAP =
            "https://drive.google.com/file/d/1vuoyk1sfrerkMKtttf7kGPpnHpkjWvl5/view?usp=drive_link"
    }
}