penguin/golifehk

@golifehk bot in Telegram

datasources/cjlookup/QueryResult.go

raw · 3127 bytes

package cjlookup

import (
	"fmt"
	"strconv"
	"strings"

	query "github.com/tgckpg/golifehk/query"
	utils "github.com/tgckpg/golifehk/utils"
)

type QueryResult struct {
	Lang  string
	Error error
	Query *query.QueryObject

	ResultType string
	isConsumed bool
}

func writeCCharInfo(sb *strings.Builder, cc *CChar) {
	sb.WriteString(getCUHARTSUrlForChar(cc.Face))
	sb.WriteString(" ")
	sb.WriteString(getCUHARTSUrlForPronounce(cc.JyutPing.Roman))
	if 0 < len(*cc.JiDukJam()) {
		sb.WriteString("\n")
		utils.WriteMDv2Text(sb, "異讀: ")
		sb.WriteString(strings.Join(getCUHARTSUrlsForPronounce(*cc.JiDukJam()), " "))
	}
	sb.WriteString("\n")
	utils.WriteMDv2Text(sb, "倉: ")
	utils.WriteMDv2Text(sb, cc.CangJie)
	sb.WriteString("\n")
	utils.WriteMDv2Text(sb, "同音字:")

	for i, cchar := range *cc.JyutPing.SortedTungJamZi() {
		sb.WriteString(" ")
		utils.WriteMDv2Text(sb, cchar.Face)
		utils.WriteMDv2Text(sb, strconv.Itoa(i+1))
	}
}

func (this QueryResult) DataType() string                  { return this.ResultType }
func (this QueryResult) UserLang() string                  { return this.Lang }
func (this QueryResult) Consumed() bool                    { return this.isConsumed }
func (this QueryResult) GetTableData() [][]query.TableCell { return nil }

func (this QueryResult) Message() (string, error) {

	this.ResultType = "PlainText"

	if this.Error != nil {
		return "", this.Error
	}

	if this.Query == nil {
		return "", nil
	}

	sb := strings.Builder{}

	q := *this.Query

	argv := *q.SearchTerms
	args := len(argv)

	if len(*q.Results) == 0 {
		terms := make([]string, args, len(*q.SearchTerms))
		for i, term := range *q.SearchTerms {
			terms[i] = term.Org
		}
		return "", fmt.Errorf("Not Found: \"%s\"", strings.Join(terms, "\", \""))
	}

	if utils.IsASCIIAlnum(argv[0].Value) {
		// Roman Search
		cjp := any((*q.Results)[0]).(*CJyutPing)
		if args == 1 {
			if len(*q.Results) == 1 {
				if len(*cjp.SortedTungJamZi()) == 1 {
					char0 := (*cjp.SortedTungJamZi())[0]
					writeCCharInfo(&sb, char0)
				} else {
					for i, cchar := range *cjp.SortedTungJamZi() {
						sb.WriteString(" ")
						utils.WriteMDv2Text(&sb, cchar.Face)
						utils.WriteMDv2Text(&sb, strconv.Itoa(i+1))
					}
				}
			} else {
				return "", fmt.Errorf("Multiple results found: %s", *q.Results)
			}
		} else if args == 2 {
			var err error
			x, err := strconv.Atoi(argv[1].Value)
			if err != nil {
				return "", fmt.Errorf("Invalid index: %s", argv[1].Org)
			}

			if len(*cjp.SortedTungJamZi()) == 1 {
				char0 := (*cjp.SortedTungJamZi())[0]
				writeCCharInfo(&sb, (*char0.JyutPing.SortedTungJamZi())[x-1])
			} else {
				writeCCharInfo(&sb, (*cjp.SortedTungJamZi())[x-1])
			}
		}
	} else {
		// Char Search
		cchar := any((*q.Results)[0]).(*CChar)
		if args == 1 {
			writeCCharInfo(&sb, cchar)
		} else if args == 2 {
			var err error
			x, err := strconv.Atoi(argv[1].Value)
			if err != nil {
				return "", fmt.Errorf("Invalid index: %s", argv[1].Org)
			}
			tjz := (*(cchar.JyutPing).SortedTungJamZi())[x-1]

			if tjz != nil {
				writeCCharInfo(&sb, tjz)
			}
		}
	}

	return sb.String(), nil
}