package model

import (
	"kpt-pasture/util"
	"time"

	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
)

type EventItem struct {
	Id            int64                       `json:"id"`
	CowId         int64                       `json:"cowId"`
	PenId         int32                       `json:"penId"`
	Lact          int32                       `json:"lact"`
	CalendarType  pasturePb.CalendarType_Kind `json:"calendarType"`
	PlanDay       int64                       `json:"planDay"`
	EndDay        int64                       `json:"endDay"`
	RealityDay    int64                       `json:"realityDay"`
	IsFinish      pasturePb.IsShow_Kind       `json:"isFinish"`
	IsExpire      pasturePb.IsShow_Kind       `json:"isExpire"`
	OperationId   int64                       `json:"operationId"`
	OperationName string                      `json:"operationName"`
	Remarks       string                      `json:"remarks"`
	CreatedAt     int64                       `json:"createdAt"`
	UpdatedAt     int64                       `json:"updatedAt"`
}

func (e *EventItem) TableName() string {
	return "event_item"
}

func NewEventItem(cow *Cow, calendarType pasturePb.CalendarType_Kind) *EventItem {
	return &EventItem{
		CowId:        cow.Id,
		PenId:        cow.PenId,
		Lact:         cow.Lact,
		CalendarType: calendarType,
		PlanDay:      util.TimeParseLocalUnix(time.Now().Format(LayoutDate2)),
		EndDay:       util.TimeParseLocalEndUnix(time.Now().Format(LayoutDate2)),
		IsFinish:     pasturePb.IsShow_Ok,
	}
}

func NewEventItemList(cowList []*Cow, calendarType pasturePb.CalendarType_Kind) []*EventItem {
	res := make([]*EventItem, len(cowList))
	for i, v := range cowList {
		res[i] = NewEventItem(v, calendarType)
	}
	return res
}

type EventItemSlice []*EventItem

func (e EventItemSlice) ToPB(penMap map[int32]*Pen, calendarType map[pasturePb.CalendarType_Kind]string) []*pasturePb.CalendarToDoList {
	res := make([]*pasturePb.CalendarToDoList, len(e))
	for i, v := range e {
		penName := ""
		if pen, ok := penMap[v.PenId]; ok {
			penName = pen.Name
		}
		res[i] = &pasturePb.CalendarToDoList{
			Id:               int32(v.Id),
			Lact:             v.Lact,
			CalendarType:     v.CalendarType,
			CalendarTypeName: calendarType[v.CalendarType],
			PlanDay:          time.Unix(v.PlanDay, 0).Format(LayoutDate2),
			EndDay:           time.Unix(v.EndDay, 0).Format(LayoutDate2),
			RealityDay:       time.Unix(v.RealityDay, 0).Format(LayoutDate2),
			IsFinish:         v.IsFinish,
			IsExpire:         v.IsExpire,
			CowId:            int32(v.CowId),
			PenName:          penName,
			Remarks:          v.Remarks,
		}
	}
	return res
}