package comm

import (
	"time"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

type Fabric struct {
	Id        primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
	UserId    primitive.ObjectID `bson:"userId,omitempty" json:"userId,omitempty"`
	TeamId    string             `bson:"teamId,omitempty" json:"teamId,omitempty"`       //所属团队Id
	CompanyId string             `bson:"companyId,omitempty" json:"companyId,omitempty"` //所属团队Id

	Name       string    `bson:"name,omitempty" json:"name,omitempty"`
	CusNum     string    `bson:"cusNum,omitempty" json:"cusNum,omitempty"`
	Thumbnail  *OssType  `bson:"thumbnail,omitempty" json:"thumbnail,omitempty"`
	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime,omitempty"`

	Price      float32         `bson:"price,omitempty" json:"price,omitempty"`
	ColorCards []*MaterialConf `bson:"colorCards,omitempty" json:"colorCards,omitempty"`

	Categories []string `bson:"categories,omitempty" json:"categories,omitempty"`
	State      int32    `bson:"state,omitempty" json:"state,omitempty"`
}

func (s *AssetMatGroup) CopyFromFabric(fab *Fabric) {
	s.Id = fab.Id
	s.AssetState = AssetState_Succ
	s.Categories = fab.Categories
	s.CreateTime = fab.CreateTime
	s.UpdateTime = fab.CreateTime
	s.UserId = fab.UserId
	s.OwnerId = fab.UserId
	s.OwnerType = "user"
	s.CusCategories = fab.Categories
	s.CusNum = fab.CusNum
	s.Name = fab.Name
	enable := true
	s.Enable = &enable
	s.Thumbnail = fab.Thumbnail
	s.Source = &AssetMatGroupSource{
		Version:    "1.0",
		ColorCards: []*MatConfigSource{},
	}

	var toFloat32 = func(f *float64) *float32 {
		var out float32 = 0

		if f != nil {
			out = float32(*f)
		}
		return &out
	}

	for _, c := range fab.ColorCards {
		conf := &MatConfigSource{
			Version:        1,
			Type:           c.Type,
			Name:           c.Name,
			CusNum:         c.CusNum,
			Thumbnail:      c.Thumbnail,
			UvTransform:    c.Uvtransform,
			CusUvTransform: c.CusUvTransform,
			Albedo: &MatTextureColor{
				Color:   c.BaseColor,
				Texture: c.BaseMap,
			},
			Roughness: &MatTextureFactor{
				Texture: c.RoughMap,
				Factor:  toFloat32(c.RoughFactor),
			},
			Metalness: &MatTextureFactor{
				Factor:  toFloat32(c.MetalFactor),
				Texture: c.MetalMap,
			},
			Normal: &MatNormal{
				UseTexture: &enable,
				Texture:    c.NormalMap,
			},
			Opacity: &MatTextureFactorWithEnable{
				Texture: c.OpacMap,
				Factor:  toFloat32(c.OpacFactor),
			},
			Displace: &MatTextureFactorWithEnable{
				Texture: c.DisplaceMap,
				Factor:  toFloat32(c.DisplaceFactor),
			},
			Diffuse: &MatTextureColor{
				Color:   c.DiffuseColor,
				Texture: c.DiffuseMap,
			},
			Gloss: &MatTextureFactor{
				Factor:  toFloat32(c.GlossFactor),
				Texture: c.GlossMap,
			},
			Specular: &MatTextureColor{
				Color:   c.SpecColor,
				Texture: c.SpecMap,
			},
			UvMap: "box",
		}

		if c.BaseMap != nil && len(c.BaseMap.Url) > 0 {
			conf.Albedo.UseTexture = &enable
		}
		if c.RoughMap != nil && len(c.RoughMap.Url) > 0 {
			conf.Roughness.UseTexture = &enable
		}
		if c.MetalMap != nil && len(c.MetalMap.Url) > 0 {
			conf.Metalness.UseTexture = &enable
		}
		if c.OpacFactor != nil && *c.OpacFactor < 0.99 {
			conf.Opacity.Enable = true
		}
		if c.OpacMap != nil && len(c.OpacMap.Url) > 0 {
			conf.Opacity.Enable = true
			conf.Opacity.UseTexture = &enable
		}

		if c.DisplaceMap != nil && len(c.DisplaceMap.Url) > 0 {
			conf.Displace.Enable = true
			conf.Displace.UseTexture = &enable
		}
		if c.DiffuseMap != nil && len(c.DiffuseMap.Url) > 0 {
			conf.Diffuse.UseTexture = &enable
		}
		if c.GlossMap != nil && len(c.GlossMap.Url) > 0 {
			conf.Gloss.UseTexture = &enable
		}
		if c.SpecMap != nil && len(c.SpecMap.Url) > 0 {
			conf.Specular.UseTexture = &enable
		}

		s.Source.ColorCards = append(s.Source.ColorCards, conf)
	}
}
