package cz.muni.fi.lasaris.sbms.data.logic;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;

import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Snapshot;
import cz.muni.fi.lasaris.sbms.data.entities.values.AggregatedValue;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
import cz.muni.fi.lasaris.sbms.data.util.Aggregator;



public class DPDataCollector {
	
	final static Logger logger = Logger.getLogger(DPDataCollector.class);
	
	public SnapshotResponse getDataPointValues(AddressRequest readSpecs, boolean allowCache) {
		return new SnapshotResponse(getDataPointValuesSnapshot(readSpecs, allowCache));
	}
	
	private Snapshot getDataPointValuesSnapshot(AddressRequest readSpecs, boolean allowCache) {
NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
		
		if(protocols.size() == 1) {
			return ProviderManager.getDataPointsProvider(protocols.firstKey())
					.getDataPointValues(protocols.get(protocols.firstKey()).getAddresses(), allowCache);
		}
		
		AtomicInteger counter = new AtomicInteger(0);
		int count = protocols.keySet().size();
		
		List<Snapshot> results = Collections.synchronizedList(new LinkedList<Snapshot>());
		
		for(String protocol : protocols.keySet()) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					Snapshot rs = (ProviderManager.getDataPointsProvider(protocol)
							.getDataPointValues(protocols.get(protocol).getAddresses(), allowCache)); 
					synchronized(results) {
						results.add(rs);
						counter.incrementAndGet();
						results.notifyAll();
					}
					
				}}).start();
		}
		synchronized(results) {
			while(counter.get() < count) {
				try {
					results.wait();
				} catch (InterruptedException e) {
					logger.error("Exception while waiting for completion.", e);
				}
			}
		}
		
		Snapshot result = new Snapshot();
		
		
		synchronized(results) {
			for(Snapshot r : results) {
				result.addAll(r.getData());
			}
		}
		
		return result;
	}
	
	public AggregateResponse getDataPointsAggregation(AddressRequest readSpecs, AggregationFunction aggregation,
			boolean allowCache) {
		
		NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
		
		if(protocols.size() == 1) {
			return new AggregateResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
					.getDataPointsAggregation(protocols.get(protocols.firstKey()).getAddresses(), aggregation, allowCache));
		}
		
		Snapshot data = getDataPointValuesSnapshot(readSpecs, allowCache);
		
		return new AggregateResponse(computeAggregation(data.getData(), aggregation));
		
	}
	
	public SnapshotResponse getDataPointGroupedValues(GroupedAddressRequest readSpecs,
			boolean allowCache) {
		return new SnapshotResponse(getDataPointGroupedValuesMap(readSpecs, allowCache));
	}
	
	private Map<String, Snapshot> getDataPointGroupedValuesMap(GroupedAddressRequest readSpecs,
			boolean allowCache) {
		NavigableMap<String, GroupedAddressRequest> protocols = readSpecs.getProtocols();
		
		if(protocols.size() == 1) {
			return ProviderManager.getDataPointsProvider(protocols.firstKey())
					.getDataPointGroupedValues(protocols.get(protocols.firstKey()).getGroups(), allowCache);
		}
		
		AtomicInteger counter = new AtomicInteger(0);
		int count = protocols.keySet().size();
		
		List<SnapshotResponse> results = Collections.synchronizedList(new LinkedList<SnapshotResponse>());
		
		for(String protocol : protocols.keySet()) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					SnapshotResponse rs = new SnapshotResponse(ProviderManager.getDataPointsProvider(protocol).getDataPointGroupedValues(protocols.get(protocol).getGroups(), allowCache)); 
					synchronized(results) {
						results.add(rs);
						counter.incrementAndGet();
						results.notifyAll();
					}
					
				}}).start();
		}
		
		synchronized(results) {
			while(counter.get() < count) {
				try {
					results.wait();
				} catch (InterruptedException e) {
					logger.error("Exception while waiting for completion.", e);
				}
			}
		}
		
		Map<String, Snapshot> result = new LinkedHashMap<String, Snapshot>();
		for(String group : readSpecs.getGroups().keySet()) {
			result.put(group, new Snapshot());
		}
		
		synchronized(results) {
			for(SnapshotResponse r : results) {
				for(String group : r.getResults().keySet()) {
					result.get(group).addAll(r.getResults().get(group).getData());
				}
			}
		}
		
		return result;
	}

	public AggregateResponse getDataPointGroupAggregations(GroupedAddressRequest readSpecs,
			AggregationFunction aggregation, boolean allowCache) {
		
		NavigableMap<String, GroupedAddressRequest> protocols = readSpecs.getProtocols();
		
		if(protocols.size() == 1) {
			return new AggregateResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
					.getDataPointGroupAggregations(protocols.get(protocols.firstKey()).getGroups(), aggregation, allowCache));
		}
		
		Map<String, Snapshot> data = getDataPointGroupedValuesMap(readSpecs, allowCache);
		
		return new AggregateResponse(computeGroupAggregations(data, aggregation));
	}

	private Map<String, AggregatedValue> computeGroupAggregations(Map<String, Snapshot> data, AggregationFunction aggregation) {
		Map<String, AggregatedValue> result = new LinkedHashMap<String, AggregatedValue>();
		
		for(String group : data.keySet()) {
			AggregatedValue value = Aggregator.computeAggregation(data.get(group).getData(), aggregation);
			result.put(group, value);
		}
		return result;
	}
	
	private AggregatedValue computeAggregation(Map<Address, RawValue> data, AggregationFunction aggregation) {
		return Aggregator.computeAggregation(data, aggregation);
	}

}
