package dev_handlers

import (
	"cpone/models"
	"cpone/utils"
	"html/template"
	"strings"

	xtemplmulti "cpone/views/dev/xtemplmulti"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
)

type XTemplMultiService interface {
}

func NewXTemplMultiHandler(us XTemplMultiService) *XTemplMultiHandler {
	return &XTemplMultiHandler{
		XTemplMultiService: us,
	}
}

type XTemplMultiHandler struct {
	XTemplMultiService XTemplMultiService
}

func (lh *MasterMenuUserGroupHandler) HandlerXTemplMulti(c echo.Context) error {
	var arrTempl []models.XtemplModel
	// toastComponent := customtoastv2.CustomToastV2Show("sukses", "halo", "warning")

	newItem := models.XtemplModel{
		NameTempl:  "Komponen 1",
		HtmlString: "<div style='background-color:red;'>Komponen 1<div>"}

	newItem2 := models.XtemplModel{
		NameTempl:  "Komponen 2",
		HtmlString: "<div style='background-color:purple;'>Komponen 2<div>"}

	arrTempl = append(arrTempl, newItem, newItem2)

	var stringx string

	for _, item := range arrTempl {
		stringx += item.HtmlString
	}

	tpl := template.Must(template.New("index").Parse(stringx))

	var stringBuilder strings.Builder
	if err := tpl.Execute(&stringBuilder, arrTempl); err != nil {
		panic(err)
	}

	newItemFix := models.XtemplModel{
		NameTempl:  "Template Gabungan",
		HtmlString: stringBuilder.String(),
	}

	arrTemplFix := []models.XtemplModel{newItemFix}

	var templx []templ.Component
	si := xtemplmulti.MainXTemplMulti(arrTemplFix)
	templx = append(templx, si)
	templx = append(templx, si)

	// return utils.View(c, si)
	return utils.ViewMulti(c, templx)
}
