package mcu_corporate_handlers

import (
	"cpone/component/chart"
	"cpone/models"
	"cpone/utils"
	corporate_mcudetail "cpone/views/corporate/mcu/mcutab"
	"strings"

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

type TabKelainanGlobalServices interface {
	GetKelainanFisik(mcuID string) (models.Barchart, error)
	GetKelainanGlobalV2(mcuID string) (models.Barchart, error)
	GetChartRenderGlobal(mcuID string) ([]models.McuTabModels, error)
}

type TabKelainanGlobalHandler struct {
	TabKelainanGlobalServices TabKelainanGlobalServices
}

func NewTabKelainanGlobalHandler(tkg TabKelainanGlobalServices) *TabKelainanGlobalHandler {
	return &TabKelainanGlobalHandler{
		TabKelainanGlobalServices: tkg,
	}
}

func (tkg *TabKelainanGlobalHandler) HandleShowTabKelainanGlobal(c echo.Context) error {
	id := c.Param("id")

	chartsxs := []templ.Component{}
	chartsmd := []templ.Component{}
	chartslg := []templ.Component{}

	tablist, err := tkg.TabKelainanGlobalServices.GetChartRenderGlobal(id)
	if err != nil {
		return err
	}

	for _, d := range tablist {
		chartsxs = append(chartsxs, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"xs", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
		chartsmd = append(chartsmd, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"md", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
		chartslg = append(chartslg, chart.ShowChartResponsiveWidth(
			strings.ToLower(strings.ReplaceAll(d.Mcu_ImageGrafikName+"lg", " ", "")),
			d.Mcu_ImageGrafikJsonRender,
			"450px",
		))
	}

	content := corporate_mcudetail.TabKelainanGlobal(
		chartsxs,
		chartsmd,
		chartslg,
	)

	return utils.View(c, content)
}
