package kamiworldserver.elem;

import org.w3c.dom.*;
import java.util.HashMap;
import java.util.ArrayList;

public class KamiMap {

    String name = null;
    Long mod;
    HashMap<String, KamiLink> hmLinks = null;
    ArrayList<KamiUser> alPlayers = null;

    public KamiMap(String _name, Document doc) {
        name = _name;

        hmLinks = new HashMap<String, KamiLink>();
        alPlayers = new ArrayList<KamiUser>();

        recreate(doc);
    }

    public void recreate(Document doc) {
        removeWhitespaceNodes(doc.getDocumentElement());
        doc.normalize();

        mod = Long.parseLong(doc.getDocumentElement().getAttribute("date"));

        Node n = doc.getDocumentElement().getElementsByTagName("linklayer").item(0).getFirstChild();
        NodeList nlLinks = null;
        if (n.getNodeType() == Node.TEXT_NODE) {
            nlLinks = n.getNextSibling().getChildNodes();
        } else {
            nlLinks = n.getChildNodes();
        }
        parseLinks(nlLinks);
        doc = null;
    }

    /*
    public void recreate(Document doc,long l) {
    recreate(doc);
    mod = l;
    }
     */
    public KamiLink getLink(String s) {
        return (KamiLink) hmLinks.get(s);
    }

    public boolean checkMod(long l) {
        return mod == l;
    }

    public synchronized boolean checkLink(String s, KamiUser ku) {
        KamiLink kl = hmLinks.get(s);
        return kl == null || ku.getX() < kl.x || ku.getX() > kl.x + kl.w || ku.getY() < kl.y || ku.getY() > kl.y + kl.h;
    }

    public ArrayList<KamiUser> getPlayers() {
        return alPlayers;
    }

    public void addPlayer(KamiUser ku) {
        synchronized (alPlayers) {
            alPlayers.add(ku);
        }
    }

    public void removePlayer(KamiUser ku) {
        synchronized (alPlayers) {
            alPlayers.remove(ku);
        }
    }

    @Override
    public String toString() {
        return name;
    }

    // =========================================================================
    // Other 
    //
    protected void parseLinks(NodeList nlLinks) {

        int l = nlLinks.getLength();
        if (l < 1) {
            return;
        }
        int i = nlLinks.item(0).getNodeType() == Node.TEXT_NODE ? 1 : 0;
        KamiLink kl = null;

        do {
            kl = new KamiLink(Integer.parseInt(nlLinks.item(i).getAttributes().getNamedItem("x").getNodeValue()),
                    Integer.parseInt(nlLinks.item(i).getAttributes().getNamedItem("y").getNodeValue()),
                    Integer.parseInt(nlLinks.item(i).getAttributes().getNamedItem("width").getNodeValue()),
                    Integer.parseInt(nlLinks.item(i).getAttributes().getNamedItem("height").getNodeValue()),
                    nlLinks.item(i).getAttributes().getNamedItem("map").getNodeValue(),
                    //Short.parseShort(nlLinks.item(i).getAttributes().getNamedItem("nx").getNodeValue()),
                    //Short.parseShort(nlLinks.item(i).getAttributes().getNamedItem("ny").getNodeValue())
                    nlLinks.item(i).getAttributes().getNamedItem("nx").getNodeValue(),
                    nlLinks.item(i).getAttributes().getNamedItem("ny").getNodeValue());
            hmLinks.put(nlLinks.item(i).getAttributes().getNamedItem("id").getNodeValue(), kl);
            ++i;
        } while (i < l);

    }

    private static void removeWhitespaceNodes(Element e) {
        NodeList children = e.getChildNodes();
        for (int i = children.getLength() - 1; i >= 0; i--) {
            Node child = children.item(i);
            if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
                e.removeChild(child);
            } else if (child instanceof Element) {
                removeWhitespaceNodes((Element) child);
            }
        }
    }
}
