package model

import (
	operationPb "kpt-tmr-group/proto/go/backend/operation"
	"time"
)

type Forage struct {
	Id                 int64                           `json:"id"`
	PastureId          int64                           `json:"pasture_id"`
	PastureName        string                          `json:"pasture_name"`
	Name               string                          `json:"name"`
	CategoryId         int64                           `json:"category_id"`
	CategoryName       string                          `json:"category_name"`
	MaterialType       int64                           `json:"material_type"`
	UniqueEncode       string                          `json:"unique_encode"`
	ForageSourceId     operationPb.ForageSource_Kind   `json:"forage_source_id"`
	ForageSourceName   string                          `json:"forage_source_name"`
	PlanTypeId         operationPb.ForagePlanType_Kind `json:"plan_type_id"`
	PlanTypeName       string                          `json:"plan_type_name"`
	SmallMaterialScale string                          `json:"small_material_scale"`
	AllowError         int64                           `json:"allow_error"`
	PackageWeight      int64                           `json:"package_weight"`
	Price              int64                           `json:"price"`
	JumpWeight         int64                           `json:"jump_weight"`
	JumpDelay          operationPb.JumpDelaType_Kind   `json:"jump_delay"`
	ConfirmStart       operationPb.IsShow_Kind         `json:"confirm_start"`
	RelayLocations     int64                           `json:"relay_locations"`
	Jmp                operationPb.IsShow_Kind         `json:"jmp"`
	DataSource         operationPb.DataSource_Kind     `json:"data_source"`
	Backup1            string                          `json:"backup1"`
	Backup2            string                          `json:"backup2"`
	Backup3            string                          `json:"backup3"`
	IsShow             operationPb.IsShow_Kind         `json:"is_show"`
	IsDelete           operationPb.IsShow_Kind         `json:"is_delete"`
	CreatedAt          int64                           `json:"created_at"`
	UpdatedAt          int64                           `json:"updated_at"`
}

func (c *Forage) TableName() string {
	return "forage"
}

func NewForage(req *operationPb.AddForageRequest) *Forage {
	return &Forage{
		Name:               req.Name,
		CategoryId:         int64(req.CategoryId),
		CategoryName:       req.CategoryName,
		UniqueEncode:       req.UniqueEncode,
		ForageSourceId:     req.ForageSourceId,
		ForageSourceName:   req.ForageSourceName,
		PlanTypeId:         req.PlanTypeId,
		PlanTypeName:       req.PlanTypeName,
		SmallMaterialScale: req.SmallMaterialScale,
		AllowError:         int64(req.AllowError),
		PackageWeight:      int64(req.PackageWeight),
		Price:              int64(req.Price),
		JumpWeight:         int64(req.JumpWeight),
		JumpDelay:          req.JumpDelay,
		ConfirmStart:       req.ConfirmStart,
		RelayLocations:     int64(req.RelayLocations),
		IsShow:             operationPb.IsShow_OK,
		IsDelete:           operationPb.IsShow_OK,
		DataSource:         operationPb.DataSource_BACKGROUND_ADD,
		Jmp:                req.Jmp,
		Backup1:            req.Backup1,
		Backup2:            req.Backup2,
		Backup3:            req.Backup3,
	}
}

type ForageSlice []*Forage

func (f ForageSlice) ToPB() []*operationPb.AddForageRequest {
	res := make([]*operationPb.AddForageRequest, len(f))
	for i, v := range f {
		res[i] = &operationPb.AddForageRequest{
			Id:                 int32(v.Id),
			Name:               v.Name,
			CategoryId:         int32(v.CategoryId),
			CategoryName:       v.CategoryName,
			MaterialType:       int32(v.MaterialType),
			UniqueEncode:       v.UniqueEncode,
			ForageSourceId:     v.ForageSourceId,
			ForageSourceName:   v.ForageSourceName,
			PlanTypeId:         v.PlanTypeId,
			PlanTypeName:       v.PlanTypeName,
			SmallMaterialScale: v.SmallMaterialScale,
			AllowError:         int32(v.AllowError),
			PackageWeight:      int32(v.PackageWeight),
			Price:              int32(v.Price),
			JumpWeight:         int32(v.JumpWeight),
			JumpDelay:          v.JumpDelay,
			ConfirmStart:       v.ConfirmStart,
			RelayLocations:     int32(v.RelayLocations),
			Jmp:                v.Jmp,
			IsShow:             v.IsShow,
			Backup1:            v.Backup1,
			Backup2:            v.Backup2,
			Backup3:            v.Backup3,
			CreatedAt:          int32(v.CreatedAt),
			CreatedAtFormat:    time.Unix(v.CreatedAt, 0).Format(LayoutTime),
		}
	}
	return res
}