penguin/golifehk

@golifehk bot in Telegram

datasources/mtr/bus/QueryResult.go

raw · 4142 bytes

package bus

import (
    "fmt"
    "sort"
    "strings"

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

type QueryResult struct {
    Schedules *map[*BusStop] *BusStopBuses
    Lang string
    Error error
    Query *query.QueryObject
}

func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
    if b.PrevStop() != nil {
        utils.WriteMDv2Text( sb, (*b.PrevStop().Name)[ *lang ] )
        sb.WriteString( " \\> " )
    }

    sb.WriteString( "*" )
    utils.WriteMDv2Text( sb, (*b.Name)[ *lang ] )
    sb.WriteString( "*" )

    if b.NextStop() != nil {
        sb.WriteString( " \\> " )
        utils.WriteMDv2Text( sb, (*b.NextStop().Name)[ *lang ] )
    }

    sb.WriteString( "\n" )
}

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

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

    sb := strings.Builder{}

    if this.Schedules == nil {

        q := *this.Query

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

        if q.Key == "" {
            sort.Sort( query.ByKey( *q.Results ) )
            for _, entry := range *q.Results {
                busStop := any( entry ).( *BusStop )
                utils.WriteMDv2Text( &sb, busStop.RouteId )
                sb.WriteString( " " )
                writeShortRoute( &this.Lang, &sb, busStop )
            }
        } else if 1 == len( *q.SearchTerms ) {

            // Route listing
            st := map[string] *BusStop{}
            keys := [] string{}

            for _, entry := range *q.Results {

                busStop := any( entry ).( *BusStop )
                if _, ok := st[ busStop.Direction ]; ok {
                    continue
                }

                st[ busStop.Direction ] = busStop
                keys = append( keys, busStop.Direction )

                for st[ busStop.Direction ].PrevStop() != nil {
                    st[ busStop.Direction ] = st[ busStop.Direction ].PrevStop()
                }
            }

            sort.Strings( keys )

            for _, d := range keys {
                b := st[ d ]
                sb.WriteString( q.Key )

                if d == "O" {
                    sb.WriteString( "↑" )
                } else if d == "I" {
                    sb.WriteString( "↓" )
                } else {
                    sb.WriteString( "\\?" )
                }
                sb.WriteString( "\n " )

                for {
                    utils.WriteMDv2Text( &sb, (*b.Name)[ this.Lang ] )
                    b = b.NextStop()
                    if b == nil {
                        break
                    }

                    sb.WriteString( " \\> " )
                }
                sb.WriteString( "\n" )
            }

        } else {
            return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
        }
    } else {
        if 0 < len( *this.Schedules ) {

            busStops := [] *BusStop{}

            for b, _ := range *this.Schedules {
                busStops = append( busStops, b )
            }

            sort.Sort( ByRoute( busStops ) )

            for _, busStop := range busStops {
                buses := (*this.Schedules)[ busStop ]

                writeShortRoute( &this.Lang, &sb, busStop )
                for _, bus := range buses.Buses {
                    sb.WriteString( "  \\* " )
                    if bus.ETAText == "" {
                        utils.WriteMDv2Text( &sb, bus.ETDText )
                    } else {
                        utils.WriteMDv2Text( &sb, bus.ETAText )
                    }
                    sb.WriteString( "\n" )
                }
                sb.WriteString( "\n" )
            }
        } else {
            utils.WriteMDv2Text( &sb, "Schedules are empty...perhaps Out of Service Time?" )
        }
    }

    return sb.String(), nil
}