package logic

import (
	"context"
	"encoding/json"
	"errors"
	"fusenapi/constants"
	"fusenapi/model/gmodel"
	"fusenapi/server/shopping-cart/internal/svc"
	"fusenapi/server/shopping-cart/internal/types"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/file"
	"fusenapi/utils/hash"
	"fusenapi/utils/s3url_to_s3id"
	"gorm.io/gorm"
	"strings"
	"time"

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

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

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

// 处理进入前逻辑w,r
// func (l *AddToCartLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }

func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if !userinfo.IsUser() {
		return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
	}
	//校验参数
	if err := l.AddToCartParamVerify(req); err != nil {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, err.Error())
	}
	//查询该用户购物车数量
	cartCount, err := l.svcCtx.AllModels.FsShoppingCart.CountUserCart(l.ctx, userinfo.UserId)
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get the count of your shopping cart")
	}
	if cartCount >= 100 {
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "sorry,the count of your carts can`t greater than 100")
	}
	logoResourceId := s3url_to_s3id.GetS3ResourceIdFormUrl(req.Logo)
	//获取用户素材信息
	userMaterialInfo, err := l.svcCtx.AllModels.FsUserMaterial.FindOneByLogoResourceId(l.ctx, logoResourceId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusAddMessage(basic.CodeDbRecordNotFoundErr, "the user logo material info is not found")
		}
		logx.Error(err)
		return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get user logo material info")
	}
	var logoMaterialMetadata interface{}
	if userMaterialInfo.Metadata != nil && len(*userMaterialInfo.Metadata) != 0 {
		_ = json.Unmarshal(*userMaterialInfo.Metadata, &logoMaterialMetadata)
	}
	//不是传路径则就是传base64
	if !strings.Contains(req.RenderImage, "https://") {
		//上传base64文件
		var upload = file.Upload{
			Ctx:        l.ctx,
			MysqlConn:  l.svcCtx.MysqlConn,
			AwsSession: l.svcCtx.AwsSession,
		}
		uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
			Source:       "webGl render image",
			FileHash:     hash.JsonHashKey(req.RenderImage),
			FileData:     req.RenderImage,
			Metadata:     "",
			UploadBucket: 1,
			ApiType:      2,
			UserId:       userinfo.UserId,
			GuestId:      userinfo.GuestId,
			FileByte:     nil,
		})
		if err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload webGl render image")
		}
		req.RenderImage = uploadRes.ResourceUrl
	}
	//获取产品是否存在
	productInfo, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
		}
		logx.Error("获取产品信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
	}
	var (
		templateJson interface{} //模板表的记录中的json设计信息
		templateTag  string      //模板表的模板标签
		fittingJson  interface{} //配件的json设计信息
		fittingName  string      //配件名
		lightJson    interface{} //灯光设计数据
		lightName    string      //灯光名字
		modelJson    interface{} //模型json
	)
	//有模板
	if req.TemplateId > 0 {
		templateInfo, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateId)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template is not exists")
			}
			logx.Error("获取模板信息错误:", err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
		}
		if *templateInfo.IsDel == 1 {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template is deleted")
		}
		if *templateInfo.Status != 1 {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s status is unNormal")
		}
		if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s design info is empty")
		}
		if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &templateJson); err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse template design info")
		}
		templateTag = *templateInfo.TemplateTag
	}
	//有配件
	if req.FittingId > 0 {
		fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the fitting is not exists")
			}
			logx.Error("获取配件信息错误:", err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info")
		}
		if *fittingInfo.Status != 1 {
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s status is unNormal")
		}
		if fittingInfo.ModelInfo == nil || *fittingInfo.ModelInfo == "" {
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s design info is empty")
		}
		if err = json.Unmarshal([]byte(*fittingInfo.ModelInfo), &fittingJson); err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse fitting design info")
		}
		fittingName = *fittingInfo.Title
	}
	//获取尺寸信息
	sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the size is not exists")
		}
		logx.Error("获取尺寸信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size info")
	}
	//状态
	if *sizeInfo.Status != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "size info status is not normal")
	}
	//获取模型信息
	modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(l.ctx, sizeInfo.Id, constants.TAG_MODEL)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template`s model is not exists")
		}
		logx.Error("获取模型信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template`s model info")
	}
	if *modelInfo.Status != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model` status is unNormal")
	}
	//如果模型是配件则返回
	if *modelInfo.Tag != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model your post is not a model but fitting")
	}
	if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty")
	}
	if err = json.Unmarshal([]byte(*modelInfo.ModelInfo), &modelJson); err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model design info")
	}
	//获取灯光信息
	if *modelInfo.Light > 0 {
		lightInfo, err := l.svcCtx.AllModels.FsProductModel3dLight.FindOne(l.ctx, *modelInfo.Light)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the model`s light info is not exists")
			}
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light info")
		}
		lightName = *lightInfo.Name
		if lightInfo.Info != nil && *lightInfo.Info != "" {
			if err = json.Unmarshal([]byte(*lightInfo.Info), &lightJson); err != nil {
				logx.Error(err)
				return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse light design info")
			}
		}
	}
	var sizeKeyInfo gmodel.SizeInfo
	if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeKeyInfo); err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse size info`s title ")
	}
	sizeKeyInfo.Capacity = *sizeInfo.Capacity
	//快照数据
	snapshot := gmodel.CartSnapshot{
		Logo:                 req.Logo,
		LogoMaterialMetadata: logoMaterialMetadata,
		CombineImage:         req.CombineImage,
		RenderImage:          req.RenderImage,
		TemplateInfo: gmodel.TemplateInfo{
			SwitchInfo:       req.SwitchInfo,
			TemplateJson:     templateJson,
			TemplateTag:      templateTag,
			SelectColorIndex: req.SelectColorIndex,
		},
		ModelInfo: gmodel.ModelInfo{
			ModelJson: modelJson,
		},
		FittingInfo: gmodel.FittingInfo{
			FittingJson: fittingJson,
			FittingName: fittingName,
		},
		SizeInfo: sizeKeyInfo,
		ProductInfo: gmodel.ProductInfo{
			ProductName: *productInfo.Title,
			ProductSn:   *productInfo.Sn,
			ProductWebsiteUnit: gmodel.ProductWebsiteUnit{
				Singular: req.ProductWebsiteUnit.Singular,
				Complex:  req.ProductWebsiteUnit.Complex,
			},
		},
		UserDiyInformation: gmodel.UserDiyInformation{
			Phone:   req.DiyInfo.Phone,
			Address: req.DiyInfo.Address,
			Website: req.DiyInfo.Website,
			Qrcode:  req.DiyInfo.Qrcode,
			Slogan:  req.DiyInfo.Slogan,
		},
		LightInfo: gmodel.LightInfo{
			LightJson: lightJson,
			LightName: lightName,
		},
	}
	snapshotJsonBytes, _ := json.Marshal(snapshot)
	snapshotJsonStr := string(snapshotJsonBytes)
	now := time.Now().UTC()
	err = l.svcCtx.AllModels.FsShoppingCart.Create(l.ctx, &gmodel.FsShoppingCart{
		UserId:             &userinfo.UserId,
		ProductId:          &req.ProductId,
		TemplateId:         &req.TemplateId,
		ModelId:            &modelInfo.Id,
		IsHighlyCustomized: &req.IsHighlyCustomized,
		LightId:            modelInfo.Light,
		SizeId:             &req.SizeId,
		FittingId:          &req.FittingId,
		PurchaseQuantity:   &req.PurchaseQuantity,
		Snapshot:           &snapshotJsonStr,
		Ctime:              &now,
		Utime:              &now,
	})
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to add to cart ")
	}
	return resp.SetStatus(basic.CodeOK, "success")
}

// 参数校验
func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error {
	if req.ProductId <= 0 {
		return errors.New("product_id is required")
	}
	if req.SizeId <= 0 {
		return errors.New("product size id is required")
	}
	if req.PurchaseQuantity <= 0 {
		return errors.New("purchase quantity can not less than 0 or equal 0")
	}
	req.RenderImage = strings.Trim(req.RenderImage, " ")
	if req.RenderImage == "" {
		return errors.New("render image is required")
	}
	if req.SelectColorIndex < 0 {
		return errors.New("invalid select color index")
	}
	if req.Logo == "" {
		return errors.New("logo is required")
	}
	return nil
}

// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }