package d2svg

import (
	
	
	

	
	
	
	
	
	
)

// this func helps define a clipPath for shape class and sql_table to draw border-radius
func clipPathForBorderRadius( string,  d2target.Shape) string {
	 := geo.NewBox(
		geo.NewPoint(float64(.Pos.X), float64(.Pos.Y)),
		float64(.Width),
		float64(.Height),
	)
	,  := .TopLeft.X+.Width, .TopLeft.Y

	 := fmt.Sprintf(`<clipPath id="%v-%v">`, , .ID)
	 += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, .TopLeft.X, .TopLeft.Y+float64(.BorderRadius), .TopLeft.X, .TopLeft.Y+float64(.BorderRadius), .TopLeft.X, .TopLeft.Y, .TopLeft.X+float64(.BorderRadius), .TopLeft.Y)
	 += fmt.Sprintf(`L %f %f L %f %f `, .TopLeft.X+.Width-float64(.BorderRadius), .TopLeft.Y, -float64(.BorderRadius), )

	 += fmt.Sprintf(`S %f %f %f %f `, , , , +float64(.BorderRadius))
	 += fmt.Sprintf(`L %f %f `, , +.Height-float64(.BorderRadius))

	if len(.Columns) != 0 {
		 += fmt.Sprintf(`L %f %f L %f %f`, , +.Height, .TopLeft.X, .TopLeft.Y+.Height)
	} else {
		 += fmt.Sprintf(`S %f % f %f %f `, , +.Height, -float64(.BorderRadius), +.Height)
		 += fmt.Sprintf(`L %f %f `, .TopLeft.X+float64(.BorderRadius), .TopLeft.Y+.Height)
		 += fmt.Sprintf(`S %f %f %f %f`, .TopLeft.X, .TopLeft.Y+.Height, .TopLeft.X, .TopLeft.Y+.Height-float64(.BorderRadius))
		 += fmt.Sprintf(`L %f %f`, .TopLeft.X, .TopLeft.Y+float64(.BorderRadius))
	}
	 += fmt.Sprintf(`Z %f %f" `, .TopLeft.X, .TopLeft.Y)
	return  + `fill="none" /> </clipPath>`
}

func tableHeader( string,  d2target.Shape,  *geo.Box,  string, , ,  float64,  *d2themes.Theme) string {
	 := d2themes.NewThemableElement("rect", )
	.X, .Y = .TopLeft.X, .TopLeft.Y
	.Width, .Height = .Width, .Height
	.Fill = .Fill
	.FillPattern = .FillPattern
	.ClassName = "class_header"
	if .BorderRadius != 0 {
		.ClipPath = fmt.Sprintf("%v-%v", , .ID)
	}
	 := .Render()

	if  != "" {
		 := label.InsideMiddleLeft.GetPointOnBox(
			,
			float64(d2target.HeaderPadding),
			float64(.Width),
			,
		)

		 := d2themes.NewThemableElement("text", )
		.X = .X
		.Y = .Y + *3/4
		.Fill = .GetFontColor()
		.ClassName = "text"
		.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx",
			"start", 4+,
		)
		.Content = svg.EscapeText()
		 += .Render()
	}
	return 
}

func tableRow( d2target.Shape,  *geo.Box, , ,  string, , ,  float64,  *d2themes.Theme) string {
	// Row is made up of name, type, and constraint
	// e.g. | diagram   int   FK |
	 := label.InsideMiddleLeft.GetPointOnBox(
		,
		d2target.NamePadding,
		0,
		,
	)

	 := d2themes.NewThemableElement("text", )
	.X = .X
	.Y = .Y + *3/4
	.Fill = .PrimaryAccentColor
	.ClassName = "text"
	.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "start", )
	.Content = svg.EscapeText()
	 := .Render()

	.X +=  + d2target.TypePadding
	.Fill = .NeutralAccentColor
	.Content = svg.EscapeText()
	 += .Render()

	.X = .TopLeft.X + (.Width - d2target.NamePadding)
	.Fill = .SecondaryAccentColor
	.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "end", )
	.Content = 
	 += .Render()

	return 
}

func drawTable( io.Writer,  string,  d2target.Shape,  *d2themes.Theme) {
	 := d2themes.NewThemableElement("rect", )
	.X = float64(.Pos.X)
	.Y = float64(.Pos.Y)
	.Width = float64(.Width)
	.Height = float64(.Height)
	.Fill, .Stroke = d2themes.ShapeTheme()
	.FillPattern = .FillPattern
	.ClassName = "shape"
	.Style = .CSSStyle()
	if .BorderRadius != 0 {
		.Rx = float64(.BorderRadius)
		.Ry = float64(.BorderRadius)
	}
	fmt.Fprint(, .Render())

	 := geo.NewBox(
		geo.NewPoint(float64(.Pos.X), float64(.Pos.Y)),
		float64(.Width),
		float64(.Height),
	)
	 := .Height / float64(1+len(.SQLTable.Columns))
	 := geo.NewBox(.TopLeft, .Width, )

	fmt.Fprint(,
		tableHeader(, , , .Label,
			float64(.LabelWidth), float64(.LabelHeight), float64(.FontSize), ),
	)

	var  int
	var  int
	for ,  := range .Columns {
		 = go2.Max(, .Name.LabelWidth)
		 = go2.Max(, .Type.LabelWidth)
	}

	 := geo.NewBox(.TopLeft.Copy(), .Width, )
	.TopLeft.Y += .Height
	for ,  := range .Columns {
		fmt.Fprint(,
			tableRow(, , .Name.Label, .Type.Label, .ConstraintAbbr(), float64(.FontSize), float64(), float64(), ),
		)
		.TopLeft.Y += 

		 := d2themes.NewThemableElement("line", )
		if  == len(.Columns)-1 && .BorderRadius != 0 {
			.X1, .Y1 = .TopLeft.X+float64(.BorderRadius), .TopLeft.Y
			.X2, .Y2 = .TopLeft.X+.Width-float64(.BorderRadius), .TopLeft.Y
		} else {
			.X1, .Y1 = .TopLeft.X, .TopLeft.Y
			.X2, .Y2 = .TopLeft.X+.Width, .TopLeft.Y
		}
		.Stroke = .Fill
		.Style = "stroke-width:2"
		fmt.Fprint(, .Render())
	}

	if .Icon != nil && .Type != d2target.ShapeImage {
		 := label.FromString(.IconPosition)
		 := d2target.GetIconSize(, .IconPosition)

		 := .GetPointOnBox(, label.PADDING, float64(), float64())

		fmt.Fprintf(, `<image href="%s" x="%f" y="%f" width="%d" height="%d" />`,
			html.EscapeString(.Icon.String()),
			.X,
			.Y,
			,
			,
		)
	}
}