package model

import (
	"time"

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

type EventCowSameTime struct {
	Id            int64                       `json:"id"`
	PastureId     int64                       `json:"pastureId"`
	CowId         int64                       `json:"cowId"`
	EarNumber     string                      `json:"earNumber"`
	CowType       pasturePb.CowType_Kind      `json:"cowType"`
	PenId         int32                       `json:"penId"`
	PenName       string                      `json:"penName"`
	Lact          int32                       `json:"lact"`
	SameTimeId    int64                       `json:"sameTimeId"`
	SameTimeName  string                      `json:"sameTimeName"`
	SameTimeType  pasturePb.SameTimeType_Kind `json:"sameTimeType"`
	PlanDay       int64                       `json:"planDay"`
	EndDay        int64                       `json:"endDay"`
	RealityDay    int64                       `json:"realityDay"`
	Status        pasturePb.IsShow_Kind       `json:"status"`
	DrugsId       int64                       `json:"drugsId"`
	Unit          pasturePb.Unit_Kind         `json:"unit"`
	Usage         float32                     `json:"usage"`
	Remarks       string                      `json:"remarks"`
	OperationId   int64                       `json:"operationId"`
	OperationName string                      `json:"operationName"`
	MessageId     int64                       `json:"messageId"`
	MessageName   string                      `json:"messageName"`
	CreatedAt     int64                       `json:"createdAt"`
	UpdatedAt     int64                       `json:"updatedAt"`
}

func (s *EventCowSameTime) TableName() string {
	return "event_cow_same_time"
}

func (s *EventCowSameTime) EventUpdate(drugs *Drugs, usage float32, remarks string, currentUser, operationUser *SystemUser) {
	s.Status = pasturePb.IsShow_Ok
	s.DrugsId = drugs.Id
	s.Unit = drugs.Unit
	s.Usage = usage
	s.Remarks = remarks
	s.OperationId = operationUser.Id
	s.OperationName = operationUser.Name
	s.MessageId = currentUser.Id
	s.MessageName = currentUser.Name
}

func NewEventCowSameTime(pastureId int64, cow *Cow, planTime int64, sameTime *SameTime, sameTimeType pasturePb.SameTimeType_Kind) *EventCowSameTime {
	return &EventCowSameTime{
		PastureId:    pastureId,
		CowId:        cow.Id,
		EarNumber:    cow.EarNumber,
		Lact:         cow.Lact,
		CowType:      cow.CowType,
		PenId:        cow.PenId,
		SameTimeId:   sameTime.Id,
		SameTimeName: sameTime.Name,
		SameTimeType: sameTimeType,
		Status:       pasturePb.IsShow_No,
		PlanDay:      planTime,
		EndDay:       planTime,
	}
}

func NewEventCowSameTimeList(pastureId int64, cowList []*Cow, sameTime *SameTime, planTime int64, sameTimeType pasturePb.SameTimeType_Kind) []*EventCowSameTime {
	res := make([]*EventCowSameTime, len(cowList))
	for i, cow := range cowList {
		res[i] = NewEventCowSameTime(pastureId, cow, planTime, sameTime, sameTimeType)
	}
	return res
}

type SameTimeItemBody struct {
	Id              int64                       `json:"id"`
	CowId           int64                       `json:"cowId"`
	EarNumber       string                      `json:"earNumber"`
	BreedStatus     pasturePb.BreedStatus_Kind  `json:"breedStatus"`
	BreedStatusName string                      `json:"breedStatusName"`
	CowType         pasturePb.CowType_Kind      `json:"cowType"`
	CowTypeName     string                      `json:"cowTypeName"`
	PenId           int32                       `json:"penId"`
	PenName         string                      `json:"penName"`
	Lact            int32                       `json:"lact"`
	CalvingAge      int32                       `json:"calvingAge"`
	AbortionAge     int32                       `json:"abortionAge"`
	DayAge          int32                       `json:"dayAge"`
	Status          pasturePb.IsShow_Kind       `json:"status"`
	SameTimeType    pasturePb.SameTimeType_Kind `json:"sameTimeType"`
	LastCalvingAt   int64                       `json:"lastCalvingAt"`
	LastAbortionAt  int64                       `json:"lastAbortionAt"`
	MatingTimes     int32                       `json:"matingTimes"`
	SameTimeName    string                      `json:"sameTimeName"`
	PlanDay         int64                       `json:"planDay"`
}

type SameTimeBodySlice []*SameTimeItemBody

func (s SameTimeBodySlice) ToPB(
	breedStatusMap map[pasturePb.BreedStatus_Kind]string,
	penMap map[int32]*Pen,
	sameTimeTypeMap map[pasturePb.SameTimeType_Kind]string,
) []*pasturePb.SameTimeItems {
	res := make([]*pasturePb.SameTimeItems, len(s))
	for i, v := range s {
		calvingAtFormat, abortionAtFormat, playDayAtFormat := "", "", ""
		if v.LastCalvingAt > 0 {
			calvingAtFormat = time.Unix(v.LastCalvingAt, 0).Format(LayoutDate2)
		}

		if v.LastAbortionAt > 0 {
			abortionAtFormat = time.Unix(v.LastAbortionAt, 0).Format(LayoutDate2)
		}

		if v.PlanDay > 0 {
			playDayAtFormat = time.Unix(v.PlanDay, 0).Format(LayoutDate2)
		}

		res[i] = &pasturePb.SameTimeItems{
			Id:               int32(v.Id),
			CowId:            int32(v.CowId),
			EarNumber:        v.EarNumber,
			BreedStatus:      v.BreedStatus,
			BreedStatusName:  breedStatusMap[v.BreedStatus],
			PenName:          v.PenName,
			PenId:            v.PenId,
			Lact:             v.Lact,
			CalvingAge:       v.CalvingAge,
			AbortionAge:      v.AbortionAge,
			DayAge:           v.DayAge,
			Status:           v.Status,
			SameTimeTypeName: sameTimeTypeMap[v.SameTimeType],
			CalvingAtFormat:  calvingAtFormat,
			AbortionAtFormat: abortionAtFormat,
			MatingTimes:      v.MatingTimes,
			SameTimeName:     v.SameTimeName,
			PlanDay:          playDayAtFormat,
		}
	}
	return res
}

type EventCowSameTimeSlice []*EventCowSameTime

func (s EventCowSameTimeSlice) ToPB(sameTimeTypeMap map[pasturePb.SameTimeType_Kind]string) []*pasturePb.EventSameTime {
	res := make([]*pasturePb.EventSameTime, len(s))
	for i, v := range s {
		res[i] = &pasturePb.EventSameTime{
			CowId:            int32(v.CowId),
			PenId:            v.PenId,
			DrugsId:          int32(v.DrugsId),
			Usage:            v.Usage,
			Lact:             v.Lact,
			SameTimeId:       int32(v.SameTimeId),
			SameTimeType:     v.SameTimeType,
			SameTimeTypeName: sameTimeTypeMap[v.SameTimeType],
		}
	}
	return res
}