package com.cim.idm.mwmsextend.storagespec.service;

import java.util.HashMap;
import java.util.Map;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.wmspackage.storage.StorageServiceProxy;
import com.cim.idm.wmspackage.storage.management.data.StorageKey;
import com.cim.idm.wmspackage.storage.management.data.StorageSpecKey;
import com.cim.idm.wmspackage.storage.management.info.CreateSpecInfo;
import com.cim.idm.wmspackage.storage.management.info.RemoveSpecInfo;
import com.cim.idm.wmspackage.storage.management.info.SetEventInfo;
import com.cim.idm.wmspackage.storage.management.info.SetSpecEventInfo;

public class StorageSpecServiceImpl {

	public void create(EventInfo eventInfo, CreateSpecInfo locationData)
	{		
		StorageServiceProxy.getStorageSpecService().create(eventInfo, locationData);
	}
	
	public void modify(EventInfo eventInfo, String siteName, String storageName , String resourceState, String useState, String capacity)
	{
		SetEventInfo setEventInfo = new SetEventInfo();
		
		StorageKey storageKey = new StorageKey();
		
		storageKey.setSiteName(siteName);
		storageKey.setStorageName(storageName);	
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		
		bindMap.put("resourceState", resourceState);
		bindMap.put("useState", useState);
		bindMap.put("packingName","");
//		bindMap.put("capacity", Long.parseLong(capacity));

		setEventInfo.setUserColumns(bindMap);

		StorageServiceProxy.getStorageService().setEvent(storageKey, eventInfo, setEventInfo);
		
		if(capacity != "")
		{
				
		String sql = "update storageSpec set capacity = :capacity where storagename = :storageName";
		Map<String, Object> args = new HashMap<String, Object>();
		
		args.put("capacity", Long.parseLong(capacity));
		args.put("storageName", storageName);
		
		IDMFrameServiceProxy.getSqlTemplate().update(sql, args);
		}
		
	}
	
	public void remove(EventInfo eventInfo, String siteName, String storageName)
	{
		
		StorageSpecKey storageSpecKey = new StorageSpecKey();
		
		storageSpecKey.setSiteName(siteName);
		storageSpecKey.setStorageName(storageName);
				
		StorageServiceProxy.getStorageSpecService().delete(storageSpecKey);
		
		StorageKey storageKey=new StorageKey();
		storageKey.setSiteName(siteName);
		storageKey.setStorageName(storageName);
				
		StorageServiceProxy.getStorageService().delete(storageKey);
	}
	
public CreateSpecInfo makeLocationData(String siteName, String storageName, String areaName,String storageusetype,String superarea,String capacity, String resourceState){
		
		CreateSpecInfo locationData = new CreateSpecInfo();
	
		locationData.setSiteName(siteName);	
		locationData.setStorageName(storageName);
		locationData.setAreaName(areaName);
		locationData.setStorageUseType(storageusetype);
		locationData.setStorageOperationType("Manual");
		locationData.setCapacity(Long.parseLong(capacity));
		locationData.setResourceState(resourceState);
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("superAreaName", superarea);
		locationData.setUserColumns(bindMap);
		
		return locationData;
	}
}
