package screens

import (
	"context"
	"fmt"
	"strings"

	"lab.zaar.be/thefish/alchemyst-go/effects"
	"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
	"lab.zaar.be/thefish/alchemyst-go/engine/gamestate"
	"lab.zaar.be/thefish/alchemyst-go/engine/types"
	"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
	"lab.zaar.be/thefish/alchemyst-go/util/appctx"
)

type DevmenuScreen struct {
    ctx        context.Context
    mw         *mainwindow.MainWindow
    controller *ecs.Controller
    scm        *types.ScreenManager
    state      *gamestate.GameState
    types.Rect

    renderParent bool

    bgColor string
    fgColor string
}

func NewDevmenuScreen(ctx context.Context, mw *mainwindow.MainWindow, controller *ecs.Controller, scm *types.ScreenManager, state *gamestate.GameState, rect types.Rect, renderParent bool) *DevmenuScreen {
    return &DevmenuScreen{
        ctx:        ctx,
        mw:         mw,
        controller: controller,
        scm:        scm,
        state:      state,

        Rect:         rect,
        renderParent: renderParent,
    }
}

func (devm *DevmenuScreen) SetBgColor(color string) *DevmenuScreen {
    devm.bgColor = color
    return devm
}

func (devm *DevmenuScreen) SetFgColor(color string) *DevmenuScreen {
    devm.fgColor = color
    return devm
}

func (devm *DevmenuScreen) UseEcs() bool { return false }
func (devm *DevmenuScreen) Enter() {

}
func (devm *DevmenuScreen) HandleInput(input string) {
    switch input {
    case "i":
        level := devm.state.Level
        for idx, _ := range level.Tiles {
            level.Tiles[idx].Visible = false
            level.Tiles[idx].Explored = false
        }
        devm.scm.SetScreen(devm.scm.PreviousScreen)
        break
    case "v":
        level := devm.state.Level
        for idx, _ := range level.Tiles {
            level.Tiles[idx].Visible = true
            level.Tiles[idx].Explored = true
        }
        appctx.Logger().Warn().Msg("making everything visible!")
        devm.scm.SetScreen(devm.scm.PreviousScreen)
        break
    case "p":
        if devm.controller.HasComponent(devm.state.Player, effects.BuffPassWall) {
            devm.controller.RemoveComponent(devm.state.Player, effects.BuffPassWall)
        } else {
            devm.controller.AddComponent(devm.state.Player, effects.PassWall{})
        }
    case "Escape":
        fallthrough
    case "Space":
        devm.scm.SetScreen(devm.scm.PreviousScreen)
        break
    }
}
func (devm *DevmenuScreen) Exit() {
    if devm.renderParent {
        devm.scm.PreviousScreen.Render()
    }
    menuLayer := devm.mw.GetLayer("menu")
    menuLayer.ClearRect(devm.Rect)
    bgLayer := devm.mw.GetLayer("menubg")
    bgLayer.ClearRect(devm.Rect)

}
func (devm *DevmenuScreen) Render() {
    menuLayer := devm.mw.GetLayer("menu")
    menuLayer.ClearRect(devm.Rect)
    bgLayer := devm.mw.GetLayer("menubg")
    bgLayer.ClearRect(devm.Rect)
    bgLayer.WithColor(devm.bgColor).NewWindow(devm.Rect).NoBorder()

    menuLayer.WithColor(devm.fgColor).PrintInside(
        devm.Rect,
        strings.Join([]string{
            "Действия для разработчика:",
            "[color=green]v[/color] - set all tiles visible",
            "[color=green]i[/color] - set all tiles invisible",
            fmt.Sprintf("[color=green]p[/color] - toggle passwall: %v",
                devm.controller.HasComponent(devm.state.Player, effects.BuffPassWall),
            ),
        }, "\n"),
        1,
    )
    menuLayer.Print(devm.X+2, devm.Y+devm.H-1, "[color=green]Space[/color] to close")

}
