package main

import (
	"fmt"
	"time"

	"github.com/go-vgo/robotgo"
)

const (
	CHECKING_INTERVAL_SECONDS = 10
	MOVE_AFTER_SECONDS        = 300
)

func moveMouse() {
	// Move mouse around 100px
	robotgo.MoveSmoothRelative(100, 0)
	time.Sleep(1 * time.Second)
	robotgo.MoveSmoothRelative(-100, 0)
}

func displayStatus(lastMove *int) {
	// Clear display, and write last move
	fmt.Print("\033[H\033[2J")
	fmt.Printf("Last move %d seconds ago\n", *lastMove)
}

func checkActive(x *int, y *int, lastMove *int) {
	// Current x, and Current Y
	cX, cY := robotgo.Location()

	if cX == *x && cY == *y {
		// Hasn't moved
		*lastMove += CHECKING_INTERVAL_SECONDS
	} else {
		// Has moved
		*lastMove = 0
	}

	// Set current x and y
	*x = cX
	*y = cY

	// Check if we should move
	if *lastMove >= MOVE_AFTER_SECONDS {
		moveMouse()
		*lastMove = 0
	}

	displayStatus(lastMove)
}

func main() {
	// reactive variables haha (useState, or reactive(), but just pointers)
	x := 0
	y := 0
	// Meant to represent seconds ago
	lastMove := 0

	// Create tickers
	ticker := time.NewTicker(CHECKING_INTERVAL_SECONDS * time.Second)
	fmt.Printf("Checking active every %d seconds", CHECKING_INTERVAL_SECONDS)
	time.Sleep(5 * time.Second)

	defer ticker.Stop()

	// Run initial calls (optional)
	checkActive(&x, &y, &lastMove)

	// Goroutine to handle ticks
	for range ticker.C {
		checkActive(&x, &y, &lastMove)
	}
}
