package model

import (
	"strconv"
	"strings"

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

type Prescription struct {
	Id                int32                 `json:"id"`
	PastureId         int64                 `json:"pastureId"`
	Name              string                `json:"name"`
	ApplicableDisease string                `json:"applicableDisease"`
	UseDays           int32                 `json:"useDays"`
	UseCount          int32                 `json:"useCount"`
	MeatExpiredDays   int32                 `json:"meatExpiredDays"`
	MilkExpiredDays   int32                 `json:"milkExpiredDays"`
	IsShow            pasturePb.IsShow_Kind `json:"isShow"`
	Remarks           string                `json:"remarks"`
	OperationId       int64                 `json:"operationId"`
	OperationName     string                `json:"operationName"`
	CreatedAt         int64                 `json:"createdAt"`
	UpdatedAt         int64                 `json:"updatedAt"`
}

func (p *Prescription) TableName() string {
	return "prescription"
}

func (p *Prescription) EventUseCountUpdate() {
	p.UseCount += 1
}

func NewPrescription(
	pastureId int64,
	req *pasturePb.PrescriptionRequest,
	applicableDisease string,
	useDays,
	meatExpiredDays,
	milkExpiredDays int32,
	systemUser *SystemUser,
) *Prescription {
	return &Prescription{
		PastureId:         pastureId,
		Name:              req.Name,
		ApplicableDisease: applicableDisease,
		UseDays:           useDays,
		UseCount:          0,
		MeatExpiredDays:   meatExpiredDays,
		MilkExpiredDays:   milkExpiredDays,
		IsShow:            req.IsShow,
		Remarks:           req.Remarks,
		OperationId:       systemUser.Id,
		OperationName:     systemUser.Name,
	}
}

type PrescriptionSlice []*Prescription

func (p PrescriptionSlice) ToPB(prescriptionDrugsList []*PrescriptionDrugs, diseaseMap map[int64]*Disease) []*pasturePb.SearchPrescriptionList {
	res := make([]*pasturePb.SearchPrescriptionList, len(p))
	for i, v := range p {
		drugsList := make([]*pasturePb.PrescriptionDrugsList, 0)
		for _, pl := range prescriptionDrugsList {
			if pl.PrescriptionId != v.Id {
				continue
			}
			drugsList = append(drugsList, &pasturePb.PrescriptionDrugsList{
				Id:        pl.Id,
				DrugsId:   int32(pl.DrugsId),
				DrugsName: pl.DrugsName,
				Unit:      pl.Unit,
				UnitName:  pl.UnitName,
				Dosages:   pl.Dosages,
				UseDays:   pl.UseDays,
				Specs:     pl.Specs,
			})
		}
		applicableDiseaseList := strings.Split(v.ApplicableDisease, ",")
		applicableDiseaseNames := make([]string, 0)
		applicableDiseaseIds := make([]int32, 0)
		for _, ad := range applicableDiseaseList {
			diseaseId, _ := strconv.ParseInt(ad, 10, 64)
			applicableDiseaseIds = append(applicableDiseaseIds, int32(diseaseId))
			if dis, ok := diseaseMap[diseaseId]; ok {
				applicableDiseaseNames = append(applicableDiseaseNames, dis.Name)
			}
		}

		res[i] = &pasturePb.SearchPrescriptionList{
			Id:                     int32(v.Id),
			Name:                   v.Name,
			ApplicableDisease:      v.ApplicableDisease,
			ApplicableDiseaseIds:   applicableDiseaseIds,
			ApplicableDiseaseNames: applicableDiseaseNames,
			UseDays:                v.UseDays,
			UseCount:               v.UseCount,
			MeatExpiredDays:        v.MeatExpiredDays,
			MilkExpiredDays:        v.MilkExpiredDays,
			IsShow:                 v.IsShow,
			Remarks:                v.Remarks,
			OperationId:            int32(v.OperationId),
			OperationName:          v.OperationName,
			CreatedAt:              int32(v.CreatedAt),
			UpdatedAt:              int32(v.UpdatedAt),
			DrugsList:              drugsList,
		}
	}
	return res
}

func (p PrescriptionSlice) ToPB2() []*pasturePb.ConfigOptionsList {
	res := make([]*pasturePb.ConfigOptionsList, len(p))
	for i, d := range p {
		res[i] = &pasturePb.ConfigOptionsList{
			Value:    int32(d.Id),
			Label:    d.Name,
			Disabled: true,
		}
	}
	return res
}