package main

import (
	"fmt"
//	"math/rand"
	"os"
//	"strings"
//	"time"

	"adventofcode2022/utils"
	"adventofcode2022/day01"
	"adventofcode2022/day02"
	"adventofcode2022/day03"
	"adventofcode2022/day04"
	"adventofcode2022/day05"
	"adventofcode2022/day06"
	"adventofcode2022/day07"
	"adventofcode2022/day08"
	"adventofcode2022/day09"
	"adventofcode2022/day10"
	"adventofcode2022/day11"


)

// Usage: go run main.go <NN>
// assumes input is in day<NN>/input.txt
func main() {
	d := day()
	fmt.Printf("Running day %02d\n", d)

	switch d {
	case 1:
		fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
	case 2:
		fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
	case 3:
		fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
	case 4:
		fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d)))
	case 5:
		fmt.Printf("part 1: %s\n", day05.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %s\n", day05.Part2(utils.Readfile(d)))
	case 6:
		fmt.Printf("part 1: %d\n", day06.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day06.Part2(utils.Readfile(d)))
	case 7:
		fmt.Printf("part 1: %d\n", day07.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day07.Part2(utils.Readfile(d)))
	case 8:
		fmt.Printf("part 1: %d\n", day08.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day08.Part2(utils.Readfile(d)))
	case 9:
		fmt.Printf("part 1: %d\n", day09.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day09.Part2(utils.Readfile(d)))
	case 10:
		fmt.Printf("part 1: %d\n", day10.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %s\n", day10.Part2(utils.Readfile(d)))
	case 11:
		fmt.Printf("part 1: %d\n", day11.Part1(utils.Readfile(d)))
		fmt.Printf("part 2: %d\n", day11.Part2(utils.Readfile(d)))
	default:
		panic(fmt.Errorf("no such day: %d", d))
	}
}

// Reads day from os.Args.
func day() int {
	latest := 10
	if len(os.Args) == 1 {
		return latest
	}

	if os.Args[1] == "next" {
		genNext(latest + 1)
		os.Exit(0)
	}
	day := utils.MustAtoi(os.Args[1])
	return day
}


func genNext(n int) {
	os.Mkdir(fmt.Sprintf("day%02d", n), 0755)
	f, err := os.Create(fmt.Sprintf("day%02d/day%02d.go", n, n))
	utils.PanicOnErr(err)
	defer f.Close()
	f.WriteString(fmt.Sprintf(`package day%02d

func Part1(input string) int {
	return 0
}

func Part2(input string) int {
	return 0
}
`, n))
	fmt.Printf("wrote day%02d/day%02d.go\n", n, n)

	f, err = os.Create(fmt.Sprintf("day%02d/day%02d_test.go", n, n))
	utils.PanicOnErr(err)
	defer f.Close()
	f.WriteString(fmt.Sprintf(`package day%02d

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestPart1(t *testing.T) {
	r := Part1("")
	require.Equal(t, 0, r)
}

func TestPart2(t *testing.T) {
	r := Part2("")
	require.Equal(t, 0, r)
}
`, n))
	fmt.Printf("wrote day%02d/day%02d_test.go\n", n, n)

}