package info.raa0121.exebattle

import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.TextureRegion


class ExeBattle : ApplicationAdapter() {
    private var batch: SpriteBatch? = null
    private var fontGenerator: FreeTypeFontGenerator? = null
    private var debugBitmapFont: BitmapFont? = null
    private var playerHPBitmapFont: BitmapFont? = null
    private var tekiHPBitmapFont: BitmapFont? = null
    private var tile: Texture? = null
    private var playerImage: Texture? = null
    private var playerImageStand: TextureRegion? = null
    private var playerImageShoot: TextureRegion? = null
    private var tekiImage: Texture? = null
    private var tekiImageStand: TextureRegion? = null
    private var tekiImageShoot: TextureRegion? = null
    private var playerX = 116f
    private var playerY = 130f
    private var tekiX = 431f
    private var tekiY = 130f
    private var playerHP = 1000
    private var tekiHP = 100

    override fun create() {
        batch = SpriteBatch()
        val font: FileHandle  = Gdx.files.internal("fonts/Cica-Regular.ttf")
        tile = Texture(Gdx.files.internal("tile.png"))
        playerImage = Texture(Gdx.files.internal("player.png"))
        playerImageStand = TextureRegion(playerImage, 0, 0, 88,124)
        playerImageShoot = TextureRegion(playerImage, 88, 0, 88, 124)
        tekiImage = Texture(Gdx.files.internal("teki.png"))
        tekiImageShoot = TextureRegion(tekiImage, 0, 0, 88,124)
        tekiImageStand = TextureRegion(tekiImage, 88, 0, 88, 124)
        fontGenerator = FreeTypeFontGenerator(font)
        val debugFontParam = FreeTypeFontGenerator.FreeTypeFontParameter()
        debugFontParam.size = 18;
        debugFontParam.incremental = true
        debugFontParam.color = Color.BLUE
        debugBitmapFont = fontGenerator?.generateFont(debugFontParam)
        val playerHPFontParam = FreeTypeFontGenerator.FreeTypeFontParameter()
        playerHPFontParam.size = 28
        playerHPFontParam.incremental = true
        playerHPFontParam.color = Color.BLACK
        playerHPBitmapFont = fontGenerator?.generateFont(playerHPFontParam)
        val tekiHPFontParam = FreeTypeFontGenerator.FreeTypeFontParameter()
        tekiHPFontParam.size = 18
        tekiHPFontParam.incremental = true
        tekiHPFontParam.color = Color.BLACK
        tekiHPBitmapFont = fontGenerator?.generateFont(tekiHPFontParam)
    }

    override fun resize(width: Int, height: Int) {
    }

    override fun render() {
        Gdx.gl.glClearColor(1f, 1f,1f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        batch?.begin()
        batch?.draw(tile, 0f, 0f)
        if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) {
            if (playerX > 11f) {
                playerX -= 105f
            }
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
            if (playerX < 221f) {
                playerX += 105f
            }
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
            if (playerY < 195f) {
                playerY += 65f
            }
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.DOWN)) {
            if (playerY > 65f) {
                playerY -= 65f
            }
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.Z)) {
            batch?.draw(playerImageShoot, playerX, playerY)
            if (playerY == tekiY && tekiHP > 0) {
                tekiHP -= 10
            }
        } else {
            batch?.draw(playerImageStand, playerX, playerY)
        }
        //debugBitmapFont?.draw(batch, "X:" + playerX + "  Y:" + playerY, 0f, 10f)
        playerHPBitmapFont?.draw(batch, playerHP.toString(), 0f, 480f)
        if (tekiHP > 0) {
            batch?.draw(tekiImageStand, tekiX, tekiY)
            tekiHPBitmapFont?.draw(batch, tekiHP.toString(), tekiX + 30f, tekiY + 129f)
        }
        batch?.end()
    }

    override fun dispose() {
        super.dispose()
        debugBitmapFont?.dispose()
        playerHPBitmapFont?.dispose()
        tekiHPBitmapFont?.dispose()
        batch?.dispose()
        tile?.dispose()
        playerImage?.dispose()
    }
}
