package dev_handlers

import (
	"fmt"
	"strconv"

	services "cpone/services/dev"
	"cpone/utils"
	"cpone/views/dev/xsample"

	"github.com/a-h/templ"

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

type XsampleService interface {
	GetListMenus() ([]services.MasterMenu, error)
}

func NewXsampleHandler(us XsampleService) *XsampleHandler {
	return &XsampleHandler{
		XsampleService: us,
	}
}

type XsampleHandler struct {
	XsampleService XsampleService
}

func (uh *XsampleHandler) Hello(c echo.Context) error {
	helo := xsample.ShowHelo("Hello World", xsample.HelloWorld("Hello World"), xsample.CssHelo(), xsample.JsHelo())
	return uh.View(c, helo)
}
func (uh *XsampleHandler) ShowCoba(c echo.Context) error {
	dataMenu, err := uh.XsampleService.GetListMenus()
	fmt.Println(dataMenu)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataMenu)
		return err
	}
	randomID := utils.GenerateRandomID("coba")
	fmt.Println(randomID)
	dataUser, err := services.GetUser()
	fmt.Println(dataUser)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUser)
		return err
	}
	// helo := xsample.ShowCoba("Hello World", xsample.Coba("Hello World"), xsample.CssCoba(), xsample.JsCoba(), dataMenu, dataUser)
	helo := xsample.Coba("Hello World")
	return uh.View(c, helo)
}
func (uh *XsampleHandler) ShowCounter(c echo.Context) error {
	typectr := c.QueryParam("type")

	counter := c.QueryParam("counter")
	if counter == "" {
		counter = "0"
	}
	ctr, err := strconv.Atoi(counter)
	if err != nil {
		fmt.Println("Error counter parse int")
		return err
	}

	if typectr == "min" {
		ctr = ctr - 1
	} else if typectr == "plus" {
		ctr = ctr + 1
	}
	helo := xsample.Page(ctr)
	return uh.View(c, helo)
}
func (uh *XsampleHandler) ChangeCounter(c echo.Context) error {

	typectr := c.QueryParam("type")

	counter := c.QueryParam("counter")
	ctr, err := strconv.Atoi(counter)
	if err != nil {
		fmt.Println("Error counter parse int")
		return err
	}

	if typectr == "min" {
		ctr = ctr - 1
	} else {
		ctr = ctr + 1
	}
	helo := xsample.Counts(ctr)
	return uh.View(c, helo)
}

func (uh *XsampleHandler) 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)
}
