package login

import (
	"context"
	"git.listensoft.net/tool/jspkit/common/lxrod"
	"git.listensoft.net/tool/jspkit/common/models"
	"git.listensoft.net/tool/jspkit/taxerr"
	"github.com/go-rod/rod"
	"strings"
)

func Login(ctx context.Context, info *models.CompanyInfo) (p *rod.Page, err error) {
	if info.Dlfs == "新版登录" || info.Dlfs == "代理登录" {
		lr := lxrod.Lxrod{}
		_, p, err = lr.NewLowBrowser2(ctx)
		err = CheckSessionAndLogin(ctx, p, GetBaseUri(info.Area), *info)
		if err != nil {
			if info.Dlfs == "新版登录" && strings.Contains(err.Error(), "卡顿") {
				err = qrLogin(p, info, &lr)
			}
			if err != nil {
				return
			}
		}
		if strings.Contains(p.MustInfo().URL, "loginb") { //todo ctx中传递参数决定是否
			C, _ := lxrod.PageToClient(p, GetBaseUri(info.Area))
			err = AfterLogin(C, p, &lr, *info, GetBaseUri(info.Area))
		}
	} else {
		err = taxerr.New("暂不支持的登录方式！")
	}
	return
}
