package product

import (
	"bsm/full/module/ec/mall/internal/models"
	pb "bsm/full/module/ec/mall/pb"
)

func ModelsToProductItems(data []*models.MallProduct) (list []*pb.ProductItem) {
	if len(data) == 0 {
		return
	}
	for _, v := range data {
		line := pb.ProductItem{
			Id:             int64(v.ID),
			Identity:       v.Identity,
			SupplyId:       v.SupplyId,
			Title:          v.Title,
			MeasuringName:  v.MeasuringName,
			SellMax:        int64(v.SellMax),
			SellMin:        int64(v.SellMin),
			Wastage:        int64(v.Wastage),
			StandardNumber: v.StandardNumber,
			Brand:          v.Brand,
			StockType:      int64(v.StockType),
			Status:         v.Status,
			PutawayTime:    v.PutawayTime,
			CoverImage:     v.CoverImage,
			Content:        v.Content,
			SerialId:       v.SerialId,
			CreatedAt:      v.CreatedAt.Format("2006-01-02 15:04:05"),
			SaleTotal:      int64(v.SaleTotal),
			RecentNumber:   v.RecentNumber,
			GasTypes:       int64(v.GasType),
		}

		for _, image := range v.Images {
			line.Images = append(line.Images, &pb.PhotoItem{
				Id:              int64(image.ID),
				Identity:        image.Identity,
				Name:            image.Name,
				ProductIdentity: image.ProductIdentity,
				Sort:            image.Sort,
				Url:             image.Url,
			})
		}
		for _, specs := range v.Specs {
			line.Specification = append(line.Specification, &pb.SpecItem{
				Id:              int64(specs.Spec.ID),
				Identity:        specs.Identity,
				ProductIdentity: v.Identity,
				Title:           specs.Spec.Title,
				StockType:       specs.Spec.StockType,
				Stock:           specs.Spec.Stock,
				Price:           specs.Spec.Price,
				DirectSales:     specs.Spec.DirectSales,
				TpType:          specs.Spec.TpType,
				DsTpType:        specs.Spec.DsTpType,
				Img:             specs.Spec.Img,
			})
		}
		for _, categories := range v.Categories {
			line.Categories = append(line.Categories, &pb.CategoriesItem{
				Id:       int64(categories.Category.ID),
				Identity: categories.Identity,
				Title:    categories.Category.Title,
				EnTitle:  categories.Category.EnTitle,
				ParentId: int64(categories.Category.ParentID),
				Paths:    categories.Category.Paths,
				Intro:    categories.Category.Intro,
				Icon:     categories.Category.Icon,
				Sort:     int32(categories.Category.Sort),
				Status:   int32(categories.Status),
				Keys:     categories.Category.Keys,
			})
		}
		if len(line.Images) == 0 {
			line.Images = make([]*pb.PhotoItem, 0)
		}
		if len(v.Specs) > 0 {
			line.Price = v.Specs[0].Spec.Price
			line.Stock = v.Specs[0].Spec.Stock
		}
		list = append(list, &line)
	}
	return
}

func ModelToProductItem(data *models.MallProduct) (*pb.ProductItem, error) {
	var item = &pb.ProductItem{
		Id:             int64(data.ID),
		Identity:       data.Identity,
		SupplyId:       data.SupplyId,
		SerialId:       data.SerialId,
		Title:          data.Title,
		MeasuringName:  data.MeasuringName,
		SellMax:        int64(data.SellMax),
		SellMin:        int64(data.SellMin),
		Wastage:        int64(data.Wastage),
		StandardNumber: data.StandardNumber,
		Brand:          data.Brand,
		StockType:      int64(data.StockType),
		Status:         data.Status,
		PutawayTime:    data.PutawayTime,
		CoverImage:     data.CoverImage,
		Content:        data.Content,
		TemplateId:     int32(data.TemplateID),
		Note:           data.Notes,
		Images:         []*pb.PhotoItem{},
		Specification:  []*pb.SpecItem{},
		Categories:     []*pb.CategoriesItem{},
		RecentNumber:   data.RecentNumber,
		Limitation:     data.Limitation,
		LiTemplateId:   data.LiTemplateID,
		SaleTotal:      int64(data.SaleTotal),
		CreatedAt:      data.CreatedAt.Format("2006-01-02 15:04:05"),
		GasTypes:       int64(data.GasType),
	}

	if len(data.Images) != 0 {
		item.Images = make([]*pb.PhotoItem, 0)
		for _, v := range data.Images {
			item.Images = append(item.Images, &pb.PhotoItem{
				Identity: v.Identity,
				Url:      v.Url,
				Name:     v.Name,
				Id:       int64(v.ID),
			})
		}
	}
	if len(data.Specs) != 0 {
		item.Specification = make([]*pb.SpecItem, 0)
		for _, specs := range data.Specs {
			item.Specification = append(item.Specification, &pb.SpecItem{
				Id:              int64(specs.Spec.ID),
				Identity:        specs.Identity,
				Title:           specs.Spec.Title,
				StockType:       specs.Spec.StockType,
				Stock:           specs.Spec.Stock,
				Img:             specs.Spec.Img,
				Price:           specs.Spec.Price,
				SpecNo:          specs.Spec.SerialNumber,
				Keyword:         specs.Spec.Keyword,
				PurchasingPrice: specs.Spec.PurchasingPrice,
				Wholesale:       specs.Spec.Wholesale,
			})
		}
	}
	if len(data.Categories) != 0 {
		item.Categories = make([]*pb.CategoriesItem, 0)
		for _, categories := range data.Categories {
			item.Categories = append(item.Categories, &pb.CategoriesItem{
				Id:       int64(categories.Category.ID),
				Identity: categories.Identity,
				Title:    categories.Category.Title,
				EnTitle:  categories.Category.EnTitle,
				ParentId: int64(categories.Category.ParentID),
				Paths:    categories.Category.Paths,
				Intro:    categories.Category.Intro,
				Icon:     categories.Category.Icon,
				Sort:     int32(categories.Category.Sort),
				Keys:     categories.Category.Keys,
			})
		}

	}
	return item, nil
}

func ModelToReplySpecList(data []*models.MallProductSpec) []*pb.SpecItem {
	var res []*pb.SpecItem
	for _, datum := range data {
		res = append(res, &pb.SpecItem{
			Id:          int64(datum.ID),
			Identity:    datum.Identity,
			Title:       datum.Title,
			StockType:   datum.StockType,
			Stock:       datum.Stock,
			Price:       datum.Price,
			DirectSales: datum.DirectSales,
			TpType:      datum.TpType,
			DsTpType:    datum.DsTpType,
			Img:         datum.Img,
			SpecNo:      datum.SpecNo,
			Keyword:     datum.Keyword,
			Wholesale:   datum.Wholesale,
		})
	}
	return res
}
