package autonavi

import (
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"strings"
)

// GetCyclingPlan 获取骑手的骑行计划
func (a *API) GetCyclingPlan(origin, destination string) ([]string, int64, int64, error) {
	param := map[string]interface{}{
		"key":         a.key,
		"origin":      origin,
		"destination": destination,
		"show_fields": "polyline",
	}
	result, err := a.AccessAPI3("direction/electrobike", param)
	if err != nil {
		return nil, 0, 0, err
	}
	var data *CyclingPlan
	if err := utils.Map2StructByJson(result, &data, false); err != nil {
		return nil, 0, 0, err
	}

	if data.Status != "1" && data.Info != "ok" {
		return nil, 0, 0, fmt.Errorf(data.Infocode)
	}

	polyLineList := make([]string, 0, 0) // 坐标
	var distance int64 = 0               // 距离 4329
	var duration int64 = 0               // 时间 978
	for _, v := range data.Route.Paths {
		for _, v2 := range v.Steps {
			if v2.Polyline != "" {
				polyLine := strings.Split(v2.Polyline, ";")
				if polyLine != nil {
					polyLineList = append(polyLineList, polyLine...)
				}
			}
		}
		distance = utils.Str2Int64(v.Distance)
		duration = utils.Str2Int64(v.Duration) + 300
	}

	return polyLineList, distance, duration, nil
}
