package main

import (
	"database/sql"
	"flag"
	"fmt"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/wlbr/feiertage"
)


var samstag = []string{"Sa A", "Sa B", "Sa C"}
var montag = []string{"Mo I", "Mo II"}
var mittwoch = []string{"Mi I", "Mi II"}

var sams int
var mont int
var mitt int

func bumpgroup(wd int) string {
	grp := ""
	switch wd {
	case 6:
		grp = samstag[sams]
		sams = sams + 1
		if sams == 3 {
			sams = 0;
		}
	case 1:
		grp = montag[mont]
		mont = mont + 1
		if mont == 2 {
			mont = 0;
		}
	case 3:
		grp = mittwoch[mitt]
		mitt = mitt + 1
		if mitt == 2 {
			mitt = 0
		}
	}
	return grp
}


func printDate (d time.Time, db *sql.DB, ft []feiertage.Feiertag, grp string, store bool) {
	zeiten := [13]int{0,20,20,20,21,22,22,22,22,21,20,20,20}
	wdays := [7]string{"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}

	year, month, day := d.Date()
	wd := int(d.Weekday())
	zt := zeiten[month]
//	fmt.Printf("%v-%02d-%02d %9v  %v Uhr\n", year, int(month), day, wdays[int(wd)], zt)

	if Contains(ft, d) || d.Equal(time.Date(year, 12, 31, 0, 0, 0, 0, time.Local)) || d.Equal(time.Date(year, 12, 24, 0, 0, 0, 0, time.Local)) {
				fmt.Println("*********  Feiertag")
		return
	}

	query := fmt.Sprintf("INSERT INTO fdatum1 (wtag, datum, uhrzeit, grp, count) VALUES ('%v', %v%02d%02d, '%v Uhr', '%v', 0)",
		wdays[int(wd)], year, int(month), day, zt, grp)
	fmt.Println(query)

	if store {
		insert, err := db.Query(query)
		if err != nil {
			panic(err.Error())
		}
		insert.Close()
	}
}

func createDates(year int, db *sql.DB, ft []feiertage.Feiertag, doStore bool) {
	d := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
	for i := 1; i< 366; i++ {
		wd := int(d.Weekday())
		grp := bumpgroup(wd)
		if d.Month() <= 4 || d.Month() >=9 {
			if wd == 1 || wd == 3 {
				printDate(d, db, ft, grp, doStore); 
			}
		} 
		if wd == 6 {
			printDate(d, db, ft, grp, doStore)
		}
		d = d.AddDate(0,0,1)
		if d.Year() > year {
			break;
		}
	}
}

// Contains tells whether a contains x.
func Contains(a []feiertage.Feiertag, x time.Time) bool {
	for _, n := range a {
			if x.Equal(n.Time) {
					return true
			}
	}
	return false
}


func sendAllOff() {

	var storeit bool

	yearPtr := flag.Int("year", 2026, "Für das Jahr 'year' berechnen")
	samPtr := flag.String("sams", "Sa A", "Start für Samstag (möglich: Sa A, Sa B, Sa C)")
	monPtr := flag.String("mont", "Mo I", "Start für Montag (möglich: Mo I, Mo II)")
	mitPtr := flag.String("mitt", "Mi I", "Start für Mittwoch  (möglich: Mi I, Mi II)")
  flag.BoolVar(&storeit, "store", false, "in der DB speichern?")


	flag.Parse()

	fmt.Println("year:", *yearPtr)
	fmt.Println("samstag:", *samPtr)
	fmt.Println("montag:", *monPtr)
	fmt.Println("mittwoch:", *mitPtr)
	fmt.Println("store:", storeit)

	switch *samPtr {
	case "Sa A":
		sams = 0
	case "Sa B":
		sams = 1
	default:
		sams = 2
	}

	if *monPtr == "Mo I" {
		mont = 0
	} else {
		mont = 1
	}
	if *mitPtr == "Mi I" {
		mitt = 0
	} else {
		mitt = 1
	}

	feiertage.SetDefaultTimeZone(time.Local)
	ft := feiertage.BadenWürttemberg(*yearPtr)
	fmt.Println(ft)

	db, err := sql.Open("mysql", "root:SFluorit@tcp(127.0.0.1:3306)/sternwarte")
	if err != nil {
					panic(err.Error())
	}

	db.SetConnMaxLifetime(time.Minute * 3)
	db.SetMaxOpenConns(10)
	db.SetMaxIdleConns(10)


	defer db.Close()

	createDates(*yearPtr, db, ft.Feiertage, storeit)
}

func main() {
	sendAllOff()
}

