package logic

import (
	MC "app.yhyue.com/moapp/jybase/common"
	"context"
	"fmt"
	IC "jyBXCore/rpc/init"
	"jyBXCore/rpc/util"

	"jyBXCore/rpc/internal/svc"
	"jyBXCore/rpc/type/bxcore"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchLimitLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchLimitLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLimitLogic {
	return &SearchLimitLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// SearchLimit 标讯搜索限制内容
func (l *SearchLimitLogic) SearchLimit(in *bxcore.SearchLimitReq) (*bxcore.SearchLimitResp, error) {
	if in.UserId == "" || !util.IsCanLogin(in.UserId) {
		return &bxcore.SearchLimitResp{
			Data: fmt.Sprintf("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">需要登录！"),
		}, nil
	}
	status := MC.If(IC.C.LimitSearchText.Flag, "打开", "关闭").(string)
	if in.SearchType == "update" {
		if in.TimeOut > 0 || in.TimeOut == -1 {
			IC.C.LimitSearchText.TimeOut = int(in.TimeOut)
		}
		if (in.Count > 0 || in.Count == -1 || in.Count == -2) && int(in.Count) != IC.C.LimitSearchText.Count && IC.C.LimitSearchText.Flag {
			IC.C.LimitSearchText.Count = int(in.Count)
			util.LimitSearchInit()
		}
		if in.Flag == -2 { //重置
			status = "重置"
			util.LimitSearchInit()
		} else if in.Flag == -1 && IC.C.LimitSearchText.Flag { //关闭
			status = "关闭"
			IC.C.LimitSearchText.Flag = false
			util.LimitSearchClear()
		} else if in.Flag == 1 && !IC.C.LimitSearchText.Flag { //打开
			status = "打开"
			IC.C.LimitSearchText.Flag = true
			util.LimitSearchInit()
		} else {
			if IC.C.LimitSearchText.Flag {
				status = "打开"
			} else {
				status = "关闭"
			}
		}
		//vip 大会员等用户 单独通道占比
		if in.Percentage > 0 && IC.C.LimitSearchText.Flag {
			IC.C.LimitSearchText.Percentage = int(in.Percentage)
		}
	}
	return &bxcore.SearchLimitResp{
		Data: fmt.Sprint("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">" + fmt.Sprintf(IC.C.LimitSearchText.Msg, status, IC.C.LimitSearchText.Count, IC.C.LimitSearchText.TimeOut)),
	}, nil
}
