package common

import (
	"git.listensoft.net/tool/jspkit/common/lxhttp"
	"git.listensoft.net/tool/jspkit/common/models"
	"git.listensoft.net/tool/jspkit/common/variable"
	"git.listensoft.net/tool/jspkit/logger"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

// CookiesToParams converts Cookies list to NetworkCookieParam list
func NetworkCookiesToParams(cookies []*proto.NetworkCookie) []*proto.NetworkCookieParam {
	list := []*proto.NetworkCookieParam{}
	for _, c := range cookies {
		list = append(list, &proto.NetworkCookieParam{
			Name:     c.Name,
			Value:    c.Value,
			Domain:   c.Domain,
			Path:     c.Path,
			Secure:   c.Secure,
			HTTPOnly: c.HTTPOnly,
			SameSite: c.SameSite,
			Expires:  c.Expires,
			Priority: c.Priority,
		})
	}
	return list
}

var sesstimeour = time.Duration(0.5 * float64(time.Second))

// SaveCookies 保存新的session, 远程
// selector, 元素选择器  selectorValue  元素值
func SaveCookies(key string, value []models.SessionInfo, refreshTime int, args ...string) error {
	//不返回任何错误！
	tsessionKey := ""
	if args != nil {
		tsessionKey = args[0]
	}
	var a interface{}
	lxhttp.PostJson(variable.SessionKeepURL+"/api/v1/session/save", map[string]interface{}{
		"sessionKey":  key,
		"tsessionKey": tsessionKey,
		"value":       value,
		"refreshTime": refreshTime,
	}, &a)
	logger.Info("SaveSessionKey:", key)
	return nil
}
