package logic

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"fusenapi/constants"
	"fusenapi/model/gmodel"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/format"
	"fusenapi/utils/s3url_to_s3id"
	"fusenapi/utils/shopping_cart"
	"fusenapi/utils/step_price"
	"math"
	"strings"

	"fusenapi/server/shopping-cart/internal/svc"
	"fusenapi/server/shopping-cart/internal/types"

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

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

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

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

func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if !userinfo.IsUser() {
		return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
	}
	if req.CurrentPage <= 0 {
		req.CurrentPage = constants.DEFAULT_PAGE
	}
	limit := 10
	//获取用户购物车列表
	carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
		UserId: userinfo.UserId,
		Sort:   "id DESC",
		Page:   req.CurrentPage,
		Limit:  limit,
	})
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts")
	}
	if len(carts) == 0 {
		return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartsRsp{
			Meta: types.Meta{
				TotalCount:  total,
				PageCount:   int64(math.Ceil(float64(total) / float64(limit))),
				CurrentPage: req.CurrentPage,
				PerPage:     limit,
			},
			CartList: nil,
		})
	}
	var (
		mapSize             = make(map[int64]gmodel.FsProductSize)
		mapModel            = make(map[int64]gmodel.FsProductModel3d)
		mapTemplate         = make(map[int64]gmodel.FsProductTemplateV2)
		mapSizePrice        = make(map[string]gmodel.FsProductPrice)
		mapProduct          = make(map[int64]gmodel.FsProduct)
		mapResourceMetadata = make(map[string]interface{})
	)
	//获取相关信息
	err = l.GetRelationInfo(GetRelationInfoReq{
		Carts:               carts,
		MapSize:             mapSize,
		MapModel:            mapModel,
		MapTemplate:         mapTemplate,
		MapSizePrice:        mapSizePrice,
		MapProduct:          mapProduct,
		MapResourceMetadata: mapResourceMetadata,
	})
	if err != nil {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
	}
	//定义map收集变更信息
	mapCartChange := make(map[int64]string)
	mapSnapshot := make(map[int64]shopping_cart.CartSnapshot)
	//校验购物车数据是否变更
	err = shopping_cart.VerifyShoppingCartSnapshotDataChange(shopping_cart.VerifyShoppingCartSnapshotDataChangeReq{
		Carts:         carts,
		MapSize:       mapSize,
		MapModel:      mapModel,
		MapTemplate:   mapTemplate,
		MapCartChange: mapCartChange,
		MapSnapshot:   mapSnapshot,
		MapProduct:    mapProduct,
	})
	if err != nil {
		logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error())
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data")
	}

	list := make([]types.CartItem, 0, len(carts))
	for _, cart := range carts {
		snapShot := mapSnapshot[cart.Id]
		sizePrice, ok := mapSizePrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)]
		if !ok {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d_%d", *cart.ProductId, *cart.SizeId))
		}
		//阶梯数量切片
		stepNum, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepNum, ","))
		if err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step number:%d_%d", *cart.ProductId, *cart.SizeId))
		}
		lenStepNum := len(stepNum)
		//阶梯价格切片
		stepPrice, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepPrice, ","))
		if err != nil {
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price:%d_%d", *cart.ProductId, *cart.SizeId))
		}
		lenStepPrice := len(stepPrice)
		if lenStepPrice == 0 || lenStepNum == 0 {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price or step number is not set:%d_%d ", *cart.ProductId, *cart.SizeId))
		}
		//购买箱数
		boxQuantity := int(math.Ceil(float64(*cart.PurchaseQuantity) / float64(*sizePrice.EachBoxNum)))
		//获取阶梯数量
		stepQuantityList := make([]int64, 0, 20)
		tmpMinBuyNum := *sizePrice.MinBuyNum
		for tmpMinBuyNum < (int64(stepNum[lenStepNum-1]) + 5) {
			//阶梯数
			tmpQuantity := tmpMinBuyNum * (*sizePrice.EachBoxNum)
			stepQuantityList = append(stepQuantityList, tmpQuantity)
			tmpMinBuyNum++
		}
		sizeCapacity := snapShot.SizeInfo.Capacity
		if sizeInfo, ok := mapSize[*cart.SizeId]; ok {
			sizeCapacity = *sizeInfo.Capacity
		}
		//根据数量获取阶梯价格中对应的价格
		itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice)
		//如果有配件,单价也要加入配件价格
		if *cart.FittingId > 0 {
			if curFittingInfo, ok := mapModel[*cart.FittingId]; ok {
				itemPrice += *curFittingInfo.Price
			}
		}
		fittingName := snapShot.FittingInfo.FittingName
		if fittingInfo, ok := mapModel[*cart.FittingId]; ok {
			fittingName = *fittingInfo.Name
		}
		totalPrice := itemPrice * (*cart.PurchaseQuantity)
		productCover := "" //产品封面图
		productName := snapShot.ProductInfo.ProductName
		productSn := snapShot.ProductInfo.ProductSn
		var productCoverMetadata interface{}
		if productInfo, ok := mapProduct[*cart.ProductId]; ok {
			productCover = *productInfo.Cover
			productName = *productInfo.Title
			productSn = *productInfo.Sn
			if metadata, ok := mapResourceMetadata[*productInfo.Cover]; ok {
				productCoverMetadata = metadata
			}
		}
		item := types.CartItem{
			ProductInfo: types.ProductInfo{
				ProductId:            *cart.ProductId,
				ProductName:          productName,
				ProductSn:            productSn,
				ProductCover:         productCover,
				ProductCoverMetadata: productCoverMetadata,
			},
			SizeInfo: types.SizeInfo{
				SizeId:   *cart.SizeId,
				Capacity: sizeCapacity,
				Title: types.SizeTitle{
					Cm:   snapShot.SizeInfo.Cm,
					Inch: snapShot.SizeInfo.Inch,
				},
			},
			FittingInfo: types.FittingInfo{
				FittingId:   *cart.FittingId,
				FittingName: fittingName,
			},
			ItemPrice:  fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)),
			TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)),
			DiyInformation: types.DiyInformation{
				Phone:   snapShot.UserDiyInformation.Phone,
				Address: snapShot.UserDiyInformation.Address,
				Website: snapShot.UserDiyInformation.Website,
				Qrcode:  snapShot.UserDiyInformation.Qrcode,
				Slogan:  snapShot.UserDiyInformation.Slogan,
			},
			PurchaseQuantity:   *cart.PurchaseQuantity,
			StepNum:            stepQuantityList,
			IsInvalid:          false,
			InvalidDescription: "",
		}
		//是否有失效的
		if description, ok := mapCartChange[cart.Id]; ok {
			item.IsInvalid = true
			item.InvalidDescription = description
		}
		list = append(list, item)
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartsRsp{
		Meta: types.Meta{
			TotalCount:  total,
			PageCount:   int64(math.Ceil(float64(total) / float64(limit))),
			CurrentPage: req.CurrentPage,
			PerPage:     limit,
		},
		CartList: list,
	})
}

