package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"strings"

	_ "embed"
)

var Error *log.Logger
var Warning *log.Logger

func init() {
	log.SetFlags(log.Lmicroseconds | log.Lshortfile)
	logFile, err := os.Create("main.log")
	if err != nil {
		log.Printf("Error creating log file: %v", err)
		os.Exit(1)
	}
	logger := io.MultiWriter(os.Stdout, logFile)
	log.SetOutput(logger)

	Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
		fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
		log.Lmicroseconds|log.Lshortfile)
	Warning = log.New(io.MultiWriter(logFile, os.Stdout),
		fmt.Sprintf("%sWarning:%s ", "\033[0;93m", "\033[0m"),
		log.Lmicroseconds|log.Lshortfile)
}

var db DB

func main() {
	db = DB{
		path: "../data/db.db",
	}
	err := db.Open()
	if err != nil {
		Error.Printf("Error opening database: %v", err)
		return
	}
	defer db.Close()

	res, err := db.readConn.Query("SELECT name FROM achievements GROUP BY name")
	if err != nil {
		Error.Printf("Error reading from database: %v", err)
		return
	}
	defer res.Close()

	characters := map[string]struct{}{}
	for res.Next() {
		var name string
		err = res.Scan(&name)
		if err != nil {
			Error.Printf("Error scanning row: %v", err)
			return
		}
		characters[name] = struct{}{}
	}
	log.Printf("Loaded %d characters", len(characters))

	//	err = CookWeakAuras("./WeakAuras.lua", characters)
	//	if err != nil {
	//		Error.Printf("Error cooking WeakAuras.lua: %v", err)
	//		return
	//	}

	weakauras := []string{
		"C:/Users/Administrator/Seafile/WoW/Ruski/WTF/Account/Iridian/SavedVariables/WeakAuras.lua",
		"C:/Users/Administrator/Seafile/WoW/Ruski/WTF/Account/phatphuckdave/SavedVariables/WeakAuras.lua",
		"C:/Users/Administrator/Seafile/WoW/Ruski/WTF/Account/phaterphuckdave/SavedVariables/WeakAuras.lua",
	}
	for _, path := range weakauras {
		err := CookWeakAuras(path, characters)
		if err != nil {
			Error.Printf("Error cooking WeakAuras.lua: %v", err)
			return
		}
	}
}

func CookWeakAuras(path string, characters map[string]struct{}) error {
	filedata, err := os.ReadFile(path)
	if err != nil {
		return err
	}

	modifiedLines := []string{}
	lines := strings.Split(string(filedata), "\n")
	log.Printf("Original lines: %d", len(lines))
	inTable := false
	for _, line := range lines {
		if strings.Contains(line, `		["AchievementSniffer2"] = {`) {
			inTable = true
			modifiedLines = append(modifiedLines, line)
			for character := range characters {
				modifiedLines = append(modifiedLines, "\t\t\t"+fmt.Sprintf(`["%s"] = true,`, character))
			}
			continue
		}
		if inTable {
			if strings.Contains(line, "},") {
				inTable = false
				modifiedLines = append(modifiedLines, line)
			}
			continue
		}
		modifiedLines = append(modifiedLines, line)
	}

	log.Printf("Modified lines: %d", len(modifiedLines))
	err = os.WriteFile(path, []byte(strings.Join(modifiedLines, "\n")), 0644)
	if err != nil {
		return err
	}
	err = os.WriteFile(path+".bak", []byte(strings.Join(lines, "\n")), 0644)
	if err != nil {
		return err
	}

	return nil
}

// /run WeakAurasSaved.Cyka.MData = nil
