package szakdolgozat.client.ui;

import szakdolgozat.client.Services;
import szakdolgozat.client.ServicesAsync;
import szakdolgozat.client.UI;
import szakdolgozat.shared.User;
import szakdolgozat.shared.UserStats;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UserStatsWindow extends Composite {

	private static Grid statsGrid;
	private Button closeTabButton;
	private VerticalPanel verticalPanel;

	public UserStatsWindow() {

		verticalPanel = new VerticalPanel();
		verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		initWidget(verticalPanel);

		Label lblSatisztikim = new Label("Statisztikák");
		lblSatisztikim.setStyleName("gwt-blueLabel");
		verticalPanel.add(lblSatisztikim);

		statsGrid = new Grid(6, 2);
		// statsGrid.setBorderWidth(1);
		statsGrid.setText(0, 0, "Bejelentkezések száma");
		statsGrid.setText(1, 0, "Játékok száma");
		statsGrid.setText(2, 0, "Megnyert játékok");
		statsGrid.setText(3, 0, "Elvesztett játékok");
		statsGrid.setText(4, 0, "Nyerési arány");
		statsGrid.setText(5, 0, "Átlagos játékszám bejelentkezésenként");
		verticalPanel.add(statsGrid);
		verticalPanel.setCellVerticalAlignment(statsGrid,
				HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHorizontalAlignment(statsGrid,
				HasHorizontalAlignment.ALIGN_CENTER);

	}

	public UserStatsWindow(boolean addCloseButton) {
		this();
		if (addCloseButton)
			addCloseButton();
		// TODO Auto-generated constructor stub
	}

	private void addCloseButton() {
		closeTabButton = new Button("New button");
		closeTabButton.setText("Fül bezárása");
		closeTabButton.setHeight("");
		closeTabButton.setStyleName("gwt-MenuItem-selected");
		verticalPanel.add(closeTabButton);
		verticalPanel.setCellVerticalAlignment(closeTabButton,
				HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHorizontalAlignment(closeTabButton,
				HasHorizontalAlignment.ALIGN_CENTER);
		final UserStatsWindow usw = this;
		closeTabButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				UI.mainMenuDecoratedTabPanel.remove(usw);
				UI.mainMenuDecoratedTabPanel.selectTab(0);

			}
		});
	}

	public UserStats setUserStats(User user) {
		ServicesAsync services = (ServicesAsync) GWT.create(Services.class);
		ServiceDefTarget target = (ServiceDefTarget) services;
		String relativeUrl = GWT.getModuleBaseURL() + "Services";
		target.setServiceEntryPoint(relativeUrl);
		final UserStats userStats = new UserStats();

		// services
		UI.servicesHandler.getUserStats(user, new AsyncCallback<UserStats>() {

			@Override
			public void onSuccess(UserStats result) {
				setUserStats(result);
				userStats.setGamesplayed(result.getGamesplayed());
				userStats.setGameswon(result.getGameswon());
				userStats.setLogincount(result.getLogincount());
				userStats.setLogintime(result.getLogintime());
				userStats.setPlayerid(result.getPlayerid());

			}

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

			}
		});
		return userStats;
	}

	public void setUserStats(UserStats us) {
		int loginCount = us.getLogincount();
		int gamesPlayed = us.getGamesplayed();
		int gamesWon = us.getGameswon();
		NumberFormat nf = NumberFormat.getFormat("##.#");
		statsGrid.setText(0, 1, loginCount + "");
		statsGrid.setText(1, 1, gamesPlayed + "");
		statsGrid.setText(2, 1, gamesWon + "");
		statsGrid.setText(3, 1, (gamesPlayed - gamesWon) + "");
		statsGrid.setText(4, 1, (gamesWon == 0 ? "0" : nf
				.format((gamesWon / (double) gamesPlayed) * 100))
				+ "%");
		statsGrid.setText(5, 1, nf.format((gamesPlayed / (double) loginCount))
				+ "db");

	}
}
