package dev_handlers

import (
	"fmt"

	landingpage "cpone/views/dev/landingpage"

	"github.com/a-h/templ"

	services "cpone/services/dev"

	"github.com/labstack/echo/v4"
)

type LandingPageService interface {
	GetClientService() ([]services.ClientService, error)
	GetClientAdvantage() ([]services.AdvantageClient, error)
	GetPromotionList() ([]services.Promotion, error)
	GetFooterNavList() ([]services.FooterNav, error)
}

func NewLandingPageHandler(us LandingPageService) *LandingPageHandler {
	return &LandingPageHandler{
		LandingPageService: us,
	}
}

type LandingPageHandler struct {
	LandingPageService LandingPageService
}

func (uh *LandingPageHandler) ShowLandingPage(c echo.Context) error {
	udata, err := uh.LandingPageService.GetClientService()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	adData, err := uh.LandingPageService.GetClientAdvantage()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	lpData, err := uh.LandingPageService.GetPromotionList()
	if err != nil {
		// fmt.Println(err)
		return err
	}
	ftNav, err := uh.LandingPageService.GetFooterNavList()
	if err != nil {
		return err
	}
	fmt.Printf("%+v\n", udata)
	helo := landingpage.ShowLandingPage("Hello World",
		landingpage.MainLandingPage(landingpage.ListMedicalService(udata),
			landingpage.ListAdvantage(adData), landingpage.ListPromotion(lpData),
			landingpage.FooterSection(ftNav)),
		landingpage.CssLandingPage(),
		landingpage.JsLandingPage())
	return uh.View(c, helo)
}

func (uh *LandingPageHandler) View(c echo.Context, cmp templ.Component) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return cmp.Render(c.Request().Context(), c.Response().Writer)
}
