package dev_handlers

import (
	"cpone/component/customtextfieldautocomplete"
	"cpone/models"
	"cpone/utils"
	"fmt"
	"net/url"
	"strconv"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
)

type AutoCompleteService interface {
	GetAutoComplete(param string, pageOf string, limit string) (string, string, string, []models.AutoComplete, error)
}

func NewAutoCompleteHandler(us AutoCompleteService) *AutoCompleteHandler {
	return &AutoCompleteHandler{
		AutoCompleteService: us,
	}
}

type AutoCompleteHandler struct {
	AutoCompleteService AutoCompleteService
}

// initial
func (lh *AutoCompleteHandler) HandlerListAutoComplete(c echo.Context) error {
	// param := c.Param("p")
	param := c.QueryParam("searchAutocomplete")
	pageOf := "1"
	limit := "3"

	pageOfBefore, _ := strconv.Atoi(c.Param("pageOf"))
	limitBefore, _ := strconv.Atoi(c.Param("limit"))

	if pageOfBefore > 1 {
		pageCurrent := pageOfBefore + 1
		pageOf = strconv.Itoa(pageCurrent)

		limitCurrent := limitBefore
		limit = strconv.Itoa(limitCurrent)
	}

	// urlCustomAutoComplete := "/client/autoCompleteLoadMore/"+ strconv.Itoa(pageOf)
	var si templ.Component
	if param != "" {
		totalData, foundCount, textFound, dataAutoComplete, err := lh.AutoCompleteService.GetAutoComplete(param, pageOf, limit)
		fmt.Println(param)
		// fmt.Println(err)
		if err != nil {
			fmt.Println(dataAutoComplete)
			return err
		}

		foundCountInt, err := strconv.Atoi(foundCount)
		if err != nil {
			return err
		}

		totalDataInt, err := strconv.Atoi(totalData)
		if err != nil {
			return err
		}

		// si := customtextfieldautocomplete.ListCustomAutoComplete(dataAutoComplete)

		si = customtextfieldautocomplete.ListCustomAutoComplete(limit, param, totalDataInt, foundCountInt, pageOf, textFound, dataAutoComplete)
	} else {
		si = customtextfieldautocomplete.ListCustomAutoCompleteHide()
	}

	return utils.View(c, si)
}

// after selected
func (lh *AutoCompleteHandler) HandlerListAutoCompleteAfterSelected(c echo.Context) error {
	// param := c.QueryParam("searchAutocomplete")

	setvalue := c.Param("setvalue")
	setvalue, err := url.PathUnescape(setvalue)
	if err != nil {
		return err
	}

	si := customtextfieldautocomplete.MainCustomAutoComplete(setvalue)

	return utils.View(c, si)
}

// load more autocomplete
func (lh *AutoCompleteHandler) HandlerListAutoCompleteLoadMore(c echo.Context) error {
	// param := c.QueryParam("searchAutocomplete")

	var totalPerPage int = 3
	paramSearch := c.Param("searchParam")
	searchParam, err := url.PathUnescape(paramSearch)
	if err != nil {
		return err
	}

	pageOfStr := c.Param("pageOf")
	pageOfInt, err := strconv.Atoi(pageOfStr)
	if err != nil {
		return err
	}

	pageOfInt++
	pageOf := strconv.Itoa(pageOfInt)

	limitOfStr := c.Param("limit")
	limitOfInt, err := strconv.Atoi(limitOfStr)
	if err != nil {
		return err
	}

	limitOfX := limitOfInt + totalPerPage
	limitOf := strconv.Itoa(limitOfX)

	var si templ.Component

	totalData, foundCount, textFound, dataAutoComplete, err := lh.AutoCompleteService.GetAutoComplete(searchParam, pageOf, limitOf)

	// fmt.Println(param)
	// fmt.Println(err)

	if err != nil {
		fmt.Println(dataAutoComplete)
		return err
	}

	foundCountInt, err := strconv.Atoi(foundCount)
	if err != nil {
		return err
	}

	totalDataInt, err := strconv.Atoi(totalData)
	if err != nil {
		return err
	}

	// si := customtextfieldautocomplete.ListCustomAutoComplete(dataAutoComplete)
	si = customtextfieldautocomplete.ListCustomAutoComplete(limitOf, searchParam, totalDataInt, foundCountInt, pageOf, textFound, dataAutoComplete)

	// if param != "" {
	// 	textFound, dataAutoComplete, err := lh.AutoCompleteService.GetAutoComplete(param, pageOf)
	// 	fmt.Println(param)
	// 	// fmt.Println(err)
	// 	if err != nil {
	// 		fmt.Println(dataAutoComplete)
	// 		return err
	// 	}

	// 	// si := customtextfieldautocomplete.ListCustomAutoComplete(dataAutoComplete)
	// 	si = customtextfieldautocomplete.ListCustomAutoComplete(pageOf, textFound, dataAutoComplete)

	// } else {
	// 	si = customtextfieldautocomplete.ListCustomAutoCompleteHide()
	// }

	return utils.View(c, si)
}

func (lh *AutoCompleteHandler) HandlerAutoCompleteExample(c echo.Context) error {
	// dataAutoComplete, err := lh.AutoCompleteService.GetAutoCompleteExample()
	// fmt.Println(dataAutoComplete)
	// fmt.Println(err)
	// if err != nil {
	// 	fmt.Println(dataAutoComplete)
	// 	return err
	// }
	// fmt.Printf("%+v\n", dataMenu)
	// si := mastermenu.ShowMasterMenu("Master Menu",
	// 	mastermenu.MainMasterMenu(dataMenu),
	// 	mastermenu.CssMasterMenu(),
	// 	mastermenu.JsMasterMenu(),
	// )

	// si := mastermenuusergroup.ShowAutoComplete(
	// 	"Master Menu",
	// 	mastermenuusergroup.MainAutoComplete(dataMenu),
	// 	mastermenuusergroup.CssAutoComplete(),
	// 	mastermenuusergroup.JsAutoComplete(),
	// )

	si := customtextfieldautocomplete.ShowCustomAutoComplete("",
		customtextfieldautocomplete.MainCustomAutoComplete(""),
		customtextfieldautocomplete.CssCustomAutoComplete(),
		customtextfieldautocomplete.JsCustomAutoComplete(),
	)

	return utils.View(c, si)
}
