package com.oguerreiro.resilient.activities.period.open;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oguerreiro.resilient.activity.AbstractActivity;
import com.oguerreiro.resilient.activity.GenericGuide;
import com.oguerreiro.resilient.activity.GenericResult;
import com.oguerreiro.resilient.activity.WorkResultEnum;
import com.oguerreiro.resilient.activity.registry.ActivityProgressRegistry;
import com.oguerreiro.resilient.domain.Period;
import com.oguerreiro.resilient.domain.enumeration.PeriodStatus;
import com.oguerreiro.resilient.repository.PeriodRepository;

@Component
public class PeriodOpenActivityImpl extends AbstractActivity<GenericResult, GenericGuide<Period>, PeriodOpenActivity> implements PeriodOpenActivity {

	private static final String ACTIVITY_KEY = "period-open";
	
	private final PeriodRepository periodRepository;
	private final ActivityProgressRegistry activityProgressRegistry;
	
	/* ----- Constructor ----- */
	public PeriodOpenActivityImpl(PeriodRepository periodRepository, ActivityProgressRegistry activityProgressRegistry) {
		this.periodRepository = periodRepository;
		this.activityProgressRegistry = activityProgressRegistry;
	}
	
	/* ----- SelectionStrategy properties ----- */
	@Override
	public Class appliesToActivityDomainClass() {
		return Period.class;
	}

	@Override
	public List<String> appliesToStates() {
		return Arrays.asList(PeriodStatus.CREATED.toString());
	}
	
	/* ----- Activity implementation ----- */
	@Override
	public String getActivityKey() {
		return ACTIVITY_KEY;
	}
	
	@Transactional
	@Override
	public GenericResult doActivity(String activityDomainClass, Long activityDomainId) {
		// Load Period
		Period period = periodRepository.getReferenceById(activityDomainId);
		
		// Validations
		if (!period.getState().equals(PeriodStatus.CREATED)) {
			throw new RuntimeException("Activity Open, applies only to CREATED period state.");
		}
		
		// Change status
		period.setState(PeriodStatus.OPENED);
		
		// Return without waiting
		return new GenericResult(WorkResultEnum.COMPLETED);
	}

	@Override
	public CompletableFuture<GenericResult> doInternalActivityAsync(GenericGuide<Period> guide) {
		// TODO Auto-generated method stub
		return null;
	}
}
