package com.poker.control.delegate;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import com.poker.analyst.AnalystResult;
import com.poker.control.PokerControl;
import com.poker.data.PokerData;
import com.poker.ui.robot.reaction.UIReaction;
import com.poker.ui.settings.rooms.events.EventType;
import com.poker.ui.windows.PlayWindow;
import com.poker.ui.windows.Window;
import com.poker.ui.windows.api.Winwrk;
import com.poker.ui.windows.auxiluary.WindowPosition;
public class WinwrkDelegate {
public static Winwrk wwk = new Winwrk();
public WinwrkDelegate() {
wwk = new Winwrk();
}
public String getHandles(final Integer processID) {
return wwk.getHandles(processID);
}
@Deprecated
public Boolean updateWindowsFromHandles(final PokerData pdata, final Integer[] handles) {
killDeadTablesFromPosition(pdata, handles);
if (handles == null) {
pdata.setMainWnd(0, null);
return false;
}
if (pdata.getMainWnd() == null) {
RegMainWindow(pdata, handles);
} else {
RegWindowsToPositions(pdata, handles);
}
return true;
}
public Boolean updateWindowsFromWaitingList(final PokerData pdata) {
PlayWindow playWnd;
WindowPosition wpos;
if (pdata.getWaitingWindowList().size() > 0) {
playWnd = pdata.getWaitingWindowList().get(0);
if (playWnd.getCurrentEvent() == EventType.ET_READY_FOR_GAME) {
wpos = GetFirstFreeWinPosition(pdata);
if (wpos == null) {
return false;
}
wpos.setPlayWindow(playWnd);
playWnd.setCurrentPosition(wpos);
movePlayWindow(pdata, wpos.getPlayWindow());
pdata.getWaitingWindowList().remove(0);
}
}
return true;
}
public boolean updateWaitingListFromHandles(final PokerData pdata, final Integer[] handles) {
killDeadTablesFromWaitingList(pdata, handles);
if (handles == null) {
pdata.setMainWnd(0, null);
return false;
}
RegWindowsToWaitingList(pdata, handles);
return true;
}
public Integer getProcID(final String processName) {
return wwk.getProcID(processName);
}
public Integer[] getOpenWindowsHandles(final Integer processID) {
final String strHandles = getHandles(processID);
//System.out.println("h: " + strHandles);
if (strHandles == null) {
return null;
}
final String[] sHandles = strHandles.split(",");
if (sHandles.length == 0 || sHandles[0].trim().isEmpty()) {
return null;
}
final Integer[] iHandles = new Integer[sHandles.length];
for (int i = 0; i < sHandles.length; i++) {
iHandles[i] = new Integer(Integer.parseInt(sHandles[i]));
}
return iHandles;
}
public boolean killAllTablesFromWaitingList(final PokerData pdata, final PokerControl pControl) {
for (final PlayWindow playWnd : pdata.getWaitingWindowList()) {
pControl.killTableFromWaitingList(pdata, pControl, playWnd);
}
pdata.getWaitingWindowList().clear();
return true;
}
public boolean killTableFromWaitingList(final PokerData pdata, final PokerControl pControl,
final PlayWindow playWnd) {
try {
pControl.activateWindow(playWnd);
Thread.sleep(new Integer(pdata.getProgramSettings().getDelayPrepareThreadMs() / 6));
pControl.react(pdata, playWnd, new AnalystResult(UIReaction.UIR_REJECT_BUYIN, null));
} catch (final Exception e) {
//
e.printStackTrace();
}
return true;
}
public boolean killDeadTablesFromWaitingList(final PokerData pdata, final Integer[] handles) {
Integer hWnd;
boolean bWndInHandles;
PlayWindow playWnd;
final List<PlayWindow> waitinglist = pdata.getWaitingWindowList();
final List<PlayWindow> listtmp = new ArrayList<PlayWindow>();
for (int i = 0; i < waitinglist.size(); i++) {
playWnd = waitinglist.get(i);
hWnd = playWnd.gethWnd();
bWndInHandles = false;
if (handles == null) {
bWndInHandles = false;
} else {
for (final Integer tmp : handles) {
if (tmp.equals(hWnd)) {
bWndInHandles = true;
}
}
}
if (!bWndInHandles) {
listtmp.add(playWnd);
System.out.println(" The Window number " + playWnd.gethWnd()
+ " was delete from waiting list");
}
}
waitinglist.removeAll(listtmp);
return true;
}
public boolean killDeadTablesFromPosition(final PokerData pdata, final Integer[] handles) {
WindowPosition wpos;
Integer hWnd;
boolean bWndInHandles;
for (int i = 0; i < pdata.getWindowsPos().size(); i++) {
wpos = pdata.getWindowsPos().get(i);
if (wpos.getPlayWindow() != null) {
hWnd = wpos.getPlayWindow().gethWnd();
bWndInHandles = false;
if (handles == null) {
bWndInHandles = false;
} else {
// System.out.println(handles);
for (final Integer tmp : handles) {
if (tmp.equals(hWnd)) {
bWndInHandles = true;
}
}
}
if (!bWndInHandles) {
wpos.setPlayWindow(null);
System.out.println(" The Position number " + wpos.getPosNum() + " is free now");
}
}
}
return true;
}
public boolean RegMainWindow(final PokerData pdata, final Integer[] handle) {
if (handle.length != 1) {
return false;
}
pdata.setMainWnd(handle[0], getWindowPosByHandle(handle[0]));
return true;
}
public WindowPosition FindPositionByHandle(final PokerData pdata, final Integer handle) {
WindowPosition wpos;
for (int i = 0; i < pdata.getWindowsPos().size(); i++) {
wpos = pdata.getWindowsPos().get(i);
if (wpos.getPlayWindow() != null && wpos.getPlayWindow().IsMineHwnd(handle)) {
return wpos;
}
}
return null;
}
public WindowPosition GetFirstFreeWinPosition(final PokerData pdata) {
for (int i = 0; i < pdata.getWindowsPos().size(); i++) {
if (pdata.getWindowsPos().get(i).getPlayWindow() == null) {
return pdata.getWindowsPos().get(i);
}
}
return null;
}
public void RegWindowsToWaitingList(final PokerData pdata, final Integer[] handles) {
WindowPosition wpos = null;
PlayWindow playWnd;
for (final Integer hWnd : handles) {
if (!pdata.getMainWnd().IsMineHwnd(hWnd)) {
wpos = FindPositionByHandle(pdata, hWnd);
if (wpos == null) {
if (FindWindowInWaitingList(pdata, hWnd) == null) {
playWnd = RegWindowToWaitingList(pdata, hWnd);
playWnd.setCurrentEvent(EventType.ET_NEED_BUYIN);
playWnd.setLeftTop(getWindowPosByHandle(hWnd));
System.out.println("" + hWnd + " was regged to WaitingList");
}
}
}
}
}
public PlayWindow FindWindowInWaitingList(final PokerData pdata, final Integer hWnd) {
for (final PlayWindow playWnd : pdata.getWaitingWindowList()) {
if (playWnd.IsMineHwnd(hWnd)) {
return playWnd;
}
}
return null;
}
public PlayWindow RegWindowToWaitingList(final PokerData pdata, final Integer handle) {
PlayWindow playWnd;
playWnd = new PlayWindow(handle, pdata.getProgramSettings().getCountPlayers());
pdata.getWaitingWindowList().add(playWnd);
return playWnd;
}
public void RegWindowsToPositions(final PokerData pdata, final Integer[] handles) {
WindowPosition wpos = null;
for (final Integer hWnd : handles) {
if (!pdata.getMainWnd().IsMineHwnd(hWnd)) {
wpos = FindPositionByHandle(pdata, hWnd);
if (wpos == null) {
wpos = RegWindowToPosition(pdata, hWnd);
if (wpos != null) {
System.out.println("" + hWnd + " was regged to position "
+ wpos.getPosNum());
movePlayWindow(pdata, wpos.getPlayWindow());
} else {
System.out.println("" + hWnd + " can't be regged to position ");
}
}
}
}
}
@Deprecated
public WindowPosition RegWindowToPosition(final PokerData pdata, final Integer handle) {
WindowPosition wpos = FindPositionByHandle(pdata, handle);
PlayWindow playWnd;
if (wpos == null) {
wpos = GetFirstFreeWinPosition(pdata);
if (wpos == null) {
return null;
}
playWnd = new PlayWindow(handle, pdata.getProgramSettings().getCountPlayers());
wpos.setPlayWindow(playWnd);
playWnd.setCurrentPosition(wpos);
}
return wpos;
}
public boolean movePlayWindow(final PokerData pdata, final PlayWindow playWnd) {
int topX = -1;
int topY = -1;
final WindowPosition wpos = playWnd.getCurrentPosition();
/*
if (wpos != null) {
topX = pdata.getComputerSettings().getIntAreaForPlayWindowsX() + wpos.getX()
* pdata.getRoomSettings().getPlayWndWidth()
+ pdata.getProgramSettings().getxDelay();
topY = pdata.getComputerSettings().getIntAreaForPlayWindowsY() + wpos.getY()
* pdata.getRoomSettings().getPlayWndHeight()
+ pdata.getProgramSettings().getyDelay();
wwk.bitResizeWindow(playWnd.gethWnd());
wwk.moveWindow(playWnd.gethWnd(), topX, topY,
pdata.getRoomSettings().getPlayWndWidth(), pdata.getRoomSettings()
.getPlayWndHeight());
}*/
if (wpos != null) {
topX = pdata.getComputerSettings().getIntAreaForPlayWindowsX() + wpos.getX()
* pdata.getRoomSettings().getPlayWndWidthDelta()
+ pdata.getProgramSettings().getxDelay();
topY = pdata.getComputerSettings().getIntAreaForPlayWindowsY() + wpos.getY()
* pdata.getRoomSettings().getPlayWndHeightDelta()
+ pdata.getProgramSettings().getyDelay();
wwk.bitResizeWindow(playWnd.gethWnd());
wwk.moveWindow(playWnd.gethWnd(), topX, topY,
pdata.getRoomSettings().getPlayWndWidthDelta(), pdata.getRoomSettings()
.getPlayWndHeightDelta());
}
playWnd.setLeftTop(new Point(topX, topY));
return true;
}
public boolean activateWindow(final Window playWnd) {
//if (playWnd.getCurrentPosition() != null){
wwk.activateWindow(playWnd.gethWnd());
//}
return true;
}
public Point getWindowPosByHandle(final int handle) {
//System.out.println("WindowPosIs!!!! " );
final String str = wwk.getCoords(handle);
int posX;
int posY;
Point pt;
try {
posX = Integer.parseInt(str.split(",")[0]);
posY = Integer.parseInt(str.split(",")[1]);
pt = new Point(posX, posY);
} catch (final Exception e) {
e.printStackTrace();
return null;
}
return pt;
}
}