package com.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.platform.dao.VolumeDao;
import com.platform.entities.Brick;
import com.platform.entities.VolumeDataEntity;
import com.platform.entities.VolumeInitEntity;
import com.platform.http.gfs.SetVolume;
import com.platform.service.IGfsService;
import com.platform.service.IVolumeService;

@Service(value = "volumeService")
public class VolumeServiceImpl implements IVolumeService {
	
	public final static Logger log = Logger.getLogger(VolumeServiceImpl.class);
	
	/**	 gfs的 web 服务的 api 	 */
	SetVolume setVolume = new SetVolume();
	
	@Resource(name = "gfsService")
	private IGfsService gfsService;
	
	@Resource(name = "volumeDao")
	private VolumeDao volumeDao;
	

	@Override
	public int save(VolumeDataEntity entity) throws Exception {
		int rest = 1;
		//createVolume("lili_test1", 0, "distributed", bricksToCreate, "/home/lili_test1_point")
		List<VolumeInitEntity> vols = volumeDao.findAll();
		Map<String, Object> volMap = new HashMap<String, Object>();
		if (null!= vols) {
			for (VolumeInitEntity volumeInitEntity : vols) {
				volMap.put(volumeInitEntity.getPath(), null);
			}
		}
		List<VolumeDataEntity> result = gfsService.getAllVolumes();
		List<VolumeDataEntity> addVolumes = new ArrayList<VolumeDataEntity>();
		VolumeDataEntity volumeTmp = null;
		if (null != result) {
			boolean isExits = false;
			//.trim() 去掉空格
			for (VolumeDataEntity volumeOnServer : result) {
				if(entity.getName().trim().equals(volumeOnServer.getName().trim())){
					isExits = true;
					//TODO 服务器上有该volume，
					volumeTmp = volumeOnServer;
					break;
				}
			}
			//如果服务器上没有该volume，
			if (!isExits) {
				addVolumes.add(entity);
			}
			
		}
		boolean isContinue = true;
		// 对比volume信息--原数据没有该volume，则新增volume:
		for (VolumeDataEntity add : addVolumes) {
			List<String> bristr = new ArrayList<String>();
			if (null == add.getName() || "".equals(add.getName())) {
				continue;
			}
			if (null == add.getPath() || "".equals(add.getPath())) {
				continue;
			}
			for ( Brick bri : add.getBrick()) {
				if (null == bri.getIp() || "".equals(bri.getIp())) {
					continue;
				}
				if (null == bri.getPath() || "".equals(bri.getPath())) {
					continue;
				}
				bristr.add(bri.getIp()+":"+bri.getPath());
			};
			
			//创建volume
			if (bristr.size() > 0) {
				//TODO 换成 web 请求
				int createreslt = setVolume.createVolume(add.getName(), 0, "Distribute", bristr, add.getPath());
				if (createreslt != 1) {
					rest = createreslt;
				}
			}
			//记录volume信息，
			VolumeInitEntity volInSql = new VolumeInitEntity();
			volInSql.setName(add.getName());
			volInSql.setPath(add.getPath());
			if (!volMap.keySet().contains(volInSql.getPath())) {
				volumeDao.save(volInSql);
			}
			isContinue = false;
		}
		// 修改 brick
		if (isContinue && null != volumeTmp) {
			List<Brick> newBricks = entity.getBrick();
			List<Brick> serverBricks = volumeTmp.getBrick();
			Map<String, Brick> newMap = new HashMap<String, Brick>();
			Map<String, Brick> serverMap = new HashMap<String, Brick>();
			for (Brick brick : newBricks) {
				//IP + path 才 确认唯一的 brick
				newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
			}
			for (Brick brick : serverBricks) {
				serverMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
			}
			// ----对比volume信息--原数据有该volume，则对比brick信息，确认那几个brick是新增的，那几个brick是待删除的----start
			Set<String> newBrickKeys = newMap.keySet();
			Set<String> serverBrickKeys = serverMap.keySet();
			//新增的Brick的 Keys
			List<String> newKeys = new ArrayList<String>();
			newBrickKeys.removeAll(serverBrickKeys);
			newKeys.addAll(newBrickKeys);
			// 新增brick s，
			if (newKeys.size() > 0) {
				//TODO 换成 web 请求
				int createreslt = setVolume.addBrickVolume(entity.getName(), newKeys, 0, "distributed");
				if (createreslt != 1) {
					rest = createreslt;
				}
			}
			//待删除的Brick的 Keys
			for (Brick brick : newBricks) {
				//IP + path 才 确认唯一的 brick
				newMap.put(brick.getIp().trim() + ":" + brick.getPath().trim(), null);
			}
			newBrickKeys = newMap.keySet();
			serverBrickKeys.removeAll(newBrickKeys);
			List<String> deleteKeys = new ArrayList<String>();
			deleteKeys.addAll(serverBrickKeys);
			// 删除brick s，
			if (deleteKeys.size() > 0) {
				//TODO 换成 web 请求
				int createreslt = setVolume.deleteBrickVolume(entity.getName(), deleteKeys, 0, "distributed");
				if (createreslt != 1) {
					rest = createreslt;
				}
			}
			// ---对比volume信息--原数据有该volume，则对比brick信息，确认那几个brick是新增的，那几个brick是待删除的----- end
		
			//TODO 查看状态 状态Started,Stopped,Created 
			if (volumeTmp.isStatus() != entity.isStatus()) {
				if (entity.isStatus()) {
					this.start(entity);
				}
				else{
					this.stop(entity);
				}
			}
		}
		return rest;
	}

	@Override
	public int delete(VolumeDataEntity entity) throws Exception {
		if (null == entity.getName() || "".equals(entity.getName())) {
			return -1;
		}
		//TODO 换成 web 请求
		int rest = setVolume.deleteVolume(entity.getName());
		volumeDao.remove(entity.getName());
		return rest;
	}

	@Override
	public int start(VolumeDataEntity entity) throws Exception {
		if (null == entity.getName() || "".equals(entity.getName())) {
			return -1;
		}
		//TODO 换成 web 请求
		return setVolume.startVolume(entity.getName());
	}

	@Override
	public int stop(VolumeDataEntity entity) throws Exception {
		if (null == entity.getName() || "".equals(entity.getName())) {
			return -1;
		}
		//TODO 换成 web 请求
		return setVolume.stopVolume(entity.getName());
	}

	@Override
	public String getAllvolume() throws Exception {
		return setVolume.getAllvolume();
	}

}
