package d2themes

import (
	
	

	
)

// ThemableElement is a helper class for creating new XML elements.
// This should be preferred over formatting and must be used
// whenever Fill, Stroke, BackgroundColor or Color contains a color from a theme.
// i.e. N[1-7] | B[1-6] | AA[245] | AB[45]
type ThemableElement struct {
	tag string

	X      float64
	X1     float64
	X2     float64
	Y      float64
	Y1     float64
	Y2     float64
	Width  float64
	Height float64
	R      float64
	Rx     float64
	Ry     float64
	Cx     float64
	Cy     float64

	D         string
	Mask      string
	Points    string
	Transform string
	Href      string
	Xmlns     string

	Fill            string
	Stroke          string
	StrokeDashArray string
	BackgroundColor string
	Color           string

	ClassName  string
	Style      string
	Attributes string

	Content  string
	ClipPath string

	FillPattern string

	inlineTheme *Theme
}

func ( string,  *Theme) *ThemableElement {
	 := ""
	if  == "div" {
		 = "http://www.w3.org/1999/xhtml"
	}

	return &ThemableElement{
		,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		math.MaxFloat64,
		"",
		"",
		"",
		"",
		"",
		,
		color.Empty,
		color.Empty,
		color.Empty,
		color.Empty,
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		,
	}
}

func ( *ThemableElement) () *ThemableElement {
	 := *
	return &
}

func ( *ThemableElement) (,  float64) {
	.Transform = fmt.Sprintf("translate(%f %f)", , )
}

func ( *ThemableElement) ( string) {
	.Mask = fmt.Sprintf("url(#%s)", )
}

func ( *ThemableElement) () string {
	 := "<" + .tag

	// href has to be at the top for the img bundler to detect <image> tags correctly
	if len(.Href) > 0 {
		 += fmt.Sprintf(` href="%s"`, .Href)
	}
	if .X != math.MaxFloat64 {
		 += fmt.Sprintf(` x="%f"`, .X)
	}
	if .X1 != math.MaxFloat64 {
		 += fmt.Sprintf(` x1="%f"`, .X1)
	}
	if .X2 != math.MaxFloat64 {
		 += fmt.Sprintf(` x2="%f"`, .X2)
	}
	if .Y != math.MaxFloat64 {
		 += fmt.Sprintf(` y="%f"`, .Y)
	}
	if .Y1 != math.MaxFloat64 {
		 += fmt.Sprintf(` y1="%f"`, .Y1)
	}
	if .Y2 != math.MaxFloat64 {
		 += fmt.Sprintf(` y2="%f"`, .Y2)
	}
	if .Width != math.MaxFloat64 {
		 += fmt.Sprintf(` width="%f"`, .Width)
	}
	if .Height != math.MaxFloat64 {
		 += fmt.Sprintf(` height="%f"`, .Height)
	}
	if .R != math.MaxFloat64 {
		 += fmt.Sprintf(` r="%f"`, .R)
	}
	if .Rx != math.MaxFloat64 {
		 += fmt.Sprintf(` rx="%f"`, calculateAxisRadius(.Rx, .Width, .Height))
	}
	if .Ry != math.MaxFloat64 {
		 += fmt.Sprintf(` ry="%f"`, calculateAxisRadius(.Ry, .Width, .Height))
	}
	if .Cx != math.MaxFloat64 {
		 += fmt.Sprintf(` cx="%f"`, .Cx)
	}
	if .Cy != math.MaxFloat64 {
		 += fmt.Sprintf(` cy="%f"`, .Cy)
	}
	if .StrokeDashArray != "" {
		 += fmt.Sprintf(` stroke-dasharray="%s"`, .StrokeDashArray)
	}

	if len(.D) > 0 {
		 += fmt.Sprintf(` d="%s"`, .D)
	}
	if len(.Mask) > 0 {
		 += fmt.Sprintf(` mask="%s"`, .Mask)
	}
	if len(.Points) > 0 {
		 += fmt.Sprintf(` points="%s"`, .Points)
	}
	if len(.Transform) > 0 {
		 += fmt.Sprintf(` transform="%s"`, .Transform)
	}
	if len(.Xmlns) > 0 {
		 += fmt.Sprintf(` xmlns="%s"`, .Xmlns)
	}

	 := .ClassName
	 := .Style

	// Add class {property}-{theme color} if the color is from a theme, set the property otherwise
	if color.IsThemeColor(.Stroke) {
		 += fmt.Sprintf(" stroke-%s", .Stroke)
		if .inlineTheme != nil {
			 += fmt.Sprintf(` stroke="%s"`, ResolveThemeColor(*.inlineTheme, .Stroke))
		}
	} else if len(.Stroke) > 0 {
		if color.IsGradient(.Stroke) {
			.Stroke = fmt.Sprintf("url('#%s')", color.UniqueGradientID(.Stroke))
		}
		 += fmt.Sprintf(` stroke="%s"`, .Stroke)
	}
	if color.IsThemeColor(.Fill) {
		 += fmt.Sprintf(" fill-%s", .Fill)
		if .inlineTheme != nil {
			 += fmt.Sprintf(` fill="%s"`, ResolveThemeColor(*.inlineTheme, .Fill))
		}
	} else if len(.Fill) > 0 {
		if color.IsGradient(.Fill) {
			.Fill = fmt.Sprintf("url('#%s')", color.UniqueGradientID(.Fill))
		}
		 += fmt.Sprintf(` fill="%s"`, .Fill)
	}
	if color.IsThemeColor(.BackgroundColor) {
		 += fmt.Sprintf(" background-color-%s", .BackgroundColor)
		if .inlineTheme != nil {
			 += fmt.Sprintf(` background-color="%s"`, ResolveThemeColor(*.inlineTheme, .BackgroundColor))
		}
	} else if len(.BackgroundColor) > 0 {
		 += fmt.Sprintf(` background-color="%s"`, .BackgroundColor)
	}
	if color.IsThemeColor(.Color) {
		 += fmt.Sprintf(" color-%s", .Color)
		if .inlineTheme != nil {
			 += fmt.Sprintf(` color="%s"`, ResolveThemeColor(*.inlineTheme, .Color))
		}
	} else if len(.Color) > 0 {
		 += fmt.Sprintf(` color="%s"`, .Color)
	}

	if len() > 0 {
		 += fmt.Sprintf(` class="%s"`, )
	}
	if len() > 0 {
		 += fmt.Sprintf(` style="%s"`, )
	}
	if len(.Attributes) > 0 {
		 += fmt.Sprintf(` %s`, .Attributes)
	}

	if len(.ClipPath) > 0 {
		 += fmt.Sprintf(` clip-path="url(#%s)"`, .ClipPath)
	}

	if len(.Content) > 0 {
		return fmt.Sprintf("%s>%s</%s>", , .Content, .tag)
	}

	 += " />"
	if .FillPattern != "" && .FillPattern != "none" {
		 := .Copy()
		.Fill = ""
		.Stroke = ""
		.BackgroundColor = ""
		.Color = ""
		.ClassName = fmt.Sprintf("%s-overlay", .FillPattern)
		.FillPattern = ""
		 += .()
	}
	return 
}

func calculateAxisRadius(, ,  float64) float64 {
	 := math.Min(, )
	 :=  / 2.0
	return math.Min(, )
}