package processors

import (
	templateModels "github.com/titansys/appy-builder/internal/platform/shared/models"
	"github.com/titansys/appy-builder/internal/platforms/android-webview/engine"
)

// ThemeProcessor generates Material 3 color schemes
type ThemeProcessor struct{}

// NewThemeProcessor creates a new theme processor
func NewThemeProcessor() *ThemeProcessor {
	return &ThemeProcessor{}
}

// GenerateColorScheme generates a full Material 3 color scheme from a primary color
func (t *ThemeProcessor) GenerateColorScheme(primaryHex string) templateModels.ColorScheme {
	// Remove # prefix if present
	if len(primaryHex) > 0 && primaryHex[0] == '#' {
		primaryHex = primaryHex[1:]
	}

	// Default if invalid
	if len(primaryHex) != 6 {
		primaryHex = "6200EE" // Material default purple
	}

	scheme := templateModels.ColorScheme{}

	// Light theme colors
	scheme.LightPrimary = primaryHex
	scheme.LightOnPrimary = engine.GetContrastColor(primaryHex)
	scheme.LightPrimaryContainer = engine.AdjustBrightness(primaryHex, 1.8)
	scheme.LightOnPrimaryContainer = engine.AdjustBrightness(primaryHex, 0.3)

	// Generate secondary from primary (shift hue slightly)
	secondary := t.generateSecondary(primaryHex)
	scheme.LightSecondary = secondary
	scheme.LightOnSecondary = engine.GetContrastColor(secondary)
	scheme.LightSecondaryContainer = engine.AdjustBrightness(secondary, 1.8)
	scheme.LightOnSecondaryContainer = engine.AdjustBrightness(secondary, 0.3)

	// Generate tertiary from primary (shift hue more)
	tertiary := t.generateTertiary(primaryHex)
	scheme.LightTertiary = tertiary
	scheme.LightOnTertiary = engine.GetContrastColor(tertiary)
	scheme.LightTertiaryContainer = engine.AdjustBrightness(tertiary, 1.8)
	scheme.LightOnTertiaryContainer = engine.AdjustBrightness(tertiary, 0.3)

	// Error colors (Material default red)
	scheme.LightError = "B3261E"
	scheme.LightOnError = "FFFFFF"
	scheme.LightErrorContainer = "F9DEDC"
	scheme.LightOnErrorContainer = "410E0B"

	// Background and surface
	scheme.LightBackground = "FFFBFE"
	scheme.LightOnBackground = "1C1B1F"
	scheme.LightSurface = "FFFBFE"
	scheme.LightOnSurface = "1C1B1F"

	// Dark theme colors (inverse of light theme)
	scheme.DarkPrimary = engine.AdjustBrightness(primaryHex, 1.5)
	scheme.DarkOnPrimary = engine.AdjustBrightness(primaryHex, 0.4)
	scheme.DarkPrimaryContainer = engine.AdjustBrightness(primaryHex, 0.5)
	scheme.DarkOnPrimaryContainer = engine.AdjustBrightness(primaryHex, 1.9)

	scheme.DarkSecondary = engine.AdjustBrightness(secondary, 1.5)
	scheme.DarkOnSecondary = engine.AdjustBrightness(secondary, 0.4)
	scheme.DarkSecondaryContainer = engine.AdjustBrightness(secondary, 0.5)
	scheme.DarkOnSecondaryContainer = engine.AdjustBrightness(secondary, 1.9)

	scheme.DarkTertiary = engine.AdjustBrightness(tertiary, 1.5)
	scheme.DarkOnTertiary = engine.AdjustBrightness(tertiary, 0.4)
	scheme.DarkTertiaryContainer = engine.AdjustBrightness(tertiary, 0.5)
	scheme.DarkOnTertiaryContainer = engine.AdjustBrightness(tertiary, 1.9)

	scheme.DarkError = "F2B8B5"
	scheme.DarkOnError = "601410"
	scheme.DarkErrorContainer = "8C1D18"
	scheme.DarkOnErrorContainer = "F9DEDC"

	scheme.DarkBackground = "1C1B1F"
	scheme.DarkOnBackground = "E6E1E5"
	scheme.DarkSurface = "1C1B1F"
	scheme.DarkOnSurface = "E6E1E5"

	return scheme
}

// generateSecondary generates a secondary color by shifting hue
func (t *ThemeProcessor) generateSecondary(primaryHex string) string {
	r, g, b, err := engine.ParseHexColor(primaryHex)
	if err != nil {
		return "625B71" // Default secondary
	}

	// Shift towards blue-grey
	r = engine.Clamp(r-20, 0, 255)
	g = engine.Clamp(g-20, 0, 255)
	b = engine.Clamp(b+20, 0, 255)

	return engine.RGBToHex(r, g, b)
}

// generateTertiary generates a tertiary color by shifting hue more
func (t *ThemeProcessor) generateTertiary(primaryHex string) string {
	r, g, b, err := engine.ParseHexColor(primaryHex)
	if err != nil {
		return "7D5260" // Default tertiary
	}

	// Shift towards pink/red
	r = engine.Clamp(r+20, 0, 255)
	g = engine.Clamp(g-30, 0, 255)
	b = engine.Clamp(b-10, 0, 255)

	return engine.RGBToHex(r, g, b)
}

// ProcessTabColors processes tab icon colors with dark mode support
// Returns: lightIcon, lightActive, darkIcon, darkActive
func (t *ThemeProcessor) ProcessTabColors(themeColor, tabIconColor, activeTabColor string) (lightIcon, lightActive, darkIcon, darkActive string) {
	// Light mode colors
	if tabIconColor != "" {
		lightIcon = t.stripHashPrefix(tabIconColor)
	} else {
		lightIcon = engine.AdjustBrightness(themeColor, 0.7) // Darker for inactive
	}

	if activeTabColor != "" {
		lightActive = t.stripHashPrefix(activeTabColor)
	} else {
		lightActive = themeColor // Use theme color for active
	}

	// Dark mode colors (auto-adjust for contrast)
	// For dark mode, we need brighter colors for visibility
	darkIcon = t.adjustForDarkMode(lightIcon)
	darkActive = t.adjustForDarkMode(lightActive)

	return lightIcon, lightActive, darkIcon, darkActive
}

// stripHashPrefix removes # prefix from hex color if present
func (t *ThemeProcessor) stripHashPrefix(hexColor string) string {
	if len(hexColor) > 0 && hexColor[0] == '#' {
		return hexColor[1:]
	}
	return hexColor
}

// adjustForDarkMode brightens colors for better contrast in dark mode
func (t *ThemeProcessor) adjustForDarkMode(hexColor string) string {
	// Remove # prefix if present
	cleanHex := t.stripHashPrefix(hexColor)

	// For dark mode, increase brightness by 1.4x for better visibility
	// This ensures tab icons/active states stand out against dark backgrounds
	return engine.AdjustBrightness(cleanHex, 1.4)
}
