package dev_handlers

import (
	breadcrumadmin "cpone/component/breadcrumbadmin"
	navbarmenu "cpone/component/navbar"
	"cpone/component/pagination"
	sidebaruserprofile "cpone/component/sidebar_user_profile"
	"cpone/models"
	"cpone/services"
	"cpone/utils"
	dev_employeeanalytic "cpone/views/dev/employeeanalytic"
	"strconv"

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

type EmployeeAnalyticServices interface {
	GetEmployeeAnalyticBreadcrumb(title string) (models.BreadCrumbV1, error)
	DummyDataTest() ([]models.DashboardPic, error)
	ListingEmployeeAnalytic(search string, startdate string, enddate string, currentpage int, rowperpage int) ([]models.DashboardPic, int, error)
}

type EmployeeAnalyticHandler struct {
	EmployeeAnalyticServices EmployeeAnalyticServices
}

func NewEmployeeAnalyticHandler(ea EmployeeAnalyticServices) *EmployeeAnalyticHandler {
	return &EmployeeAnalyticHandler{
		EmployeeAnalyticServices: ea,
	}
}

func (ea *EmployeeAnalyticHandler) HandleShowEmployeeAnalyticScreen(c echo.Context) error {
	logger, _ := zap.NewProduction()
	title := "PT. Sadhana Abiyasa Sampoerna"

	listID := utils.GenerateRandomID("listid")
	paginationID := utils.GenerateRandomID("paginationid")

	user, err := services.GetUserLogin()
	if err != nil {
		defer logger.Sync()
		logger.Info("Error get user dev", zap.Any("error", err))
		return err
	}

	dataBreadcrumb, err := ea.EmployeeAnalyticServices.GetEmployeeAnalyticBreadcrumb(title)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error breadcrumb dev", zap.Any("error", err))
		return err
	}
	defer logger.Sync()

	navbaruser := navbarmenu.NavbarWithLogo(user)
	sidbaruser := sidebaruserprofile.Navbaruserprofile(user)
	breadcrumb := breadcrumadmin.MainBreadcrumbAdminV1(dataBreadcrumb)

	listingData, totalPage, err := ea.EmployeeAnalyticServices.ListingEmployeeAnalytic("", "", "", 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error get listing data", zap.Any("error", err), zap.Any("total", totalPage))
		return err
	}
	listingcomponent := dev_employeeanalytic.ListingData(listingData, listID)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		1,
		"/dev/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_employeeanalytic.BeforeRequestContent(),
		dev_employeeanalytic.AfterRequestContent(),
	)

	filtercomponent := dev_employeeanalytic.FilterListing(
		"/dev/dashboard_pic/filter",
		"input changed delay:500ms",
		"#searchID, #startdateID, #enddateID, #listID, #paginationID",
		"outerHTML",
		"#"+paginationID,
		dev_employeeanalytic.BeforeRequestContent(),
		dev_employeeanalytic.AfterRequestContent(),
	)

	content := dev_employeeanalytic.EmployeeAnalyticScreen(
		listID,
		paginationID,
		breadcrumb,
		filtercomponent,
		listingcomponent,
		paginationcomponent,
	)
	css := dev_employeeanalytic.CSSEmployeeAnalytic()
	js := dev_employeeanalytic.JSEmployeeAnalytic()

	view := dev_employeeanalytic.ShowEmployeeAnalytic(title, content, css, js, navbaruser, sidbaruser)
	return utils.View(c, view)
}

func (ea *EmployeeAnalyticHandler) HandlePagination(c echo.Context) error {

	pageparam := c.QueryParam("page")
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")
	search := c.QueryParam("search")
	startdate := c.QueryParam("startdate")
	enddate := c.QueryParam("enddate")

	var retVal []templ.Component
	logger, _ := zap.NewProduction()
	page, err := strconv.Atoi(pageparam)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error convert page param")
		return err
	}

	listdata, totalPage, err := ea.EmployeeAnalyticServices.ListingEmployeeAnalytic(search, startdate, enddate, page, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error convert page param")
		return err
	}
	listcomponent := dev_employeeanalytic.ListingData(listdata, listID)

	paginationcomponent := pagination.PaginationV3(
		totalPage,
		page,
		"/dev/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID,
		"outerHTML", "", "",
		dev_employeeanalytic.BeforeRequestContent(),
		dev_employeeanalytic.AfterRequestContent(),
	)

	retVal = append(retVal, listcomponent)
	retVal = append(retVal, paginationcomponent)
	return utils.ViewMulti(c, retVal)
}

func (ea *EmployeeAnalyticHandler) HandleFilter(c echo.Context) error {
	logger, _ := zap.NewProduction()

	search := c.QueryParam("search")
	startdate := c.QueryParam("startdate")
	enddate := c.QueryParam("enddate")
	listID := c.QueryParam("listID")
	paginationID := c.QueryParam("paginationID")

	dataList, totalPage, err := ea.EmployeeAnalyticServices.ListingEmployeeAnalytic(search, startdate, enddate, 1, 5)
	if err != nil {
		defer logger.Sync()
		logger.Info("Error Get Listing Data", zap.Any("error", err))
		return err
	}

	listcomponent := dev_employeeanalytic.ListingData(dataList, listID)
	paginationcomponent := pagination.PaginationV3(
		totalPage, 1,
		"/dev/dashboard_pic/changepage",
		paginationID,
		"#listID, #paginationID, #searchID, #startdateID, #enddateID",
		"#"+paginationID, "outerHTML", "", "",
		dev_employeeanalytic.BeforeRequestContent(),
		dev_employeeanalytic.AfterRequestContent(),
	)

	var retval []templ.Component
	retval = append(retval, listcomponent)
	retval = append(retval, paginationcomponent)
	return utils.ViewMulti(c, retval)
}

func (ea *EmployeeAnalyticHandler) HandleIntoDetail(c echo.Context) error {
	logger, _ := zap.NewProduction()
	id := c.Param("id")

	logger.Info("params", zap.Any("id", id))

	url := "/dev/detail/" + id
	c.Response().Header().Set("HX-Redirect", url)

	return nil
}
