package main

import (
	"bufio"
	"bytes"
	"log"
	"regexp"
	"strings"

	"github.com/asaskevich/govalidator"
)

func parseComplex(data []byte) []string {
	var domains []string

	// convert data to reader for line-by-line reading
	r := bytes.NewReader(data)

	// process combined files line-by-line
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		line := scanner.Text()

		// skip lines where the first non-whitespace character is '#' or '//'
		if regexp.MustCompile(`^(\s+)?(#|\/\/)`).MatchString(line) {
			continue
		}

		// skip lines with no characters and/or whitespace only
		if regexp.MustCompile(`^(\s+)?$`).MatchString(line) {
			continue
		}

		// split line by whitespace
		lineItems := strings.Fields(line)

		if len(lineItems) >= 2 {
			// the second item is the domain, check if its valid and add it
			if govalidator.IsDNSName(lineItems[1]) {
				domains = append(domains, lineItems[1])
			} else {
				log.Printf("[TRACE] Domain is not valid: %s\n", lineItems[0])
			}
		}
	}

	return domains
}
