package task

import (
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/scheduler"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
)

type taskFactory struct {
	logger         *logs.Logger
	sensorDataRepo repository.SensorDataRepository
	deviceRepo     repository.DeviceRepository
	deviceService  device.Service
}

func NewTaskFactory(
	logger *logs.Logger,
	sensorDataRepo repository.SensorDataRepository,
	deviceRepo repository.DeviceRepository,
	deviceService device.Service,
) scheduler.TaskFactory {
	return &taskFactory{
		logger:         logger,
		sensorDataRepo: sensorDataRepo,
		deviceRepo:     deviceRepo,
		deviceService:  deviceService,
	}
}

func (t *taskFactory) Production(claimedLog *models.TaskExecutionLog) scheduler.Task {
	switch claimedLog.Task.Type {
	case models.TaskTypeWaiting:
		return NewDelayTask(t.logger, claimedLog)
	case models.TaskTypeReleaseFeedWeight:
		return NewReleaseFeedWeightTask(claimedLog, t.sensorDataRepo, t.deviceRepo, t.deviceService, t.logger)
	case models.TaskTypeFullCollection:
		return NewFullCollectionTask(claimedLog, t.deviceRepo, t.deviceService, t.logger)
	default:
		// TODO 这里直接panic合适吗? 不过这个场景确实不该出现任何异常的任务类型
		t.logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
		panic("不支持的任务类型") // 显式panic防编译器报错
	}
}
