package main

import (
	"image/color"
	"log"
	"os"

	"tetris/internal/font"
	"tetris/internal/game"
	"tetris/pkg/config"

	"github.com/hajimehoshi/ebiten/v2"
)

type FontTestGame struct {
	fontRenderer *font.FontRenderer
}

func (g *FontTestGame) Update() error {
	return nil
}

func (g *FontTestGame) Draw(screen *ebiten.Image) {
	// Test Chinese text rendering
	g.fontRenderer.DrawText(screen, "下一个:", 50, 50, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "分数: 1000", 50, 80, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "等级: 5", 50, 110, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "操作说明:", 50, 140, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "← → 移动", 50, 170, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "↑ 旋转", 50, 200, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "↓ 软降落", 50, 230, color.RGBA{255, 255, 255, 255})
	g.fontRenderer.DrawText(screen, "空格 硬降落", 50, 260, color.RGBA{255, 255, 255, 255})

	// Test English text
	g.fontRenderer.DrawText(screen, "Font Test - English", 300, 50, color.RGBA{0, 255, 0, 255})
	g.fontRenderer.DrawText(screen, "NEXT:", 300, 80, color.RGBA{0, 255, 0, 255})
	g.fontRenderer.DrawText(screen, "Score: 1000", 300, 110, color.RGBA{0, 255, 0, 255})
	g.fontRenderer.DrawText(screen, "Level: 5", 300, 140, color.RGBA{0, 255, 0, 255})
}

func (g *FontTestGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
	return 640, 480
}

func testFont() {
	log.Println("Testing Chinese font rendering...")
	game := &FontTestGame{
		fontRenderer: font.NewFontRenderer(),
	}

	ebiten.SetWindowSize(640, 480)
	ebiten.SetWindowTitle("Chinese Font Test")

	if err := ebiten.RunGame(game); err != nil {
		log.Fatal(err)
	}
}

func main() {
	// Check if font test is requested
	if len(os.Args) > 1 && os.Args[1] == "--test-font" {
		testFont()
		return
	}

	ebiten.SetWindowSize(config.ScreenWidth, config.ScreenHeight)
	ebiten.SetWindowTitle("Tetris")

	g := game.New()
	if err := ebiten.RunGame(g); err != nil {
		log.Fatal(err)
	}
}
