package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// shorter declaration option
// type Animal struct {
// 	food, locomotion, sound string
// }

type Animal struct {
	food       string
	locomotion string
	noise      string
}

var cow Animal = Animal{"grass", "walk", "moo"}
var bird Animal = Animal{"worms", "fly", "peep"}
var snake Animal = Animal{"mice", "slither", "hiss"}

func (a Animal) Eat() {
	fmt.Println(a.food)
}

func (a Animal) Move() {
	fmt.Println(a.locomotion)
}

func (a Animal) Speak() {
	fmt.Println(a.noise)
}

func ParseUserInput(s string) []string {
	noNewLine := strings.Trim(s, "\n")
	return strings.Split(noNewLine, " ")

}

func ProcessCommand(cmd []string) {
	switch cmd[0] {
	case "cow":
		PerformAction(cow, cmd[1])
	case "bird":
		PerformAction(bird, cmd[1])
	case "snake":
		PerformAction(snake, cmd[1])
	default:
		fmt.Println("Invalid animal. Options {cow, bird, snake}")
	}
}

func PerformAction(v Animal, action string) {
	switch action {
	case "eat":
		v.Eat()
	case "move":
		v.Move()
	case "speak":
		v.Speak()
	default:
		fmt.Println("Invalid action. Options: {eat, move, speak}")
	}
}

func main() {
	fmt.Println("Welcome to Animal Farm! We have a \"cow\", \"bird\", and \"snake\".")
	fmt.Println("You can ask eat animal to \"eat\", \"move\", or \"speak\".")
	fmt.Println("Example: >cow move")

	inputReader := bufio.NewReader(os.Stdin)

	for {
		fmt.Printf(">")
		userInput, _ := inputReader.ReadString('\n')
		splitInputs := ParseUserInput(userInput)

		if len(splitInputs) > 2 {
			fmt.Println("Error user input contains more values than the expected \"<animal> <action>\"")
			continue
		}

		if len(splitInputs) < 2 {
			fmt.Println("Error user input contains less values than the expected \"<animal> <action>\"")
			continue
		}
		ProcessCommand(splitInputs)
	}
}
