package jp.ac.kyutech.mns.ist.util;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JFrame;

public class MultiDisplay {
	public static ArrayList<Rectangle> getMultiDisplayRectList(){
		ArrayList<Rectangle> displaySizeList = new ArrayList<Rectangle>();

		GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
		for(GraphicsDevice gd: devices){
			//			System.out.println(gd.getIDstring());
			GraphicsConfiguration gc = gd.getDefaultConfiguration();
			Rectangle rec = gc.getBounds();
			displaySizeList.add(rec);
		}
		return displaySizeList;
	}

	/* 右なら１，下なら３ */
	public static void alineWindow(JFrame base, JFrame target, int direction, boolean isresize){
		int bx = base.getX();
		int by = base.getY();
		int bw = base.getWidth();
		int bh = base.getHeight();
		if (direction==1){
			target.setLocation(bx+bw,by);
			if (isresize) target.setSize(target.getWidth(), bh);
		} else if (direction==3){
			target.setLocation(bx,by+bh);
			if (isresize) target.setSize(bw, target.getHeight());
		} 
	}
	public static void moveWindow(JFrame base, JFrame f, boolean doMoveOnOtherDisplay, boolean fullsize){
		ArrayList<Rectangle> dispList = MultiDisplay.getMultiDisplayRectList();
		int findBaseWinDisplay = 0;
		int findTargetWinDisplay = 0;
		int destinationDisplay = 0;
		if (dispList.size()>1){
			for(int i=0;i<dispList.size();i++){
				if (dispList.get(i).contains(base.getLocation())){
					findBaseWinDisplay = i;
					break;
				}
			}
			for(int i=0;i<dispList.size();i++){
				if (dispList.get(i).contains(f.getLocation())){
					findTargetWinDisplay = i;
					break;
				}
			}
			if (doMoveOnOtherDisplay) {
				destinationDisplay = 1-findBaseWinDisplay;
			} else {
				destinationDisplay = findBaseWinDisplay;
			}
		}

		if (fullsize){
			f.setLocation(dispList.get(destinationDisplay).getLocation());
			f.setSize(dispList.get(destinationDisplay).getSize());
		} else {
			int owx = dispList.get(findTargetWinDisplay).getLocation().x;
			int owy = dispList.get(findTargetWinDisplay).getLocation().y;
//			int oww = dispList.get(findTargetWinDisplay).getSize().width;
//			int owh = dispList.get(findTargetWinDisplay).getSize().height;
			
//			int ox = f.getLocation().x;
//			int oy = f.getLocation().y;
//			int ow = f.getSize().width;
//			int oh = f.getSize().height;
			
//			float xposrate = ox/oww;
//			float yposrate = oy/owh;
//			float wrate = ow/oww;
//			float hrate = oh/owh;
			
			int dwx = dispList.get(destinationDisplay).getLocation().x;
			int dwy = dispList.get(destinationDisplay).getLocation().y;
			int dww = dispList.get(destinationDisplay).getSize().width;
			int dwh = dispList.get(destinationDisplay).getSize().height;
			
//			f.setLocation((int)(dwx+xposrate*dww), (int)(dwy+yposrate*dwh));
//			f.setSize((int)(ow*wrate),(int)(oh*hrate));
			f.setLocation((int)(owx+dwx+dww/16), (int)(owy+dwy+dwh/16));
			f.setSize((int)(dww*4/5),(int)(dwh*4/5));
		}
	}
	public static void changeDisplayOfWindow(JFrame base, JFrame f, boolean doMoveOnOtherDisplay){
		ArrayList<Rectangle> dispList = MultiDisplay.getMultiDisplayRectList();
		int findBaseWinDisplay = 0;
		int destinationDisplay = 0;
		if (dispList.size()>1){
			for(int i=0;i<dispList.size();i++){
				if (dispList.get(i).contains(base.getLocation())){
					findBaseWinDisplay = i;
					break;
				}
			}
			if (doMoveOnOtherDisplay) {
				destinationDisplay = 1-findBaseWinDisplay;
			} else {
				destinationDisplay = findBaseWinDisplay;
			}
		}
		
		int x = dispList.get(destinationDisplay).getLocation().x;
		int y = dispList.get(destinationDisplay).getLocation().y;
		int w = dispList.get(destinationDisplay).getSize().width;
		int h = dispList.get(destinationDisplay).getSize().height;
		f.setLocation(x+w/5, y+h/5);
		f.setSize(w/2,h/2);
	}
}
