package jguard.client.gui;

import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.object.MapObject;
import cucky.jGuard.lib.object.ObjectConnection;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import jguard.client.Client;


public class MapView extends JPanel{
    
    public static List<PaintObject> items;
    private static List<PaintConnection> connection;

    public void prekreslit() {
        repaint();
    }

    public MapView() {
        
        this.setBackground(Color.white);
        
        addMouseMotionListener(new MapViewAction());
        addMouseListener(new MapViewAction());
        
        
        // nacteni objektů          
        items = new ArrayList<>();
        for (MapObject obj : Client.map.get(Window.currentMapIndex).getObjects()) {
            items.add(new PaintObject(obj.getId()));
        }
        
        // nacteni propojeni
        connection = new ArrayList<>();
        for (ObjectConnection c : Client.map.get(Window.currentMapIndex).getConnection()) {
            connection.add(new PaintConnection(c.getId()));
        }

        
        // periodicke prekreslovani
        Timer timer = new Timer(100, (ActionEvent ae) -> {
            repaint();
        });
        timer.start();
        
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();

        // vykresleni lock/unlock mapy
        try {
            BufferedImage image;
            if (Client.map.get(Window.currentMapIndex).isLock()) {
                image = ImageIO.read(new File("img/lock.png"));
            } else {
                image = ImageIO.read(new File("img/unlock.png"));
            }
            g.drawImage(image, 0, 0, this);
        } catch (IOException ex) {
            LogFile.printErr("File not found: " + ex);
        }
        
        // kreslení nového propojeni
        if (MapViewAction.createConnection && MapViewAction.startConMapObject != null) {
            g2.draw(new Line2D.Double(MapViewAction.startConMapObject.getPoint(), MapViewAction.endConnPoint));
        }
        
        // vykresleni stavajicich propojeni
        for (PaintConnection c : connection) {
            c.paint(g2);
        }
       
        // vykresleni objektu
        for (PaintObject i : items) {
            i.paint(g2);
        }
        
        g2.dispose();
    }
    
    public static void addObject(MapObject mo) {
        items.add(new PaintObject(mo.getId()));
    }

    static void removeObject(int id) {
        for (PaintObject i : items) {
            if (i.getId() == id) {
                items.remove(i);
                break;
            }
        }
    }
    
    public static void addObjectConnection(ObjectConnection oc) {
        connection.add(new PaintConnection(oc.getId()));
    }
}
