package utils

import (
	"errors"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var SessionTalentInfo = sessionTalentInfo{}

type sessionTalentInfo struct {
}

func (*sessionTalentInfo) GetTalentIdFromSession(r *ghttp.Request) (string, error) {
	t := r.GetHeader("Token")
	if t == "" {
		return "", errors.New("not found info by token")
	}

	infoMap := r.Session.Get(t)
	if infoMap == nil {
		return "", errors.New("info in session not found")
	}

	sInfoMap, ok := infoMap.(g.Map)
	if !ok {
		return "", errors.New("info in session is not a map")
	}

	sTid := sInfoMap["talentId"]
	if sTid == nil {
		return "", errors.New("session map not contains key talentId")
	}

	tid := sTid.(string)
	return tid, nil
}
