package jnet.client.gui;

import jnet.client.Client;
import jnet.client.gui.dialog.UserDialog;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import jnet.lib.object.User;

public class TableUsers extends JTable {

    private static final String[] COLUMN_NAMES = {
        "ID",
        "Uživatel",
        "Přidat mapu",
        "Odebrat mapu",
        "Upravit mapu",
        "Přidat objekt",
        "Odebrat objekt",
        "Upravit objekt",
        "Nastavit server"
    };

    public TableUsers() {

        tableSetup();

    }

    private void tableSetup() {
        DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, Client.users.size()) {
            @Override
            public boolean isCellEditable(int row, int column) {
                //all cells false
                return false;
            }

            @Override
            public Class<?> getColumnClass(int columnIndex) {
                if (columnIndex > 1) {
                    return ImageIcon.class; 
                }
                return super.getColumnClass(columnIndex);
            }
        };

        this.setModel(model);
        
        this.setRowHeight(25);

        this.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer());
        
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) { // Kontrola dvojitého kliku
                    int row = getSelectedRow(); // Získání vybraného řádku
                    if (row != -1) {
                        Object id = getValueAt(row, 0);
                        for (User user : Client.users) {
                            if (user.getId() == (int) id) {
                                new UserDialog(user).setVisible(true);
                                break;
                            }
                        }
                        
                    }
                }
            }
        });

        int row = 0;
        for (User u : Client.users) {
            try {

                ImageIcon ii_yes = new ImageIcon(ImageIO.read(new File("img/yes.png")));
                ImageIcon ii_no = new ImageIcon(ImageIO.read(new File("img/no.png")));

                this.setValueAt(u.getId(), row, 0);
                this.setValueAt(u.getUsername(), row, 1);
                this.setValueAt((u.isAddMap() ? ii_yes : ii_no), row, 2);
                this.setValueAt((u.isRemoveMap() ? ii_yes : ii_no), row, 3);
                this.setValueAt((u.isEditMap() ? ii_yes : ii_no), row, 4);
                this.setValueAt((u.isAddObject()? ii_yes : ii_no), row, 5);
                this.setValueAt((u.isEditObject()? ii_yes : ii_no), row, 6);
                this.setValueAt((u.isRemoveObject()? ii_yes : ii_no), row, 7);
                this.setValueAt((u.isEditServer() ? ii_yes : ii_no), row, 8);

                row++;
            } catch (IOException ex) {
                Logger.getLogger(TableDeviceType.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    static class ImageRenderer extends JLabel implements TableCellRenderer {

        public ImageRenderer() {
            setHorizontalAlignment(CENTER); // Obrázky budou centrované
            setOpaque(true); // Zajištění vykreslení pozadí
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
                setBackground(table.getSelectionBackground());
            } else {
                setBackground(table.getBackground());
            }

            if (value instanceof ImageIcon) {
                setIcon((ImageIcon) value);
                setText(null); // Zamezení zobrazení textu
            } else {
                setIcon(null);
                setText(value != null ? value.toString() : "");
            }

            return this;
        }
    }

}
