package launcher

import (
	"epochsilicon/pkg/log"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strings"

	"epochsilicon/pkg/paths"
)

// RecommendedSettings contains the recommended graphics settings for optimal performance
var RecommendedSettings = map[string]string{
	"farclip":              "177",
	"M2UseShaders":         "1",
	"gxColorBits":          "24",
	"gxDepthBits":          "24",
	"gxMultisampleQuality": "0.000000",
	"gxMultisample":        "2",
}

// CheckRecommendedSettings reads the Config.wtf file and checks if all recommended settings are applied
// Returns true if all settings are correctly applied, false otherwise
func CheckRecommendedSettings() bool {
	if paths.EpochPath == "" {
		log.Debugf("Epoch path not set, cannot check Config.wtf")
		return false
	}

	configPath := filepath.Join(paths.EpochPath, "WTF", "Config.wtf")

	if _, err := os.Stat(configPath); os.IsNotExist(err) {
		log.Debugf("Config.wtf not found at %s", configPath)
		return false
	}

	content, err := os.ReadFile(configPath)
	if err != nil {
		log.Debugf("Failed to read Config.wtf: %v", err)
		return false
	}

	configText := string(content)

	// Check each recommended setting
	for setting, expectedValue := range RecommendedSettings {
		if !isSettingCorrect(configText, setting, expectedValue) {
			log.Debugf("Setting %s not found or incorrect in Config.wtf", setting)
			return false
		}
	}

	log.Debugf("All recommended settings are correctly applied")
	return true
}

// isSettingCorrect checks if a specific setting has the correct value in the config text
func isSettingCorrect(configText, setting, expectedValue string) bool {
	// Create regex pattern to match the setting
	pattern := fmt.Sprintf(`SET\s+%s\s+"([^"]*)"`, regexp.QuoteMeta(setting))
	re := regexp.MustCompile(pattern)

	matches := re.FindStringSubmatch(configText)
	if len(matches) < 2 {
		return false
	}

	currentValue := matches[1]
	return currentValue == expectedValue
}

// ApplyRecommendedSettings applies all recommended graphics settings to Config.wtf
func ApplyRecommendedSettings() error {
	if paths.EpochPath == "" {
		return fmt.Errorf("Epoch path not set")
	}

	configPath := filepath.Join(paths.EpochPath, "WTF", "Config.wtf")

	// Create WTF directory if it doesn't exist
	wtfDir := filepath.Dir(configPath)
	if err := os.MkdirAll(wtfDir, 0755); err != nil {
		return fmt.Errorf("failed to create WTF directory: %v", err)
	}

	var configText string

	// Read existing config if it exists
	if content, err := os.ReadFile(configPath); err == nil {
		configText = string(content)
	} else {
		log.Debugf("Config.wtf not found, creating new file")
		configText = ""
	}

	// Apply each recommended setting
	for setting, value := range RecommendedSettings {
		configText = updateOrAddSetting(configText, setting, value)
	}

	// Write the updated config back to file
	if err := os.WriteFile(configPath, []byte(configText), 0644); err != nil {
		return fmt.Errorf("failed to write Config.wtf: %v", err)
	}

	log.Debugf("Successfully applied recommended settings to Config.wtf")
	return nil
}

// updateOrAddSetting updates an existing setting or adds a new one if it doesn't exist
func updateOrAddSetting(configText, setting, value string) string {
	// Create regex pattern to match the setting
	pattern := fmt.Sprintf(`SET\s+%s\s+"[^"]*"`, regexp.QuoteMeta(setting))
	re := regexp.MustCompile(pattern)

	newSetting := fmt.Sprintf(`SET %s "%s"`, setting, value)

	if re.MatchString(configText) {
		// Replace existing setting
		configText = re.ReplaceAllString(configText, newSetting)
		log.Debugf("Updated setting %s to %s", setting, value)
	} else {
		// Add new setting
		if configText != "" && !strings.HasSuffix(configText, "\n") {
			configText += "\n"
		}
		configText += newSetting + "\n"
		log.Debugf("Added new setting %s with value %s", setting, value)
	}

	return configText
}
