package com.bocloud.ims.service.resource;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.NetworkType;
import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.Network;
import com.bocloud.ims.entity.resource.NetworkHost;
import com.bocloud.ims.entity.resource.PortGroup;
import com.bocloud.ims.entity.resource.Switcher;
import com.bocloud.ims.interfaces.resource.PortGroupService;
import com.bocloud.ims.repository.resource.NetworkHostRepository;
import com.bocloud.ims.repository.resource.NetworkRepository;
import com.bocloud.ims.repository.resource.PortGroupRepository;
import com.bocloud.ims.repository.resource.SwitcherRepository;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 端口组service接口实现类
 *
 * @author wangyu
 * @version 1.0
 * @since 2017年12月25日
 */
@Service("portGroupService")
public class PortGroupServiceImpl implements PortGroupService {

	private static Logger logger = LoggerFactory.getLogger(PortGroupServiceImpl.class);
	@Autowired
	private PortGroupRepository portGroupRepository;
	@Autowired
	private SwitcherRepository switcherRepository;
	@Autowired
	private NetworkHostRepository networkHostRepository;
	@Autowired
	private NetworkRepository networkRepository;

	@Override
	public BocloudResult<GridBean<PortGroup>> list(Pager pager) {
		try {
			int page = pager.getPage();
			int rows = pager.getRows();
			List<Param> params = pager.getParams();
			Map<String, String> sorter = pager.getSorter();
			boolean simple = pager.getSimple();

			GridBean gridBean = null;
			if (null == params) {
				params = new ArrayList<>();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			sorter.put("gmtCreate", Common.ONE);
			int total = portGroupRepository.count(params);
			if (simple) {
				List<SimpleBean> beans = portGroupRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				List<PortGroup> list = portGroupRepository.list(page, rows, params, sorter);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list port group failure:", e);
			return new BocloudResult<>(false, "查询失败", null, null);
		}
	}

	@Override
	public BocloudResult<List<PortGroup>> condition(String condition) {
		try {
			JSONObject object = JSONObject.parseObject(condition);
			if ("listByHostId".equalsIgnoreCase(object.getString("condition"))) {
				return this.listByHostId(object.getLong("hostId"));
			} else if ("listByVendorId".equalsIgnoreCase(object.getString("condition"))) {
				List<PortGroup> groups = portGroupRepository.listByVendorId(object.getLong("vendorId"));
				return new BocloudResult<>(true, groups, "查询成功");
			}
			return  new BocloudResult<>(false, "查询失败");
		} catch (Exception e) {
			logger.error("list port group failure:", e);
			return new BocloudResult<>(false, "查询失败", null, null);
		}
	}

	@Override
	public BocloudResult<List<PortGroup>> listByHostId(Long hostId) {
		try {
			// 查询虚拟机端口组
			List<PortGroup> groups = Lists.newArrayList();
			List<Switcher> switchers = switcherRepository.listByHostId(hostId);
			for (Switcher switcher : switchers) {
				List<PortGroup> portGroups = portGroupRepository.listBySwitcher(switcher.getId());
				for (PortGroup portGroup : portGroups) {
					if (portGroup.getType().equals(NetworkType.PortGroupType.VIRTUAL.name())) {
						portGroup.setSwitchType(NetworkType.SwitchType.VSWITCH.name());
						groups.add(portGroup);
					}
				}
			}
			List<NetworkHost> hosts = networkHostRepository.listByVhostId(hostId, "PM");
			// 查询分布式端口组
			for (NetworkHost host : hosts) {
				Network network = networkRepository.query(host.getNetworkId());
				if (null != network && null != network.getSwitcherId() && 0L != network.getSwitcherId()) {
					List<PortGroup> portGroups = portGroupRepository.listBySwitcher(network.getSwitcherId());
					for (PortGroup portGroup : portGroups) {
						if (portGroup.getType().equals(NetworkType.PortGroupType.DVPORT.name())) {
							Switcher switcher = switcherRepository.query(portGroup.getSwitcherId());
							portGroup.setName(portGroup.getName() + "(" + network.getName() + ")");
							portGroup.setSwitchName(switcher.getName());
							portGroup.setSwitchType(switcher.getType());
							groups.add(portGroup);
						}
					}
				}
			}
			return new BocloudResult<>(true, groups, "查询成功");
		} catch (Exception e) {
			logger.error("Get error message:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

}
