package mqtt_client

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/AFASystems/presence/internal/pkg/model"
)

func incomingBeaconFilter(incoming model.Incoming_json) model.Incoming_json {
	out_json := incoming
	if incoming.Beacon_type == "hb_button" {
		raw_data := incoming.Data
		hb_button_prefix_str := fmt.Sprintf("02010612FF5900")
		if strings.HasPrefix(raw_data, hb_button_prefix_str) {
			out_json.Namespace = "ddddeeeeeeffff5544ff"
			counter_str := fmt.Sprintf("0x%s", raw_data[22:24])
			counter, _ := strconv.ParseInt(counter_str, 0, 64)
			out_json.HB_ButtonCounter = counter

			battery_str := fmt.Sprintf("0x%s%s", raw_data[20:22], raw_data[18:20])

			battery, _ := strconv.ParseInt(battery_str, 0, 64)
			out_json.HB_Battery = battery

			out_json.TX_power = fmt.Sprintf("0x%s", "4")

			out_json.Beacon_type = "hb_button"
			out_json.HB_ButtonMode = "presence_button"
		}
	}

	return out_json
}
