package handlers

import (
	"fmt"

	"github.com/a-h/templ"
	"github.com/emarifer/go-templ-project-structure/models"
	"github.com/emarifer/go-templ-project-structure/services"
	mastermenuusergroup "github.com/emarifer/go-templ-project-structure/views/mastermenuusergroup"
	"github.com/labstack/echo/v4"
)

type MasterMenuUserGroupService interface {
	GetMasterMenus() ([]services.MasterMenu, error)
	GetUserGroup() ([]models.UserGroup, error)
	GetUserGroupByID(id string) (models.UserGroup, error)
}

func NewMasterMenuUserGroupHandler(us MasterMenuUserGroupService) *MasterMenuUserGroupHandler {
	return &MasterMenuUserGroupHandler{
		MasterMenuUserGroupService: us,
	}
}

type MasterMenuUserGroupHandler struct {
	MasterMenuUserGroupService MasterMenuUserGroupService
}

func (lh *MasterMenuUserGroupHandler) HandlerShowMasterMenuUserGroup(c echo.Context) error {
	dataMenu, err := lh.MasterMenuUserGroupService.GetMasterMenus()
	fmt.Println(dataMenu)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataMenu)
		return err
	}
	dataUser, err := services.GetUser()
	fmt.Println(dataUser)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUser)
		return err
	}
	dataUserGroup, err := lh.MasterMenuUserGroupService.GetUserGroup()
	fmt.Println(dataUserGroup)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUserGroup)
		return err
	}

	si := mastermenuusergroup.ShowMasterMenuUserGroup(
		"Master Menu",
		mastermenuusergroup.ContentMasterMenuUserGroup(dataUserGroup),
		mastermenuusergroup.CssMasterMenuUserGroup(),
		mastermenuusergroup.JsMasterMenuUserGroup(),
		dataMenu,
		dataUser,
	)

	return lh.View(c, si)
}

func (lh *MasterMenuUserGroupHandler) ChangeFormEdit(c echo.Context) error {
	id := c.QueryParam("id")
	dataUserGroup, err := lh.MasterMenuUserGroupService.GetUserGroupByID(id)
	fmt.Println(dataUserGroup)
	fmt.Println(err)
	if err != nil {
		fmt.Println(dataUserGroup)
		return err
	}
	vw := mastermenuusergroup.DialogEditForm(dataUserGroup.UserGroupKode, dataUserGroup.UserGroupName)

	return lh.View(c, vw)
}

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