package com.bocloud.ims.service.resource;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.Sign;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.Image;
import com.bocloud.ims.entity.resource.Network;
import com.bocloud.ims.entity.resource.Vdc;
import com.bocloud.ims.interfaces.resource.IaasService;
import com.bocloud.ims.repository.resource.CloudServerRepository;
import com.bocloud.ims.repository.resource.ClusterRepository;
import com.bocloud.ims.repository.resource.DataStoreRepository;
import com.bocloud.ims.repository.resource.ImageRepository;
import com.bocloud.ims.repository.resource.NetworkRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import com.bocloud.ims.repository.resource.VdcRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

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

/**
 * Iaas资源查询service实现
 *
 * @author wangyu
 * @version 3.0.0-SNAPSHOT
 * @since 2018/5/10
 */
@Service("iaasService")
public class IaasServiceImpl implements IaasService {

	private static Logger logger = LoggerFactory.getLogger(IaasServiceImpl.class);
	@Autowired
	private ImageRepository imageRepository;
	@Autowired
	private NetworkRepository networkRepository;
	@Autowired
	private VdcRepository vdcRepository;
	@Autowired
	private CloudServerRepository cloudServerRepository;
	@Autowired
	private ServerRepository serverRepository;
	@Autowired
	private DataStoreRepository dataStoreRepository;
	@Autowired
	private ClusterRepository clusterRepository;

	@Override
	public BocloudResult list(JSONObject object) {
		try {
			String resType = object.getString("restype");
			Long vendorId = object.getLong("vendorId");
			if (null == vendorId) {
				vendorId = -1L;
			}
			Long tenantId = object.getLong("tenantId");
			if (null == tenantId) {
				tenantId = 0L;
			}
			List<JSONObject> list = Lists.newArrayList();
			List<Param> params = Lists.newArrayList();
			Map<String, Object> paramMap = Maps.newHashMap();
			switch (resType) {
			case "IMAGE":
				List<Image> images = imageRepository.listByVendorId(vendorId);
				for (Image image : images) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", image.getId());
					if (!StringUtils.isEmpty(image.getDefaultUser())) {
						jsonObject.put("name", image.getName() + "(默认账号:)" + image.getDefaultUser());
					} else {
						jsonObject.put("name", image.getName());
					}
					list.add(jsonObject);
				}
				break;
			case "NETWORK":
				List<Network> networks = networkRepository.listByVid(vendorId);
				for (Network network : networks) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", network.getId());
					jsonObject.put("name", network.getName());
					list.add(jsonObject);
				}
				break;
			case "VDC":
				List<Vdc> vdcs = vdcRepository.listByVendor(vendorId);
				for (Vdc vdc : vdcs) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", vdc.getId());
					jsonObject.put("name", vdc.getName());
					list.add(jsonObject);
				}
				break;
			case "VM":
				params.clear();
				paramMap.put("vendorId", object.getLong("vendorId"));
				paramMap.put("vdcId", object.getLong("vdcId"));
				params.add(new Param(paramMap, Sign.EQ));
				List<SimpleBean> vms = cloudServerRepository.list(params, new HashMap<>(), null, null);
				for (SimpleBean vm : vms) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", vm.getId());
					jsonObject.put("name", vm.getName());
					list.add(jsonObject);
				}
				break;
			case "CLUSTER":
				params.clear();
				paramMap.put("vendorId", object.getLong("vendorId"));
				paramMap.put("vdcId", object.getLong("vdcId"));
				params.add(new Param(paramMap, Sign.EQ));
				List<SimpleBean> clusters = clusterRepository.list(params, new HashMap<>());
				for (SimpleBean cluster : clusters) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", cluster.getId());
					jsonObject.put("name", cluster.getName());
					list.add(jsonObject);
				}
				break;
			case "DATASTORE":
				params.clear();
				paramMap.put("vendorId", object.getLong("vendorId"));
				paramMap.put("vhostId", object.getLong("vhostId"));
				paramMap.put("category", object.getLong("PM"));
				params.add(new Param(paramMap, Sign.EQ));
				params.add(new Param(MapTools.simpleMap("status", object.getLong("inactive")), Sign.UEQ));
				List<SimpleBean> datastores = dataStoreRepository.list(params, new HashMap<>());
				for (SimpleBean datastore : datastores) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", datastore.getId());
					jsonObject.put("name", datastore.getName());
					list.add(jsonObject);
				}
				break;
			case "SERVER":
				params.clear();
				paramMap.put("vendorId", object.getLong("vendorId"));
				paramMap.put("vdcId", object.getLong("vdcId"));
				params.add(new Param(paramMap, Sign.EQ));
				List<SimpleBean> servers = serverRepository.list(params, new HashMap<>(), null);
				for (SimpleBean server : servers) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", server.getId());
					jsonObject.put("name", server.getName());
					list.add(jsonObject);
				}
				break;
			case "PUBLICNET":
				params.clear();
				paramMap.put("vendorId", object.getLong("vendorId"));
				paramMap.put("isRouterExternal", true);
				params.add(new Param(paramMap, Sign.EQ));
				List<SimpleBean> pubnets = networkRepository.list(params, new HashMap<>(), tenantId);
				for (SimpleBean pubnet : pubnets) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id", pubnet.getId());
					jsonObject.put("name", pubnet.getName());
					list.add(jsonObject);
				}
				break;
			default:
				break;
			}
			return new BocloudResult<>(true, list, "查询成功");
		} catch (Exception e) {
			logger.error("Get error message：", e);
			return new BocloudResult<>(true, "查询失败");
		}
	}
}
