package com.smartgwt.sample.server;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;


public class DataService {

	public DSResponse fetchContractsByType(DSRequest request){
		DSResponse resp = new DSResponse();
		Contract c = getContracts()[0];
		List<Contract> list = new ArrayList<Contract>();
		list.add(c);
		resp.setData(list);		
		return resp;
	}
	
	public Contract[] getContracts(){
		Contract c = new Contract();
		c.setCategoryType("A05");
		c.setName("Contract 1");
		c.setCapacityContractType("A03");
		c.setId(3);
		ArrayOfItem items = new ArrayOfItem();
		items.setItem(createItems());
		c.setItems(items);
		
		Contract c1 = new Contract();
		c1.setId(5);
		c1.setCategoryType("A04");
		c1.setName("Contract 2");
		ArrayOfItem items1 = new ArrayOfItem();
		items1.setItem(createItems());
		c1.setItems(items1);
		
		Contract[] contracts = new Contract[2];
		contracts[0] = c;
		contracts[1] = c1;
		
		return contracts;
	}
	
	public Item[] createItems()	{
		Item[] items = new Item[2];		
		Item i1 = new Item();
		Item i2 = new Item();
		i1.setId(1);
		i2.setId(2);
		i1.setTimeFrom(Calendar.getInstance());
		i1.setTimeTo(Calendar.getInstance());
		i1.setValue(12);
		i1.setBusinessType("A03");
		i2.setBusinessType("A04");
		i2.setTimeFrom(Calendar.getInstance());
		i2.setTimeTo(Calendar.getInstance());
		i1.setCreationTime(Calendar.getInstance());
		i2.setCreationTime(Calendar.getInstance());
		EnDays_type0[] en0 = new EnDays_type0[2];
		en0[0] = EnDays_type0.Monday;
		en0[1] = EnDays_type0.Saturday;
		EnDays days = new EnDays();
		days.setEnDays_type0(en0);
		i1.setDays(days);		
		i2.setDays(days);		
		i1.setTimeSeries(createTimeSeries(34.67));
		i2.setTimeSeries(createTimeSeries(800));
		i1.setItemVariables(createItemVariableData());
		i2.setItemVariables(createItemVariableData());
		
		
		items[0] = i1;
		items[1] = i2;
		
		return items;
		
	}
	
	public ArrayOfItemVariableData createItemVariableData(){
		ArrayOfItemVariableData arrVd = new ArrayOfItemVariableData();
		ItemVariableData[] arr = new ItemVariableData[1];
		ItemVariableData vd = new ItemVariableData();
		vd.setCapacityContract("SOME_LONG_STRING");
		vd.setDate(Calendar.getInstance());
		vd.setCapacityContractType("A04");
		vd.setId(1);
		arr[0] = vd;
		arrVd.setItemVariableData(arr);
		return arrVd;
	}
		
	public TimeSeries createTimeSeries(double val){
		TimeSeries ts = new TimeSeries();
		ts.setId(1);
		ts.setSeriesType(EnTimeSeriesPeriodType.Min60);
		ts.setValues(createValues(val));
		return ts;
	}
	
	public ArrayOfKeyValueOfdateTimedouble createValues(double val){
		ArrayOfKeyValueOfdateTimedouble arr = new ArrayOfKeyValueOfdateTimedouble();
		KeyValueOfdateTimedouble_type0[] kvPairs = new KeyValueOfdateTimedouble_type0[24];
		for (int i = 0; i < 24; i++){
			KeyValueOfdateTimedouble_type0 kv = new KeyValueOfdateTimedouble_type0();
			kv.setKey(Calendar.getInstance());
			kv.setValue(val);
			kvPairs[i] = kv;
		}
		arr.setKeyValueOfdateTimedouble(kvPairs);
		return arr;
	}

}
