package ai

import (
	"fmt"
	"regexp"
	"strings"

	"git.postblue.info/keygoller/utils"
)

// ReadFromStream is the main AI function, it reads from the stream of the keys
// and then answers if some conditions are met.
func ReadFromStream(inputChan chan utils.InputEvent) {
	var b strings.Builder
	for {
		data := <-inputChan
		b.WriteString(data.KeyString())

		switch b.String() {
		case b.String():
			if findPassword(b.String()) {
				fmt.Println("I LIKE YOUR PASSWORD")
				b.Reset()
			}
			if findLove(b.String()) {
				fmt.Println("I love you too...")
				b.Reset()
			}
		default:
			continue

		}
	}
}

func findPassword(input string) bool {
	r, _ := regexp.Compile("PASSWORD")

	if ok := r.MatchString(input); ok {
		return true
	}

	return false
}

func findLove(input string) bool {
	r, _ := regexp.Compile("ILOVEYOU")

	if ok := r.MatchString(input); ok {
		return true
	}

	return false
}
