package com.lu.model;

import com.avaje.ebean.Ebean;
import com.lu.Constants;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Team {

  @Id
  private int id;
  @Column(unique = true)
  private String name;

  public Team() {
  }

  public Team(String teamName) {
    setName(teamName);
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public static Team getByName(String name) {
    return Ebean.find(Team.class).where().eq("name", name).findUnique();
  }

  public static List<Team> findAll() {
    return Ebean.find(Team.class).findList();
  }

  public int getPoints() {
    List<TeamGame> teamGames = getTeamGames();
    int res = 0;
    for (TeamGame teamGame : teamGames) {
      res += teamGame.getPoints();
    }
    return res;
  }

  private List<TeamGame> getTeamGames() {
    return Ebean.find(TeamGame.class).where().eq("team", this).findList();
  }

  public int getScoredGoals() {
    int goalsAmount = Ebean.find(Goal.class)
        .where().in("scoredTeam", this).findRowCount();
    return goalsAmount;
  }

  public int getScoredGoalInMainTime() {
    int goalsAmount = Ebean.find(Goal.class)
        .where().in("scoredTeam", this)
        .le("goalTimeInSeconds", Constants.SIXTEE_MINUTES_IN_SECOND)
        .findRowCount();

    return goalsAmount;
  }

  public int getLostGoalInMainTime() {
    int goalsAmount = Ebean.find(Goal.class)
        .where().in("lostTeam", this)
        .le("goalTimeInSeconds", Constants.SIXTEE_MINUTES_IN_SECOND)
        .findRowCount();
    return goalsAmount;
  }

  public int getLostGoals() {
    int goalsAmount = Ebean.find(Goal.class)
        .where().in("lostTeam", this).findRowCount();

    return goalsAmount;
  }

  private List<TeamGame> getTeamGamesWithOponent() {
    List<TeamGame> teamGames = getTeamGames();
    List<Game> res1 = Ebean.find(Game.class).where().in("teamGameOne", teamGames).findList();
    List<Game> res2 = Ebean.find(Game.class).where().in("teamGameTwo", teamGames).findList();

    List<TeamGame> oponentTeamGames = new ArrayList<>();
    for (Game g : res2) {
      oponentTeamGames.add(g.getTeamGameOne());
    }
    for (Game g : res1) {
      oponentTeamGames.add(g.getTeamGameTwo());
    }
    return oponentTeamGames;
  }

  @Override
  public String toString() {
    return "Team{" +
        "id=" + id +
        ", name=" + name +
        ", points=" + getPoints() +
        ", TotalScoredGoals=" + getScoredGoals() +
        ", TotalLostGoals=" + getLostGoals() +
        ", ScoredGoalsInMainTime=" + getScoredGoalInMainTime() +
        ", LostGoalsInMainTime=" + getLostGoalInMainTime() +
        "}";
  }
}

@@ -171,36 +171,8 @@
 Id;%0A
-import java.util.ArrayList;%0A
 impo
@@ -1204,34 +1204,34 @@
        .where().
-in
+eq
 (%22scoredTeam%22, t
@@ -1383,18 +1383,18 @@
 where().
-in
+eq
 (%22scored
@@ -1623,34 +1623,34 @@
        .where().
-in
+eq
 (%22lostTeam%22, thi
@@ -1867,18 +1867,18 @@
 where().
-in
+eq
 (%22lostTe
@@ -1941,33 +1941,24 @@
 %0A  p
-rivate List%3CTeamGame%3E
+ublic int
  get
+Won
 Team
@@ -1966,17 +1966,12 @@
 ames
-WithOpone
+Amou
 nt()
@@ -1981,70 +1981,18 @@
     
-List%3CTeamGame%3E teamGames = getTeamGames();%0A    List%3CGame%3E res1
+int amount
  = E
@@ -1993,32 +1993,36 @@
 nt = Ebean.find(
+Team
 Game.class).wher
@@ -2008,32 +2008,41 @@
 (TeamGame.class)
+%0A        
 .where().in(%22tea
@@ -2038,267 +2038,162 @@
 e().
-in
+eq
 (%22team
-GameOne%22, teamGames).findLis
+%22, this).eq(%22winners%22, true).findRowCoun
 t();%0A
+%0A
     
-List%3CGame%3E res2 = Ebean.find(Game.class).where().in(%22teamGameTwo%22, teamGames).findList()
+return amount
 ;%0A
-%0A
   
-  List%3CTeamGame%3E oponentTeamGames = new ArrayList%3C%3E();%0A    for (Game g : res2) %7B%0A      oponentTeamGames.add(g.get
+%7D%0A%0A  public int getLostTeamGamesAmount() %7B%0A    int amount = Ebean.find(
 TeamGame
 One(
@@ -2192,101 +2192,87 @@
 Game
-One());
+.class)
 %0A    
-%7D%0A
     
-for (Game g : res1) %7B%0A      oponentTeamGames.add(g.getTeamGameTwo
+.where().eq(%22team%22, this).eq(%22winners%22, false).findRowCount
 ()
-)
 ;%0A
-    %7D
 %0A   
@@ -2283,24 +2283,14 @@
 urn 
-oponentTeamGames
+amount
 ;%0A  
@@ -2434,24 +2434,117 @@
 tPoints() +%0A
+        %22, win=%22 + getWonTeamGamesAmount() +%0A        %22, loose=%22 + getLostTeamGamesAmount() +%0A
         %22, T

Fix for sql queries, instead of in() use eq() queries for get wonGames and lostGame