// parseNameStatement parses ResourceD name related query statement and turns it into postgres statement.
func parseNameStatement(statement, operator string) string {
	parts := strings.Split(statement, operator)

	name := parts[len(parts)-1]
	name = strings.TrimSpace(name)
	name = libstring.StripChars(name, `"'`)

	return fmt.Sprintf("name %v '%v'", operator, name)
}
// parseStatement parses ResourceD statement and turns it into postgres statement.
func parseStatement(statement string) string {
	statement = strings.TrimSpace(statement)

	// Querying tags.
	// There can only be 1 operator for tags: "="
	if strings.HasPrefix(statement, "Tags") || strings.HasPrefix(statement, "tags") {
		operator := ""

		if strings.Contains(statement, "=") {
			operator = "="
		}

		if operator != "" {
			parts := strings.Split(statement, operator)

			// Remove tags prefix
			parts[0] = strings.Replace(parts[0], "tags.", "", -1)

			pgJsonPath := strings.Replace(parts[0], ".", ",", -1)
			pgJsonPath = strings.TrimSpace(pgJsonPath)

			value := parts[len(parts)-1]
			value = strings.TrimSpace(value)
			value = libstring.StripChars(value, `"'`)

			return fmt.Sprintf("tags #>> '{%v}' %v '%v'", pgJsonPath, operator, value)
		}
	}

	// Querying name.
	// Operators:
	// "="   : Exact match.
	// "!~*" : Does not match regular expression, case insensitive.
	// "!~"  : Does not match regular expression, case sensitive.
	// "~*"  : Matches regular expression, case insensitive.
	// "~^"  : Starts with, case sensitive.
	// "~"   : Matches regular expression, case sensitive.
	if strings.HasPrefix(statement, "Name") || strings.HasPrefix(statement, "name") {
		if strings.Contains(statement, "=") {
			return parseNameStatement(statement, "=")

		} else if strings.Contains(statement, "!~*") {
			return parseNameStatement(statement, "!~*")

		} else if strings.Contains(statement, "!~") {
			return parseNameStatement(statement, "!~")

		} else if strings.Contains(statement, "~*") {
			return parseNameStatement(statement, "~*")

		} else if strings.Contains(statement, "~^") {
			parts := strings.Split(statement, "~^")

			name := parts[len(parts)-1]
			name = strings.TrimSpace(name)
			name = libstring.StripChars(name, `"'`)

			return `name LIKE '` + name + `%'`

		} else if strings.Contains(statement, "~") {
			return parseNameStatement(statement, "~")
		}
	}

	// Querying data.
	// Operators: >=, <=, =, <, >
	// Expected output: data #>> '{/free,Swap,Free}' = '0'
	if strings.HasPrefix(statement, "/") {
		operator := ""

		if strings.Contains(statement, ">=") {
			operator = ">="
		} else if strings.Contains(statement, "<=") {
			operator = "<="
		} else if strings.Contains(statement, "=") {
			operator = "="
		} else if strings.Contains(statement, "<") {
			operator = ">"
		} else if strings.Contains(statement, ">") {
			operator = ">"
		}

		if operator != "" {
			parts := strings.Split(statement, operator)

			pgJsonPath := strings.Replace(parts[0], ".", ",", -1)
			pgJsonPath = strings.TrimSpace(pgJsonPath)

			value := parts[len(parts)-1]
			value = strings.TrimSpace(value)
			value = libstring.StripChars(value, `"'`)

			return fmt.Sprintf("data #>> '{%v}' %v '%v'", pgJsonPath, operator, value)
		}
	}

	return ""
}