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 TabKelainanNonLabServices interface {
	GetDataKelNonLab(mcuID string) (models.Barchart, error)
	GetThoraxPA(mcuID string) (models.BarchartV2, error)
	GetGangguanJantung(mcuID string) (models.BarchartV2, error)
	GetAutospirometri(mcuID string) (models.BarchartV2, error)
	GetPendengaran(mcuID string) (models.BarchartV2, error)
	GetChartRenderNonLab(mcuID string) ([]models.McuTabModels, error)
}

type TabKelainanNonLabHandler struct {
	TabKelainanNonLabServices TabKelainanNonLabServices
}

func NewTabKelainanNonLabHandler(tknl TabKelainanNonLabServices) *TabKelainanNonLabHandler {
	return &TabKelainanNonLabHandler{
		TabKelainanNonLabServices: tknl,
	}
}

func (tknl *TabKelainanNonLabHandler) HandleShowTabKelainanNonLab(c echo.Context) error {
	id := c.Param("id")

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

	tablist, err := tknl.TabKelainanNonLabServices.GetChartRenderNonLab(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.TabKelainanNonLab(
		chartsxs,
		chartsmd,
		chartslg,
	)

	return utils.View(c, content)
}
