package dev_services

import (
	"cpone/db"
	"cpone/utils"
	"fmt"
	"strconv"
)

func NewServicesAutoComplete(uStore db.AppStore) *ServicesAutoComplete {

	return &ServicesAutoComplete{
		AutoCompleteStore: uStore,
	}
}

type ServicesAutoComplete struct {
	AutoCompleteStore db.AppStore
}

// example autocomplete
func (su *ServicesAutoComplete) GetAutoComplete(param string, pageOfParam string, limitOfStr string) (string, string, string, []AutoComplete, error) {
	// fmt.Println("ini param : ", param)

	dummyAutoComplete := []AutoComplete{
		{
			Name: "Google",
		},
		{
			Name: "Google 1",
		},
		{
			Name: "Google 2",
		},
		{
			Name: "Google 3",
		},
		{
			Name: "Google 4",
		},
		{
			Name: "Google 5",
		},
		{
			Name: "Google 6",
		},
		{
			Name: "Gojek",
		},
		{
			Name: "Mozilla Firefox",
		},
		{
			Name: "Apple",
		},
		{
			Name: "Microsoft",
		},
		{
			Name: "Facebook",
		},
		{
			Name: "Amazon",
		},
		{
			Name: "Twitter",
		},
		{
			Name: "Instagram",
		},
		{
			Name: "Snapchat",
		},
		{
			Name: "LinkedIn",
		},
		{
			Name: "Netflix",
		},
		{
			Name: "YouTube",
		},
		{
			Name: "Reddit",
		},
		{
			Name: "WhatsApp",
		},
		{
			Name: "Pinterest",
		},
		{
			Name: "TikTok",
		},
		{
			Name: "Spotify",
		},
		{
			Name: "Zoom",
		},
	}

	var textFound string
	var results []AutoComplete
	// var currentRes []AutoComplete
	// var oldResults []AutoComplete
	// var limit int = 3
	var count int = 0
	var foundCount int = 0
	var totalData int = 0
	var startIndex int = 0
	var endIndex int = 3

	if len(dummyAutoComplete) > 0 {
		totalData = len(dummyAutoComplete)
	}

	// pageOf, err := strconv.Atoi(pageOfParam)
	// if err != nil {
	// 	return "", "", "", nil, err
	// }

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

	// find data search
	if pageOfParam == "1" {
		count = 0
		// for _, item := range dummyAutoComplete {
		// 	if utils.Contains(item.Name, param) {
		// 		results = append(results, item)
		// 		count++
		// 		if count == limit {
		// 			break
		// 		}
		// 	}
		// }

		for _, item := range dummyAutoComplete {
			if utils.Contains(item.Name, param) {
				// Check if the item is already in results
				found := false
				for _, res := range results {
					if res.Name == item.Name {
						found = true
						break
					}
				}
				// If not found, append it
				if !found {
					results = append(results, item)
					count++
					if count == limit {
						break
					}
				}
			}
		}

	} else {
		// ketika klik load more
		// count = 0
		for _, item := range dummyAutoComplete {
			if utils.Contains(item.Name, param) {
				results = append(results, item)
				count++
				if count == limit {
					break
				}
			}
		}

		startIndex = 0
		endIndex = limit

		if startIndex >= len(dummyAutoComplete) {
			return "", "max", "", nil, nil
		}

		if endIndex > len(dummyAutoComplete) {
			endIndex = len(dummyAutoComplete)
		}

		for _, item := range dummyAutoComplete[startIndex:endIndex] {
			found := false
			for _, res := range results {
				if res.Name == item.Name {
					found = true
					break
				}
			}
			// If not found, append it
			if !found {
				results = append(results, item)
			}
		}
	}

	foundCount = len(results)
	totalCount := len(dummyAutoComplete)
	textFound = fmt.Sprintf("Ditemukan %d dari %d", foundCount, totalCount)

	return strconv.Itoa(totalData), strconv.Itoa(foundCount), textFound, results, nil
}
