package com.last.commit.map

import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.last.commit.GameState

class Collectible(
    name: String,
    val pos: Position,
    val size: Vector2,
    val requiredItem: String,
    val image: Image
) : Interactable {

    val name: String
    private val collider: Rectangle

    init {
        this.name = name
        this.collider = Rectangle(pos.x, pos.y, size.x, size.y)
    }

    override fun interact(otherCollider: Rectangle, state: GameState): Boolean {
        println("Interacting with item $name")
        state.soundEngine.play("GRAB")
        if (state.inventory.hasItem(this.name)) {
            state.dialogStage.setTexts("You already have this item.")
            state.dialogStage.show()
        } else if (state.inventory.isFull()) {
            state.dialogStage.setTexts("You can't carry anymore items.")
        } else {
            state.inventory.add(this.name)
            return true
        }
        return false
    }

    override fun canInteract(state: GameState): Boolean {
        if (requiredItem == "") {
            return true
        }
        state.inventory.items.find { it.name == requiredItem } ?: return false

        return true

    }

    override fun getCollider(): Rectangle {
        return this.collider
    }
}
