package main

import (
	"math/rand/v2"
	"os"

	"github.com/gdamore/tcell/v2"
)

func randNum() int {
	n := rand.IntN(100)
	if n < 85 {
		return 0
	} else {
		return 1
	}
}

func main() {
	// create new screen
	s, err := tcell.NewScreen()
	if err != nil {
		panic(err)
	}

	// initialise screen
	if err := s.Init(); err != nil {
		panic(err)
	}

	// polling to be able to quit
	
	style := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorPurple)
	s.SetStyle(style)
	h, w := s.Size()

	var cells [][]Cell
	for r := 0; r<h; r++ {
		var row []Cell
		for c := 0; c<w; c++ {
			newCell := Cell{r, c, randNum()}
			row = append(row, newCell)
		}
		cells = append(cells, row)
	}

	game := Game{
		Screen: s,
		Cells: cells,
	}
	println("about to start")

	// game = game.Glider()

	for {
		newGame := game.Run()
		switch event := s.PollEvent().(type) {
		case *tcell.EventResize:
			s.Sync()
		case *tcell.EventKey:
			if event.Rune() == 'q' || event.Key() == tcell.KeyCtrlC {
				s.Fini()
				os.Exit(0)
			}
		}
		game = newGame
	}
}
