import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {
	
	public static Color galeriBG;
	
	public Main() throws IOException {
		
		galeriBG = new Color (0, 0, 0);
		
		JFrame frame = new JFrame("Resim Galerisi Kayıt | Java Blackboard [127]");
		
		JPanel anapanel = new JPanel();
		anapanel.setLayout(null);
		anapanel.setBackground(galeriBG);
		anapanel.setPreferredSize(new Dimension(800,2500));
		
		JScrollPane scrollPane = new JScrollPane(anapanel);
        scrollPane.getViewport().setBackground(galeriBG); // Siyah rengi kullanabilirsiniz
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(0, 0, 940, 650);
		
		JButton kaydet = new JButton("Kaydet");
		kaydet.setBounds(10, 10, 100, 20);
		
		JButton resmiGuncelle = new JButton("Güncelle");
		resmiGuncelle.setBounds(120, 10, 100, 20);
		
		resmiGuncelle.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        anapanel.removeAll();

		        try {
		            anapanel.add(kaydet);
		            anapanel.add(resmiGuncelle);
		            anapanel.add(sekizliPanel());    
		            anapanel.add(ucluPanel());
		            anapanel.add(dortluPanel());
		            
		         
			        
		        } catch (IOException e) {
		            e.printStackTrace();
		            System.err.println("Hata Mesaji!...");
		        }

		     // Bileşenleri tekrar görünür hale getiriyoruz.
		        Component[] components = anapanel.getComponents();
		        for (Component component : components) {
		            component.setVisible(true);
		        }

		        anapanel.revalidate();
		        anapanel.repaint();
		    }
		});


		kaydet.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		    	 try {
					kaydetPanelResimleri(sekizliPanel());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		    	 
		         try {
					kaydetPanelResimleri(ucluPanel());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        
		         try {
					kaydetPanelResimleri(dortluPanel());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		         
		         // Bildirim ver
		         JOptionPane.showMessageDialog(null, "Resimler başarıyla kaydedildi.");
		    
		    }
		});

		
		anapanel.add(kaydet);
		anapanel.add(resmiGuncelle);
		anapanel.add(sekizliPanel());
		anapanel.add(ucluPanel());
		anapanel.add(dortluPanel());
		
		frame.add(scrollPane);
		
		frame.setSize(940,650);
		frame.setLayout(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
	}
	
	public static JLabel resimEkle(int w, int h) throws IOException {
		
	    URL imageUrl = new URL("https://picsum.photos/" + w + "/" + h);

	    JLabel label = new JLabel(new ImageIcon(imageUrl));
	    label.setOpaque(true); // Opaque'yi true olarak ayarlayarak arka plan rengini görünür yapın
	    label.setBounds(0, 0, w, h);

	    return label;
	}
	
	// Benzersiz dosya adı üretme işlevi
	private String BenzersizDosyaIsmi() {
	
		return "/resim_" + System.currentTimeMillis();
	
	}
	
	public static JPanel sekizliPanel() throws IOException {
		
		JPanel anapanel = new JPanel();
		anapanel.setBackground(galeriBG);
		anapanel.setLayout(null);
		anapanel.setBounds(20,40,880,1080);
		
		int x = 0;
		int y = 0;
		int f = 30;
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBounds(x, y, 420, 290); x = x + 420 + f;
		
		panel1.add(resimEkle(420,290));
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(null);
		panel2.setBounds(x, y, 180, 290); x = x + 180 + f;
		
		panel2.add(resimEkle(180,290));
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(null);
		panel3.setBounds(x, y, 220, 400); y = y + 400 + f;
		
		panel3.add(resimEkle(220,400));
		
		JPanel panel4 = new JPanel();
		panel4.setLayout(null);
		panel4.setBounds(x, y, 220, 250); x = 0; y = 290 + f;
		
		panel4.add(resimEkle(220,250));
		
		JPanel panel5 = new JPanel();
		panel5.setLayout(null);
		panel5.setBounds(x, y, 240, 360); x = x + 240 + f;
		
		panel5.add(resimEkle(240,360));
		
		JPanel panel6 = new JPanel();
		panel6.setLayout(null);
		panel6.setBounds(x, y, 360, 360); x = 0; y = y + 360 + f;
		
		panel6.add(resimEkle(360,360));
		
		JPanel panel7 = new JPanel();
		panel7.setLayout(null);
		panel7.setBounds(x, y, 630, 370); x = x + 630 + f;
		     
		panel7.add(resimEkle(630,370));
		
		JPanel panel8 = new JPanel();
		panel8.setLayout(null);
		panel8.setBounds(x, y, 220, 370);
		
		panel8.add(resimEkle(220,370));
		
		anapanel.add(panel1);
		anapanel.add(panel2);
		anapanel.add(panel3); 
		anapanel.add(panel4);
		anapanel.add(panel5);
		anapanel.add(panel6);
		anapanel.add(panel7);
		anapanel.add(panel8);
		
		return anapanel;
	}
	
	public static JPanel ucluPanel() throws IOException {
		
		JPanel anapanel = new JPanel();
		anapanel.setBackground(galeriBG);
		anapanel.setLayout(null);
		anapanel.setBounds(20,1150,880,233);
		
		int x = 0;
		int y = 0;
		int w = 273;
		int f = 30;
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBounds(x, y, w, w); x = x + w + f;
		
		
		panel1.add(resimEkle(273,273));
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(null);
		panel2.setBounds(x, y, w, w); x = x + w + f;
		
		panel2.add(resimEkle(274,274));
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(null);
		panel3.setBounds(x, y, w, w);
		
		panel3.add(resimEkle(275,275));
		
		anapanel.add(panel1);
		anapanel.add(panel2);
		anapanel.add(panel3); 
		
		return anapanel;
	}
	
	public static JPanel dortluPanel() throws IOException {
		
		JPanel anapanel = new JPanel();
		anapanel.setBackground(galeriBG);
		anapanel.setLayout(null);
		anapanel.setBounds(20,1413,880,1060);
		
		int x = 0;
		int y = 0;
		int f = 30;
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBounds(x, y, 480, 530); x = x + 480 + f;
		panel1.setBackground(Color.blue);
		
		panel1.add(resimEkle(480,530));
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(null);
		panel2.setBounds(x, y, 370, 200); y = y + 200 + f;
		panel2.setBackground(Color.blue);
		
		panel2.add(resimEkle(370,200));
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(null);
		panel3.setBounds(x, y, 370, 300); x = 0; y = y + 300 + f;
		panel3.setBackground(Color.blue);
		
		panel3.add(resimEkle(370,300));
		
		JPanel panel4 = new JPanel();
		panel4.setLayout(null);
		panel4.setBounds(x, y, 880, 300); 
		panel4.setBackground(Color.blue);
		
		panel4.add(resimEkle(880,300));
		
		anapanel.add(panel1);
		anapanel.add(panel2);
		anapanel.add(panel3); 
		anapanel.add(panel4);
		
		return anapanel;
	}
	
	private void kaydetPanelResimleri(JPanel panel) {
		
	    Component[] components = panel.getComponents();
	    
	    for (Component component : components) {
	    	
	        if (component instanceof JPanel) {
	        	
	            JPanel subPanel = (JPanel) component;
	            
	            Component[] subComponents = subPanel.getComponents();
	            
	            for (Component subComponent : subComponents) {
	            	
	                if (subComponent instanceof JLabel) {
	                	
	                    JLabel label = (JLabel) subComponent;
	                    
	                    ImageIcon icon = (ImageIcon) label.getIcon();
	                    
	                    BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
	                    
	                    Graphics2D g2d = bufferedImage.createGraphics();
	                    
	                    icon.paintIcon(null, g2d, 0, 0);
	                    g2d.dispose();
	                    
	                    // Resmi dosyaya kaydet
	                    try {
	                        File output = new File(getDesktopPath() + BenzersizDosyaIsmi() + ".jpg");
	                        ImageIO.write(bufferedImage, "jpg", output);
	                        
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	        }
	    }
	}
	
	public static String getDesktopPath() {
	  
		Path desktopPath = Paths.get(System.getProperty("user.home"), "/Desktop");
	    
		String desktopPathString = desktopPath.toString();
	    
	    return desktopPathString;
	  }
	
}


