package main

import (
	"fmt"
	"strings"
)

func parseList(s string) []string {
	if !isList(&_unknown{s: s}) {
		panic(fmt.Errorf("list: %s (%s)", errListExpected, s))
	}
	list, bracketCnt, start, resList := strings.Split(s, " "), 0, 1, []string{}
	for i := 1; i < len(list)-1; i++ {
		if list[i] == "[" {
			bracketCnt++
		}
		if list[i] == "]" {
			bracketCnt--
		}
		if bracketCnt == 0 {
			resList = append(resList, strings.Join(list[start:i+1], " "))
			start = i + 1
		}
	}
	return resList
}

func makeList(list []string) string {
	return strings.Join(append(append([]string{"["}, list...), "]"), " ")
}

func indexOfList(list []string, index int) string {
	if index < 0 || index >= len(list) {
		panic(fmt.Errorf("list: %s (%d / %d)", errIndexOutOfBound, index, len(list)))
	}
	return list[index]
}

func rangeOfList(list []string, index1, index2 int) string {
	if index1 < 0 || index1 > len(list) || index2 < 0 || index2 > len(list) {
		panic(fmt.Errorf("list: %s (%d : %d / %d)", errIndexOutOfBound, index1, index2, len(list)))
	}
	if index1 > index2 {
		panic(fmt.Errorf("list: %s (%d : %d)", errIllegalRange, index1, index2))
	}
	return makeList(list[index1:index2])
}

func spliceList(list []string, index int, cnt int, val ...string) string {
	if index < 0 || index > len(list) {
		panic(fmt.Errorf("list: %s (%d / %d)", errIndexOutOfBound, index, len(list)))
	}
	if index+cnt > len(list) {
		cnt = len(list) - index
	}
	return makeList(append(append(list[:index], val...), list[index+cnt:]...))
}

func opWord(val1, val2 value) value {
	if !isWord(val1) || isList(val2) {
		panic(fmt.Errorf("word: %s (%s, %s)", errWordExpceted, val1.String(), val2.String()))
	}
	return &_unknown{s: `"` + toWord(val1) + val2.String()}
}

func opSentence(val1, val2 value) value {
	list1, list2 := []string{val1.String()}, []string{val2.String()}
	if isList(val1) {
		list1 = toList(val1)
	}
	if isList(val2) {
		list2 = toList(val2)
	}
	return &_unknown{s: makeList(append(list1, list2...))}
}

func opList(val1, val2 value) value {
	return &_unknown{s: makeList([]string{val1.String(), val2.String()})}
}

func opJoin(val1, val2 value) value {
	if !isList(val1) {
		panic(fmt.Errorf("join: %s (%s)", errListExpected, val1.String()))
	}
	return &_unknown{s: makeList(append(toList(val1), val2.String()))}
}

func opFirst(val1 value) value {
	if !isWord(val1) && !isList(val1) {
		panic(fmt.Errorf("first: %s (%s)", errWordOrListExpected, val1.String()))
	}
	if isEmpty(val1) {
		panic(fmt.Errorf("first: %s (%s)", errEmptyWordOrList, val1.String()))
	}
	if isWord(val1) {
		s := escapeWord(toWord(val1))
		return &_unknown{s: `"` + s[:1]}
	}
	list := toList(val1)
	s := list[0]
	if !isList(&_unknown{s: s}) {
		s = `"` + s
	}
	return &_unknown{s: s}
}

func opLast(val1 value) value {
	if !isWord(val1) && !isList(val1) {
		panic(fmt.Errorf("last: %s (%s)", errWordOrListExpected, val1.String()))
	}
	if isEmpty(val1) {
		panic(fmt.Errorf("last: %s (%s)", errEmptyWordOrList, val1.String()))
	}
	if isWord(val1) {
		s := escapeWord(toWord(val1))
		return &_unknown{s: `"` + s[len(s)-1:]}
	}
	list := toList(val1)
	s := list[len(list)-1]
	if !isList(&_unknown{s: s}) {
		s = `"` + s
	}
	return &_unknown{s: s}
}

func opButFirst(val1 value) value {
	if !isWord(val1) && !isList(val1) {
		panic(fmt.Errorf("butfirst: %s (%s)", errWordOrListExpected, val1.String()))
	}
	if isEmpty(val1) {
		panic(fmt.Errorf("butfirst: %s (%s)", errEmptyWordOrList, val1.String()))
	}
	if isWord(val1) {
		s := escapeWord(toWord(val1))
		return &_unknown{s: `"` + s[1:]}
	}
	list := toList(val1)
	return &_unknown{s: makeList(list[1:])}
}

func opButLast(val1 value) value {
	if !isWord(val1) && !isList(val1) {
		panic(fmt.Errorf("butlast: %s (%s)", errWordOrListExpected, val1.String()))
	}
	if isEmpty(val1) {
		panic(fmt.Errorf("butlast: %s (%s)", errEmptyWordOrList, val1.String()))
	}
	if isWord(val1) {
		s := escapeWord(toWord(val1))
		return &_unknown{s: `"` + s[:len(s)-1]}
	}
	list := toList(val1)
	return &_unknown{s: makeList(list[:len(list)-1])}
}
