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 TabKelainanLabServices interface {
	GetDataPresentaseKelainanLab(mcuID string) (models.Barchart, error)
	GetHematologi(mcuID string) (models.BarchartV2, error)
	GetUrinalisa(mcuID string) (models.BarchartV2, error)
	GetGangguanFungsiHati(mcuID string) (models.BarchartV2, error)
	GetGangguanMetabolismeLemak(mcuID string) (models.BarchartV2, error)
	GetPeningkatanGlukosa(mcuID string) (models.BarchartV2, error)
	GetChartRenderLab(mcuID string) ([]models.McuTabModels, error)
}

type TabKelainanLabHandler struct {
	TabKelainanLabServices TabKelainanLabServices
}

func NewTabKelainanLabHandler(tkl TabKelainanLabServices) *TabKelainanLabHandler {
	return &TabKelainanLabHandler{
		TabKelainanLabServices: tkl,
	}
}

func (tkl *TabKelainanLabHandler) HandleShowTabKelainanLab(c echo.Context) error {
	id := c.Param("id")

	tablist, err := tkl.TabKelainanLabServices.GetChartRenderLab(id)
	if err != nil {
		return err
	}

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

	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.TabKelainanLab(
		chartsxs,
		chartsmd,
		chartslg,
	)

	return utils.View(c, content)
}
