/**
 * <p>Copyright:Copyright(c) 2017</p>
 * <p>Company:上海中信信息发展股份有限公司</p>
 * <p>包名:com.cesgroup.cesmonitor.service.map</p>
 * <p>文件名:TwoColumnLayout.java</p>
 * <p>创建时间:2017年5月23日 上午9:52:30</p>
 * <p>作者:zhang.erwei</p>
 */

package com.cesgroup.cesmonitor.service.map;

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

import com.zabbix4j.map.MapElementObject;
import com.zabbix4j.map.MapUpdateRequest;

/**
 * 两列布局
 *
 * @author zhang.erwei
 * @version 1.0.0 2017年5月23日
 */
public class UmbrellaLayout implements Layout {

    /**
     * 单个组的高度
     */
    public static final int HEIGHT_PER_GROUP = 600;

    /**
     * 单个节点宽度
     */
    public static final int WIDTH_PER_NODE = 150;

    /**
     * 计算拓扑图高度
     *
     * @param groups
     *            主机组
     * @return 拓扑图高度
     * @throws Exception
     *             异常对象
     */
    private int caculateMapHeight(Map<String, HostGroup> groups) throws Exception {
        return UmbrellaLayout.HEIGHT_PER_GROUP * groups.size();
    }

    private int caculateWidth(Map<String, HostGroup> groups) throws Exception {
        int max = 2;
        for (String ipPrefix : groups.keySet()) {
            HostGroup hg = groups.get(ipPrefix);
            int c = hg.getHosts().size() / 2 + 1;
            if (max < c) {
                max = c;
            }
        }
        return (UmbrellaLayout.WIDTH_PER_NODE) * (max);
    }

    @Override
    public void doLayout(MapUpdateRequest.Params map, Map<String, HostGroup> groups) throws Exception {
        int maxWidth = caculateWidth(groups);
        int centerX = maxWidth / 2;
        int groupNo = 0;
        for (String ipPrefix : groups.keySet()) {
            groupNo++;
            HostGroup group = groups.get(ipPrefix);
            Host gateway = group.getGateway();
            MapElementObject from = null;

            int centerY = HEIGHT_PER_GROUP * (groupNo - 1) + (HEIGHT_PER_GROUP / 2);
            if (gateway != null) {
                from = gateway.getMapElementObject();
                from.setX(centerX);
                from.setY(centerY);
            }
            List<Point> list = new ArrayList<Point>();
            int size = group.getHosts().size();
            //            int startX = centerX - (UmbrellaLayout.WIDTH_PER_NODE) * (size / 4);
            //            for (int i = 0; i < size / 2 + 1; i++) {
            //                list.add(new Point(startX, centerY - 200));
            //                list.add(new Point(startX, centerY + 200));
            //                startX += UmbrellaLayout.WIDTH_PER_NODE;
            //            }
            for (int i = 0; i < size / 4 + 2; i++) {
                if (i == 0) {
                    list.add(new Point(centerX, centerY - 200));
                    list.add(new Point(centerX, centerY + 200));
                } else {
                    list.add(new Point(centerX - i * WIDTH_PER_NODE, centerY - 200));
                    list.add(new Point(centerX + i * WIDTH_PER_NODE, centerY - 200));

                    list.add(new Point(centerX - i * WIDTH_PER_NODE, centerY + 200));
                    list.add(new Point(centerX + i * WIDTH_PER_NODE, centerY + 200));
                }

            }
            int hostNo = 0;
            for (Host host : group.getHosts()) {
                MapElementObject to = host.getMapElementObject();
                to.setX(list.get(hostNo).getX());
                to.setY(list.get(hostNo).getY());
                hostNo++;
            }
        }
        map.setHeight(caculateMapHeight(groups) + 20);
        map.setWidth(maxWidth);
    }
}