import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 	örnek linkler :
 	
	https://img.freepik.com/free-photo/forest-with-trees-close-up_23-2148910463.jpg
	https://img.freepik.com/free-vector/wooden-stilt-house-green-forest-glade-landscape_107791-17082.jpg
	https://img.freepik.com/free-photo/beautiful-shot-forest-with-tall-green-trees-with-sun-shining-through-branches_181624-20728.jpg
	https://scontent.fayt2-1.fna.fbcdn.net/v/t39.30808-6/323429132_727646398688535_4026597820015818919_n.jpg?stp=dst-jpg_p843x403&_nc_cat=102&ccb=1-7&_nc_sid=5cd70e&_nc_ohc=GHfJiVKa3xsAX-oFA5D&tn=8waWM1PxwYl9MZcb&_nc_ht=scontent.fayt2-1.fna&oh=00_AfC1XhK_g6lTcW90DrqxO_mU2NjGkhEVAE7MNJ_mdCa6ZA&oe=63DA8462
 */



public class LinktenDosyaIndir {
	
	LinktenDosyaIndir () {
		
		//Framemimizi olusturalim.
		JFrame frame = new JFrame("Linkteki Dosyayi Indir | Java Blackboard [099]");
        
        // Panel olusturalim
        JPanel panel = new JPanel();
        panel.setBackground(new Color(219, 242, 39));
        

        // JTextField ekleyelim ki kullanicidan linki alabilelim :)
        JTextField linkField = new JTextField(65);
        panel.add(linkField);

        // Kayit icin buton olusturalim.
        JButton saveAsButon = new JButton("Save As...");
        panel.add(saveAsButon);
        
        saveAsButon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
                try {
                	
                    // JTextField'dan linki alalim.
                    URL url = new URL(linkField.getText());

                    // JFileChooser ile kaydedecegimiz yeri seçelim.
                    JFileChooser fileChooser = new JFileChooser();
                    fileChooser.setDialogTitle("Save File Penceresi");
                    File desktop = new File(System.getProperty("user.home"), "Desktop"); // Desktop olarak acilmasini istiyoruz
                    fileChooser.setCurrentDirectory(desktop);
                    
                    int kullaniciSecimi = fileChooser.showSaveDialog(frame);
                    // 0 değeri "Kaydet" -> "APPROVE_OPTION"
                    // 1 değeri "Farklı Kaydet" -> "CANCEL_OPTION"
                    // 2 değeri "İptal" -> "ERROR_OPTION"
                    
                    if (kullaniciSecimi == JFileChooser.APPROVE_OPTION) {
                        
                    	File dosyayiKeydet = fileChooser.getSelectedFile();
                        
                        // Dosyanin linkten Download edilmesi ve kayit
                        Files.copy(url.openStream(), dosyayiKeydet.toPath());
                        
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        
        // paneli frame'e ekliyoruz
        frame.add(panel);

        // frame ozellikleri
        frame.setSize(800, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}