package logic

import (
	"context"
	"encoding/json"
	"errors"
	"fusenapi/model/gmodel"
	"fusenapi/server/product/internal/svc"
	"fusenapi/server/product/internal/types"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/encryption_decryption"
	"fusenapi/utils/id_generator"
	"fusenapi/utils/ip"
	"net/http"
	"time"

	"gorm.io/gorm"

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

type DesignGatherLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
	ipAddr string
}

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

// 处理进入前逻辑w,r
func (l *DesignGatherLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
	//获取ip地址
	ipAddr, err := ip.GetClientIP(r)
	if err != nil {
		logx.Error(err)
		return
		// return resp.SetStatusWithMessage(basic.CodeServiceErr, "client ip is not available")
	}
	l.ipAddr = ipAddr
}

func (l *DesignGatherLogic) DesignGather(req *types.DesignGatherReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if l.ipAddr == "" {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "client ip is not available")
	}

	encryptWebsetting, err := l.svcCtx.AllModels.FsWebSet.FindValueByKey(l.ctx, "is_encrypt")
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "web setting is_encrypt is not exists")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get web setting")
	}
	var postInfo map[string]interface{}
	//不加密
	if encryptWebsetting.Value == nil || *encryptWebsetting.Value == "0" {
		if err = json.Unmarshal([]byte(req.Data), &postInfo); err != nil {
			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data,format may be invalid")
		}
	} else { //加密的
		//解密数据
		desData, err := encryption_decryption.CBCDecrypt(req.Data)
		if err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "failed to decryption data")
		}
		if err = json.Unmarshal([]byte(desData), &postInfo); err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data,format may be invalid")
		}
	}

	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "client ip is not available")
	}
	postInfo["client_ip"] = l.ipAddr
	var (
		optionalId int64
		sizeId     int64
		productId  int64
		templateId int64
		materialId int64
		cover      string
		info       string
		clientNo   string
	)
	//校验数据
	if _, ok := postInfo["optional_id"].(float64); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param optional_id")
	}
	optionalId = int64(postInfo["optional_id"].(float64))
	if _, ok := postInfo["size_id"].(float64); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param size_id")
	}
	sizeId = int64(postInfo["size_id"].(float64))
	if _, ok := postInfo["product_id"].(float64); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param product_id")
	}
	productId = int64(postInfo["product_id"].(float64))
	if _, ok := postInfo["template_id"].(float64); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param template_id")
	}
	templateId = int64(postInfo["template_id"].(float64))
	if _, ok := postInfo["material_id"].(float64); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param material_id")
	}
	materialId = int64(postInfo["material_id"].(float64))
	if _, ok := postInfo["client_no"].(string); !ok {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param client_no")
	}
	clientNo = postInfo["client_no"].(string)
	if postInfo["info"] == nil {
		info = ""
	} else {
		jsonInfoBytes, _ := json.Marshal(postInfo["info"])
		info = string(jsonInfoBytes)
	}
	if postInfo["cover"] != nil {
		cover = postInfo["cover"].(string)
	}
	//保存数据
	designSn, err := id_generator.GenSnowFlakeId()
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate design sn")
	}
	now := time.Now().Unix()
	err = l.svcCtx.AllModels.FsProductDesignGather.Create(l.ctx, &gmodel.FsProductDesignGather{
		Sn:         &designSn,
		UserId:     &userinfo.UserId,
		ProductId:  &productId,
		TemplateId: &templateId,
		MaterialId: &materialId,
		SizeId:     &sizeId,
		OptionalId: &optionalId,
		Cover:      &cover,
		Info:       &info,
		Utime:      &now,
		ClientIp:   &l.ipAddr,
		ClientNo:   &clientNo,
	})
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to save data")
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success", types.DesignGatherRsp{
		Sn: designSn,
	})
}