package de.oaa.xxx.games.chastity.timelock;

import java.util.List;

import de.oaa.xxx.games.chastity.common.BaseLockTemplateEntity;
import de.oaa.xxx.games.chastity.common.PenaltyType;
import de.oaa.xxx.games.chastity.spinningwheel.SpinningWheelConverter;
import de.oaa.xxx.games.chastity.spinningwheel.SpinningWheelEntry;
import de.oaa.xxx.games.chastity.tasks.TaskMode;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@DiscriminatorValue("TIMELOCK")
public class TimeLockTemplateEntity extends BaseLockTemplateEntity {
	
	@Column
	private Integer minTimeInMinutes;
	@Column
	private Integer maxTimeInMinutes;
	@Column
	private boolean endTimeVisible;
	@Column
	private Integer taskEveryMinutes;
	@Column
	private Integer minTasksPerDay;
	@Convert(converter = SpinningWheelConverter.class)
	@Column(columnDefinition = "TEXT")
	private List<SpinningWheelEntry> spinningWheelEntries;
	@Column
	private Integer spinsEveryMinutes;
	@Column
	private Integer minSpinsPerDay;
	@Column
	private PenaltyType penaltyType;
	@Column
	private Integer penaltyValue;

	public TimeLockTemplate toTimeLockTemplate() {
		return new TimeLockTemplate(getTemplateId(), getOwner(), getName(), minTimeInMinutes, maxTimeInMinutes, endTimeVisible,
				getHygineOpeningDurationMinutes(), getHygineOpeningEveryMinites(), getTasks(), taskEveryMinutes, minTasksPerDay,
				spinningWheelEntries, spinsEveryMinutes, minSpinsPerDay, isRequiresVerification(), getTaskMode(), penaltyType,
				penaltyValue);
	}

	public static TimeLockTemplateEntity fromTemplate(TimeLockTemplate template) {
		if (template != null) {
			TimeLockTemplateEntity entity = new TimeLockTemplateEntity();
			entity.setOwner(template.owner());
			entity.setName(template.name());
			entity.setMinTimeInMinutes(template.minTimeInMinutes());
			entity.setMaxTimeInMinutes(template.maxTimeInMinutes());
			entity.setEndTimeVisible(template.endTimeVisible());
			entity.setHygineOpeningDurationMinutes(template.hygineOpeningDurationMinutes());
			entity.setHygineOpeningEveryMinites(template.hygineOpeningEveryMinites());
			entity.setTasks(template.tasks());
			entity.setTaskEveryMinutes(template.taskEveryMinutes());
			entity.setMinTasksPerDay(template.minTasksPerDay());
			entity.setSpinningWheelEntries(template.spinningWheelEntries());
			entity.setSpinsEveryMinutes(template.spinsEveryMinutes());
			entity.setMinSpinsPerDay(template.minSpinsPerDay());
			entity.setRequiresVerification(template.requiresVerification());
			entity.setTaskMode(template.taskMode() != null ? template.taskMode() : TaskMode.RANDOM);
			entity.setPenaltyType(template.penaltyType());
			entity.setPenaltyValue(template.penaltyValue());
			return entity;
		}
		return null;
	}
}
