package cz.mendelu.xmarik.train_manager.models;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import cz.mendelu.xmarik.train_manager.helpers.ParseHelper;

/**
 * Class Server represents a single server. Server contains database of all trains.
 */
public class Server {
    public static AtomicInteger globalId = new AtomicInteger(0);
    public static final int DEFAULT_PORT = 5823;

    public String name;
    public String host;
    public int port;
    public String type;
    public boolean active;

    public String username;
    public String password;

    public Server(String name, String host, int port, boolean active, String type, String username, String password) {
        this.name = name;
        this.host = host;
        this.port = port;
        this.type = type;
        this.active = active;
        this.username = username;
        this.password = password;
    }

    // Create server from discovery packet.
    // @param disovery in format:
    //   "hJOP";verze_protokolu;typ_zarizeni;server_nazev;server_ip;server_port;server_status;server_popis
    public Server(String discovery) {
        ArrayList<String> parsed = ParseHelper.parse(discovery, ";", "");

        this.name = parsed.get(7);
        this.host = parsed.get(4);
        this.port = Integer.parseInt(parsed.get(5));
        this.type = parsed.get(3);
        this.active = parsed.get(6).equals("on");
        this.username = "";
        this.password = "";
    }

    public String getTitle() {
        return this.name + "\u00A0\u00A0" + this.host;
    }

    public String getSaveDataString() {
        return this.name + ";" + this.host + ";" + this.port + ";"
                + this.type + ";" + this.username + ";" + this.password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Server server = (Server) o;

        if (port != server.port) return false;
        if (!name.equals(server.name)) return false;
        return host != null ? host.equals(server.host) : server.host == null;
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + (host != null ? host.hashCode() : 0);
        result = 31 * result + port;
        return result;
    }
}

@@ -91,39 +91,15 @@
 til.
-concurrent.atomic.AtomicInteger
+Objects
 ;%0A%0Ai
@@ -276,123 +276,8 @@
 r %7B%0A
-    public static AtomicInteger globalId = new AtomicInteger(0);%0A    public static final int DEFAULT_PORT = 5823;%0A%0A
     
@@ -1384,32 +1384,66 @@
 %7B%0A        return
+ this.name.isEmpty() ? this.host :
  this.name + %22%5Cu
@@ -1973,35 +1973,23 @@
 urn 
-host != null ? host
+Objects
 .equals(
 serv
@@ -1988,22 +1988,13 @@
 als(
-server.
 host
-) :
+,
  ser
@@ -2001,24 +2001,17 @@
 ver.host
- == null
+)
 ;%0A    %7D%0A

Improve displaying server without name, server cleanup