package jnet.client.gui;

import jnet.client.Client;
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.ListIterator;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import jnet.lib.EventComparator;
import jnet.lib.object.Map;
import jnet.lib.object.MapObject;

public class EventsPanel extends JPanel {

    public EventsPanel() {

        int limit = 50;

        String column[] = {"Začátek", "Konec", "Délka", "Stav", "Mapa", "Zařízení"};
        DefaultTableModel tableModel = new DefaultTableModel(column, 0) {

            @Override
            public boolean isCellEditable(int row, int column) {
                //all cells false
                return false;
            }
        };

        Collections.sort(Client.events, new EventComparator());
        Collections.reverse(Client.events);

        if (limit > Client.events.size()) {
            limit = Client.events.size();
        }

        for (int i = 0; i < limit; i++) {
            String[] info = getObjetInfo(Client.events.get(i).getObject());
            Object[] d = {
                getDate(Client.events.get(i).getStart()),
                getDate(Client.events.get(i).getEnd()),
                getDuration(Client.events.get(i).getStart(), Client.events.get(i).getEnd()),
                Client.events.get(i).getDescription(),
                info[0],
                info[1]};
            tableModel.addRow(d);
        }

        JTable table = new JTable(tableModel);
        table.setRowHeight(30);

        this.setLayout(new BorderLayout());  // Ujistěte se, že používáte vhodný layout
        this.add(new JScrollPane(table), BorderLayout.CENTER);  // Ujistěte se, že je tabulka ve JScrollPane

    }

    private String getDate(long date) {
        if (date != 0) {
            SimpleDateFormat ft = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            return ft.format(new Date(date));
        }
        return "";
    }

    private String getDuration(long startTime, long endTime) {
        if (endTime != 0) {
            long difference = endTime - startTime;

            // převod na sekundy, minuty, hodiny
            long seconds = difference / 1000;
            long minutes = difference / (1000 * 60);
            long hours = difference / (1000 * 60 * 60);

            SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
            return hours + "h " + minutes + "m " + seconds + "s";
        }
        return "";
    }

    private String[] getObjetInfo(int id) {
        for (Map map : Client.maps) {
            for (MapObject mapObject : map.getObjects()) {
                if (mapObject.getId() == id) {
                    if (map.getId() == mapObject.getMap()) {
                        String[] data = {map.getName(), mapObject.getName()};
                        return data;
                    }
                }
            }
        }

        return null;
    }

}
