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

	 := borderRadiusClipPathID(, .ID)
	 := svg.OpenElement("clipPath", svg.Attr("id", .String()))
	 += fmt.Sprintf(`<path d="M %s %s L %s %s S %s %s %s %s `, svg.FormatFloat(.TopLeft.X), svg.FormatFloat(.TopLeft.Y+float64(.BorderRadius)), svg.FormatFloat(.TopLeft.X), svg.FormatFloat(.TopLeft.Y+float64(.BorderRadius)), svg.FormatFloat(.TopLeft.X), svg.FormatFloat(.TopLeft.Y), svg.FormatFloat(.TopLeft.X+float64(.BorderRadius)), svg.FormatFloat(.TopLeft.Y))
	 += fmt.Sprintf(`L %s %s L %s %s `, svg.FormatFloat(.TopLeft.X+.Width-float64(.BorderRadius)), svg.FormatFloat(.TopLeft.Y), svg.FormatFloat(-float64(.BorderRadius)), svg.FormatFloat())

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

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

func borderRadiusClipPathID(,  string) svg.ID {
	return svg.LiteralID(fmt.Sprintf("%s-%s", , svg.SVGID()))
}

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 {
		.SetClipPathID(borderRadiusClipPathID(, .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+,
		)
		.SetText()
		 += .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", )
	.SetText()
	 := .Render()

	.X +=  + d2target.TypePadding
	.Fill = .NeutralAccentColor
	.SetText()
	 += .Render()

	.X = .TopLeft.X + (.Width - d2target.NamePadding)
	.Fill = .SecondaryAccentColor
	.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "end", )
	.SetText()
	 += .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.Fprint(, svg.EmptyElement("image",
			svg.Attr("href", .Icon.String()),
			svg.FloatAttr("x", .X),
			svg.FloatAttr("y", .Y),
			svg.IntAttr("width", ),
			svg.IntAttr("height", ),
		))
	}
}