package utils

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
)

const (
	TBDelimiter = ","
)

// 分割分页的top/bottom
func SplitTopBottomData(param string) (base, num, extra int64, err error) {
	if param == "" {
		return 0.0, 0, 0.0, nil
	}

	mems := strings.Split(param, TBDelimiter)
	switch {
	case len(mems) == 0:
		return 0.0, 0, 0.0, errors.New("member is not enough")
	case len(mems) == 1:
		base, _ = strconv.ParseInt(mems[0], 10, 64)
	case len(mems) == 2:
		if base, err = strconv.ParseInt(mems[0], 10, 64); err != nil {
			return 0.0, 0, 0.0, err
		} else {
			num, _ = strconv.ParseInt(mems[1], 10, 64)
		}
	case len(mems) >= 3:
		if base, err = strconv.ParseInt(mems[0], 10, 64); err != nil {
			return 0.0, 0, 0.0, err
		} else {
			num, _ = strconv.ParseInt(mems[1], 10, 64)
			extra, _ = strconv.ParseInt(mems[2], 10, 64)
		}
	default:
		return 0.0, 0, 0.0, nil
	}

	return
}

// 组装分页的top/bottom
func JoinTopBottomData(base, num, extra int64) string {
	return fmt.Sprintf("%d%s%d%s%d", base, TBDelimiter, num, TBDelimiter, extra)
}
