package szakdolgozat.client.ui;

import szakdolgozat.client.UI;
import szakdolgozat.shared.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;

public class GameProposalDialog extends DialogBox {

	private long startTime;

	public GameProposalDialog(final User from, final User to) {
		startTime = System.currentTimeMillis();
		setAnimationEnabled(true);
		setGlassEnabled(true);

		FlexTable flexTable = new FlexTable();
		setWidget(flexTable);
		flexTable.setSize("100%", "100%");

		Label label = new Label("Kedves " + UI.currentUser.getUsername()
				+ ", \"" + from.getUsername()
				+ "\" nevű játékos\n játszani szeretne veled. Elfogadod?");
		flexTable.setWidget(0, 0, label);

		DisclosurePanel disclosurePanel = new DisclosurePanel(from
				.getUsername()
				+ " statisztikái");
		flexTable.setWidget(1, 1, disclosurePanel);
		UserStatsWindow usw = new UserStatsWindow();
		usw.setUserStats(from);
		disclosurePanel.add(usw);

		Button acceptButton = new Button("Igen");
		acceptButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				hide();
				informOtherPlayer(true);
				GameDialog gd = new GameDialog();
				gd.opponent = from;
				gd.center();
				gd.colorButtons();
				gd.setTitle("Ellenfél lép!");
				gd.isMineTurn = false;
				UI.servicesHandler.gameStarted(from, to, new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});
		flexTable.setWidget(2, 0, acceptButton);

		Button refuseButton = new Button("Nem");
		refuseButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				informOtherPlayer(false);
				hide();
				UI.userListWindow.userListGrid.show();

			}
		});
		flexTable.setWidget(2, 2, refuseButton);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(2, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(2, 2,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(2, 2,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(1, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setVerticalAlignment(1, 1,
				HasVerticalAlignment.ALIGN_MIDDLE);

		showFor30Sec(this);
	}

	private void showFor30Sec(final GameProposalDialog gameProposalDialog) {
		Timer t = new Timer() {

			@Override
			public void run() {
				if (System.currentTimeMillis() - startTime > 30000)
					gameProposalDialog.hide();
				else
					this.schedule(1000);

			}
		};		
		t.schedule(1);
	}

	public static void informOtherPlayer(boolean b) {
		UI.servicesHandler.replyToProposal(UI.currentUser, b,
				new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFailure(Throwable caught) {
						System.err.println(caught.getLocalizedMessage());

					}
				});

	}	
}
