package premium_store.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Entity
@Table(name = "GAMECLIENTS")
public class GameClient {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "login", nullable = false, length = 255)
    private String login;

    @Column(name = "email", nullable = false, length = 255)
    private String email;

    @Column(name = "password", nullable = false, length = 255)
    private String password;

    @Column(name = "balance")
    private Integer balance;

    @OneToMany(fetch = FetchType.EAGER)
    private List<Tank> tanks = new ArrayList<>();

    private UserRole role;

    public GameClient(){ }

    public GameClient(String login, String email, String password, Integer balance, UserRole role){
        this.login = login;
        this.email = email;
        this.password = password;
        this.balance = balance;
        this.role = role;
    }

    public GameClient(String login, String email, String password, Integer balance){
        this(login, email, password, balance, UserRole.USER);
    }

    public Long getId(){
        return id;
    }

    public String getLogin(){
        return login;
    }

    public void setLogin(String login){
        this.login = login;
    }

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public String getPassword(){
        return password;
    }

    public Integer getBalance(){
        return balance;
    }

    public void setBalance(Integer balance){
        this.balance = balance;
    }

    public List<Tank> getTanks(){
        return tanks;
    }

    public void setTanks(Tank tank){
        this.tanks.add(tank);
    }

    public UserRole getRole() {
        return role;
    }

    public void setRole(UserRole role) {
        this.role = role;
    }

    //метод для сравнения
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;

        if (o == null || getClass() != o.getClass())
            return false;

        GameClient gameClient = (GameClient) o;

        return Objects.equals(id, gameClient.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    //преобразование данных по объекту в строчку
    @Override
    public String toString() {
        return "Client{" +
                "id =" + id +
                ", login ='" + login + '\'' +
                ", password ='" + password + '\'' +
                ", email ='" + email + '\'' +
                ", balance ='" + balance + '\'' +
                ", tanks ='" + tanks.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
                ", role ='" + role + '\'' +
                '}';
    }
}

