package com.iofferyoutea.WitchQueen

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g3d.ModelCache
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.Container
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.badlogic.gdx.utils.Logger
import com.badlogic.gdx.utils.Scaling
import com.badlogic.gdx.utils.viewport.ScreenViewport
import com.badlogic.gdx.utils.viewport.StretchViewport
import com.badlogic.gdx.utils.viewport.Viewport
import ktx.actors.centerPosition
import ktx.actors.setPosition
import ktx.actors.stage
import ktx.app.KtxScreen
import ktx.app.clearScreen
import javax.swing.text.View

class MainMenu : KtxScreen {
    val myTable = Table().apply {
        debug = true
        setFillParent(true)
    }
    val myStage = Stage().apply {
        Gdx.input.inputProcessor = this
        addActor(myTable)
        // We need this to get the size of the table later
        //myTable.setSize(viewport.screenWidth.toFloat(), viewport.screenHeight.toFloat())
    }

    //region Map Button
    val mapButtonStyle = ImageButton.ImageButtonStyle(
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png"))
    )
    val mapButton = ImageButton(mapButtonStyle).apply {
        myTable.add(this)
        myTable.getCell<ImageButton>(this)
    }
    //endregion

    //endregion
    //region Player Buttons
    // Local Player
    // Remote Player
    val remotePlayerButton = PlayerButton().apply {
        myTable.row()
        myTable.add(this.playerGroup)
        myTable.getCell<HorizontalGroup>(this.playerGroup) // You've got to size the sub actors!
    }

    //region Play Buttons
    val casualButtonStyle = TextButton.TextButtonStyle(
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default_flipped.png")),
        TextureRegionDrawable(Texture("default.png")),
        BitmapFont()
    )
    val casualButton = TextButton("Casual", casualButtonStyle)

    val competitiveButtonStyle = TextButton.TextButtonStyle(
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        TextureRegionDrawable(Texture("default.png")),
        BitmapFont()
    )
    val competitiveButton = TextButton("Competitive", competitiveButtonStyle)

    // We need to wrap the play buttons in a group so they only take up 1 column
    val playButtonGroup = HorizontalGroup().apply {
        addActor(casualButton)
        addActor(competitiveButton)
        myTable.row()
        myTable.add(this)
        myTable.getCell<HorizontalGroup>(this)
    }
    //endregion

    private fun update(delta: Float) {
        if (casualButton.isPressed) {
            Gdx.app.log("MainMenu", "Casual Button Pressed!")
        }
    }

    override fun render(delta: Float) {
        clearScreen(0.3f, 0.3f, 0.3f)

        myStage.act(delta)
        myStage.draw()

        update(delta)
    }

    override fun dispose() {
        myStage.dispose()
    }
}

//class MainMenu : KtxScreen {
//    val viewport = ScreenViewport().apply {
//
//    }
//
//    val table = Table().apply {
//        setFillParent(true)
//        debug = true
//    }
//    val stage = Stage(viewport).apply {
//        Gdx.app.log("WitchQueen", "Width: " + viewport.screenWidth.toString() + " Height: " + viewport.screenHeight.toString())
//
//        Gdx.input.inputProcessor = this
//        addActor(table)
//    }
//
//    // Map Selection Button
//
//    // Player Table
////    val inviteButton = Button(
////        "default.png",
////        "default_flipped.png",
////        "",
////        "Invite Player",
////        true,
////        2f
////    ).apply {
////        table.add(this.button).size(1024f, 512f)
////        table.row()
////    }
//
//    // Invite Player Button
//    val playerPlaceholderButtonUp = TextureRegionDrawable(Texture("player-frame-up.png"))
//    val playerPlaceholderButtonDown = TextureRegionDrawable(Texture("player-frame-down.png"))
//    val playerPlaceholderButtonChecked = TextureRegionDrawable(Texture("player-frame-up.png"))
//    val playerPlaceholderButtonStyle = ImageButton.ImageButtonStyle()
//        .apply { up = playerPlaceholderButtonUp; down = playerPlaceholderButtonDown; checked = playerPlaceholderButtonChecked }
//    val playerPlaceholderButton = ImageButton(playerPlaceholderButtonStyle)
//    val playerPlaceholderContainer = Container<ImageButton>(playerPlaceholderButton).apply {
//        width(viewport.screenWidth * 0.8f)
//        height(((viewport.screenWidth * 0.8f) * 9f) / 32f) // container.width is 0. We need to work out the width again.
//        padLeft(0f)
//    }
//
//    val player1TableButton = PlayerTableButton().apply { // These should only be collections no t buttons?
//        container.width(viewport.screenWidth * 0.8f)
//        container.height(500f)
//        //container.height(((viewport.screenWidth * 0.8f) * 9f) / 32f) // container.width is 0. We need to work out the width again.
//        container.padLeft(0f)
//    }
//    val player2TableButton = PlayerTableButton() // Since the collection contains the buttons we need anyway?
//    val playerTableButtonCollection = VerticalGroup().apply {
//        addActor(player1TableButton.container)
//        addActor(playerPlaceholderContainer)
//        table.add(this)
//        table.row()
//    }
//
//    // Casual Button
//    val casualButton = Button(
//        "default.png",
//        "default_flipped.png",
//        "",
//        "Casual",
//        true,
//        2f
//    ).apply { // Could probably do this in Button. Just take in width and aspect ratio and padding?
//        container.width(viewport.screenWidth * 0.375f)
//        container.height(((viewport.screenWidth * 0.375f) * 3f) / 4) // container.width is 0. We need to work out the width again.
//        container.padLeft(viewport.screenWidth * 0.1f)
//    }
//
//    // Competitive Button
//    val competitiveButton = Button(
//        "default.png",
//        "default_flipped.png",
//        "",
//        "Competitive",
//        true,
//        2f
//    ).apply {
//        container.width(viewport.screenWidth * 0.375f)
//        container.height(((viewport.screenWidth * 0.375f) * 3f) / 4) // container.width is 0. We need to work out the width again.
//        container.padLeft(viewport.screenWidth * 0.05f)
//    }
//
//    val playButtonGroup = HorizontalGroup().apply {
//        addActor(casualButton.container)
//        addActor(competitiveButton.container)
//
//        table.add(this).width(viewport.screenWidth.toFloat())
//    }
//
//    // Methods
//    private fun processInput() {
//        casualButton.button.addListener(object : ClickListener() {
//            override fun touchUp(
//                event: InputEvent?,
//                x: Float,
//                y: Float,
//                pointer: Int,
//                button: Int
//            ) {
//                Gdx.app.log("My Project", "Clicked!")
//                //setScreen()
//            }
//        })
//    }
//
//    override fun resize(width: Int, height: Int) {
//        super.resize(width, height)
//        stage.viewport.update(width, height, true)
//    }
//
//    override fun render(delta: Float) {
//        super.render(delta)
//        clearScreen(0f, 255f, 0f)
//
//        processInput()
//
//        stage.act(delta)
//        stage.draw()
//    }
//
//    override fun dispose() {
//        stage.dispose()
//    }
//}