// 获取相关信息
type GetRelationInfoReq struct {
	Carts               []gmodel.FsShoppingCart
	MapSize             map[int64]gmodel.FsProductSize
	MapModel            map[int64]gmodel.FsProductModel3d
	MapTemplate         map[int64]gmodel.FsProductTemplateV2
	MapSizePrice        map[string]gmodel.FsProductPrice
	MapProduct          map[int64]gmodel.FsProduct
	MapResourceMetadata map[string]interface{}
}

func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error {
	lenCarts := len(req.Carts)
	templateIds := make([]int64, 0, lenCarts)
	modelIds := make([]int64, 0, lenCarts) //模型 + 配件
	sizeIds := make([]int64, 0, lenCarts)
	productIds := make([]int64, 0, lenCarts)
	for index := range req.Carts {
		templateIds = append(templateIds, *req.Carts[index].TemplateId)
		modelIds = append(modelIds, *req.Carts[index].ModelId, *req.Carts[index].FittingId)
		sizeIds = append(sizeIds, *req.Carts[index].SizeId)
		productIds = append(productIds, *req.Carts[index].ProductId)
	}
	//获取产品集合
	productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, productIds, "")
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get product list")
	}
	resourceIds := make([]string, 0, len(productList))
	for _, v := range productList {
		req.MapProduct[v.Id] = v
		resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.Cover))
	}
	//根据resourceUrls找到对应的元数据
	resourceMetadataList, err := l.svcCtx.AllModels.FsResource.FindAllByResourceIds(l.ctx, resourceIds)
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get resource list")
	}
	for _, v := range resourceMetadataList {
		var metadata interface{}
		if v.Metadata != nil {
			_ = json.Unmarshal(*v.Metadata, &metadata)
		}
		req.MapResourceMetadata[*v.ResourceUrl] = metadata
	}
	//获取尺寸列表
	sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByIds(l.ctx, sizeIds, "")
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get size list")
	}
	for _, v := range sizeList {
		req.MapSize[v.Id] = v
	}
	//获取模型和配件信息
	modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "")
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get model list")
	}
	for _, v := range modelList {
		req.MapModel[v.Id] = v
	}
	//获取模板列表
	templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIds(l.ctx, templateIds)
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get template list")
	}
	for _, v := range templateList {
		req.MapTemplate[v.Id] = v
	}
	//根据sizeid获取价格列表
	priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
	if err != nil {
		logx.Error(err)
		return errors.New("failed to get cart`s product price list")
	}
	for _, v := range priceList {
		req.MapSizePrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v
	}
	return nil
}

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