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"
	"go.uber.org/zap"
)

type TabKelainanFisikServices interface {
	GetKelainanFisikBMI(mcuID string) (models.Piechart, error)
	GetKelainanFisikHipertensi(mcuID string) (models.Piechart, error)
	GetKelainanFisikRefraksi(mcuID string) (models.Piechart, error)
	GetKelainanFisikPresentase(mcuID string) (models.Barchart, error)
	GetKelFisikAlkohol(mcuID string) (models.Piechart, error)
	GetKelFisikMerokok(mcuID string) (models.Piechart, error)
	GetKelFisikOlahraga(mcuID string) (models.Piechart, error)
	GetChartRenderFisik(mcuID string) ([]models.McuTabModels, error)
}

type TabKelainanFisikHandler struct {
	TabKelainanFisikServices TabKelainanFisikServices
}

func NewTabKelainanFisikHandler(tkf TabKelainanFisikServices) *TabKelainanFisikHandler {
	return &TabKelainanFisikHandler{
		TabKelainanFisikServices: tkf,
	}
}

func (tkf *TabKelainanFisikHandler) HandleShowTabKelainanFisik(c echo.Context) error {
	logger, _ := zap.NewProduction()
	logger.Info("Tab Kelainan Fisik")

	id := c.Param("id")

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

	tablist, err := tkf.TabKelainanFisikServices.GetChartRenderFisik(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.Tabkelainanfisik(
		chartsxs,
		chartsmd,
		chartslg,
	)

	return utils.View(c, content)
}
