package wol

import (
	"fmt"
	"net"
	"regexp"
	"strings"
	"time"
)

var macRegex = regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$`)

func ParseMAC(s string) ([6]byte, error) {
	s = strings.ReplaceAll(s, "-", ":")
	s = strings.ToLower(s)
	if !macRegex.MatchString(s) {
		return [6]byte{}, fmt.Errorf("invalid MAC address: %s", s)
	}
	parts := strings.Split(s, ":")
	var mac [6]byte
	for i := 0; i < 6; i++ {
		var b int
		if _, err := fmt.Sscanf(parts[i], "%x", &b); err != nil {
			return [6]byte{}, fmt.Errorf("invalid MAC address: %s", s)
		}
		mac[i] = byte(b)
	}
	return mac, nil
}

func FormatMAC(mac [6]byte) string {
	return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
}

func BuildMagicPacket(mac [6]byte) []byte {
	packet := make([]byte, 6+16*6)
	for i := 0; i < 6; i++ {
		packet[i] = 0xFF
	}
	for i := 0; i < 16; i++ {
		offset := 6 + i*6
		copy(packet[offset:offset+6], mac[:])
	}
	return packet
}

func Send(addr string, mac [6]byte, broadcastAddr string) error {
	packet := BuildMagicPacket(mac)

	udpAddr := &net.UDPAddr{
		IP:   net.IPv4bcast,
		Port: 9,
	}
	if broadcastAddr != "" {
		udpAddr.IP = net.ParseIP(broadcastAddr)
		if udpAddr.IP == nil {
			return fmt.Errorf("invalid broadcast address: %s", broadcastAddr)
		}
	}

	conn, err := net.DialUDP("udp4", nil, udpAddr)
	if err != nil {
		return fmt.Errorf("creating UDP connection: %w", err)
	}
	defer conn.Close()

	if err := conn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
		return fmt.Errorf("setting broadcast mode: %w", err)
	}

	n, err := conn.Write(packet)
	if err != nil {
		return fmt.Errorf("sending magic packet: %w", err)
	}
	if n != len(packet) {
		return fmt.Errorf("incomplete write: sent %d/%d bytes", n, len(packet))
	}
	return nil
}
