/**
 * <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 FanLayout implements Layout {

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

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

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

    private int caculateWidth(Map<String, HostGroup> groups) throws Exception {
        return HEIGHT_PER_GROUP;
    }

    @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();
            double per = Math.PI * 2 / size;
            //            for (int i = 0; i < size; i++) {
            //
            //                list.add(new Point(centerX + (int) (WIDTH_PER_NODE * Math.cose(per) * i),
            //                    centerY - centerX + (int) (WIDTH_PER_NODE * Math.sin(per) * i)));
            //
            //            }

            int hostNo = 0;
            for (Host host : group.getHosts()) {
                MapElementObject to = host.getMapElementObject();
                to.setX(centerX + (int) (WIDTH_PER_NODE * Math.cos(per * hostNo)));
                to.setY(centerY - (int) (WIDTH_PER_NODE * Math.sin(per * hostNo)));
                hostNo++;
            }
        }
        map.setHeight(caculateMapHeight(groups) + 20);
        map.setWidth(maxWidth);
    }
}