package dev_handlers

import (
	breadcrumbadmin "cpone/component/breadcrumbadmin"
	navbarmenu "cpone/component/navbar"
	"cpone/component/pagination"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	"cpone/models"
	"cpone/services"
	"cpone/utils"
	dev_mdsamplestationview "cpone/views/dev/mdsamplestation"
	"fmt"
	"strconv"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

type MDSampleStationServices interface {
	GetMDSampleStationBreadcrumb() (models.BreadCrumbV1, error)
	GetListSampleStation(search string, currentPage int, rowPerPage int) ([]models.MDSampleStation, int, error)
}

func NewMDSampleStationHandler(ss MDSampleStationServices) *MDSampleStationHandler {
	return &MDSampleStationHandler{
		MDSampleStationServices: ss,
	}
}

type MDSampleStationHandler struct {
	MDSampleStationServices MDSampleStationServices
}

func (mdss *MDSampleStationHandler) HandleShowMDSampleStationScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()
	title := "Master Sample Station"

	tableID := utils.GenerateRandomID("tableID")
	paginationID := utils.GenerateRandomID("paginationID")
	dialogAddID := utils.GenerateRandomID("dialogAddID")
	// dialogEditID := utils.GenerateRandomID("dialogEditID")
	// dialogEditBodyID := utils.GenerateRandomID("dialogEditBodyID")
	// dialogDeleteID := utils.GenerateRandomID("dialogDeleteID")
	// dialogDeleteBodyID := utils.GenerateRandomID("dialogDeleteBodyID")

	dataMenu, err := services.GetMenu()
	if err != nil {
		return err
	}

	dataUser, err := services.GetUserLogin()
	if err != nil {
		return err
	}
	defer logger.Sync()
	logger.Info("ERROR LOAD DEV", zap.Any("data", dataUser))

	dataBreadcrumb, err := mdss.MDSampleStationServices.GetMDSampleStationBreadcrumb()
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR BREADCRUMB DEV", zap.Any("error", err))
		return err
	}
	navbarmenuComp := navbarmenu.NavbarMenu(dataMenu)
	navbaruserComp := navbarmenu.Navbar(dataUser)
	breadcrumbComp := breadcrumbadmin.MainBreadcrumbAdminV1(dataBreadcrumb)
	sidebaruserprofilceComp := sidebaruserprofile.Navbaruserprofile(dataUser)

	// table content
	dataTable, totalPage, err := mdss.MDSampleStationServices.GetListSampleStation("", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET LIST SAMPLE STATION", zap.Any("error", err))
		return err
	}
	fmt.Println("data sample station", dataTable)

	tableComponent := dev_mdsamplestationview.TableSampleStation(dataTable, tableID)

	// pagination table
	paginationTable := pagination.PaginationV2(
		totalPage,
		1,
		"/dev/md/samplestation/changepage",
		paginationID,
		"#tableID, #paginationID",
		"#"+paginationID,
		"outerHTML", "", "",
	)

	content := dev_mdsamplestationview.ContentMDSampleStation(
		tableID,
		dialogAddID,
		paginationID,
		breadcrumbComp,
		tableComponent,
		paginationTable,
	)
	css := dev_mdsamplestationview.CSSMDSampleStation()
	js := dev_mdsamplestationview.JSMDSampleStation()

	view := dev_mdsamplestationview.ShowMDSampleStation(title, content, css, js, navbarmenuComp, navbaruserComp, sidebaruserprofilceComp)
	return utils.View(c, view)
}

func (mdss *MDSampleStationHandler) HandleChangePageSampleStation(c echo.Context) error {
	pageparam := c.QueryParam("page")
	tableID := c.QueryParam("tableID")
	paginationID := c.QueryParam("paginationID")

	var retval []templ.Component
	logger, _ := zap.NewProduction()
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR CONVERT PAGE PARAM", zap.Any("page", page), zap.Any("error", err))
		return err
	}

	dataSS, totalPage, err := mdss.MDSampleStationServices.GetListSampleStation("", page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("ERROR GET DATA", zap.Any("error", err))
		return err
	}

	tableComp := dev_mdsamplestationview.TableSampleStation(dataSS, tableID)
	paginationComp := pagination.PaginationV2(
		totalPage,
		page,
		"/dev/md/samplestation/changepage",
		paginationID,
		"#tableID, #paginationID",
		"#"+paginationID,
		"outerHTML", "", "")

	retval = append(retval, tableComp)
	retval = append(retval, paginationComp)
	return utils.ViewMulti(c, retval)
}
