package org.skierz.net;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.UIManager;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

public class Startup
extends Application
{
	private static int playing = 0;
	private static ArrayList<String> musicLibrary = new ArrayList<String>();
	private long StartTime;
	private static Image iconFile;
	private static ImageIcon playIcon;
	private static ImageIcon pauseIcon;
	private static ImageIcon nextIcon;
	private static ImageIcon prevIcon;
	private static ImageIcon repeatIcon;
	private static ImageIcon shuffleIcon;
	private static ImageIcon playingIcon;
	private static ImageIcon muteVolIcon;
	private static ImageIcon quietVolIcon;
	private static ImageIcon loudVolIcon;
	private static ImageIcon fullVolIcon;
	private static BufferedImage albumIcon;
	
	
	public void start(Stage primaryStage) throws Exception
	{
		try
		{
			for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName()))
				{
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		}
		catch (Exception e)
		{
			System.err.println("Nimbus unavailiable");
		}
		new Save();
		loadIcons();
		this.StartTime = System.currentTimeMillis();
		if (resourcesNotPresent()) {
			downloadResources();
		}
		albumIcon = ImageIO.read(new File(Save.iconsFolder.getAbsolutePath() + "/1389381141_HP-iTunes-Dock-512.png"));
		Save.loadSongs();
		System.err.println("Load Time: " + (System.currentTimeMillis() - this.StartTime));
	}

	private void loadIcons() {
		iconFile = Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389138801_Equipment_icon_3D_rev.png");
		playIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137487_Gnome-Media-Playback-Start-48.png"));
		pauseIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137486_Gnome-Media-Playback-Pause-48.png"));
		nextIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137484_Gnome-Media-Seek-Forward-48.png"));
		prevIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137495_Gnome-Media-Seek-Backward-48.png"));
		repeatIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137493_Gnome-Media-Playlist-Repeat-48.png"));
		shuffleIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137499_Gnome-Media-Playlist-Shuffle-48.png"));
		playingIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/PlayCover.png"));
		muteVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137476_Gnome-Audio-Volume-Muted-48.png"));
		quietVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137502_Gnome-Audio-Volume-Low-48.png"));
		loudVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137510_Gnome-Audio-Volume-Medium-48.png"));
		fullVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137507_Gnome-Audio-Volume-High-48.png"));
	}

	private void downloadResources()
	{
		Log.println("Downloading resources");
		try
		{
			InputStream in = null;
			in = URI.create("https://dl.dropboxusercontent.com/s/taricjr95qvbtrc/icons.zip?dl=1&token_hash=AAEgYNS6GJEmbr-qPr6pFRtpwfktJbP-sCr_czHGaUwnWw").toURL().openStream();
			Files.copy(in, new File(Save.settingFolder.getAbsolutePath() + "/icons.zip").toPath());
			in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		String destination = Save.settingFolder.getAbsolutePath() + "/icons/";
		new File(destination).mkdirs();
		extractZip(new File(Save.settingFolder.getAbsolutePath() + "/icons.zip"), destination);
		new File(Save.settingFolder.getAbsolutePath() + "/icons.zip").delete();
		iconFile = Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389138801_Equipment_icon_3D_rev.png");
		playIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137487_Gnome-Media-Playback-Start-48.png"));
		pauseIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137486_Gnome-Media-Playback-Pause-48.png"));
		nextIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137484_Gnome-Media-Seek-Forward-48.png"));
		prevIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137495_Gnome-Media-Seek-Backward-48.png"));
		repeatIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137493_Gnome-Media-Playlist-Repeat-48.png"));
		shuffleIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137499_Gnome-Media-Playlist-Shuffle-48.png"));
		playingIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/PlayCover.png"));
		muteVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137476_Gnome-Audio-Volume-Muted-48.png"));
		quietVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137502_Gnome-Audio-Volume-Low-48.png"));
		loudVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137510_Gnome-Audio-Volume-Medium-48.png"));
		fullVolIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Save.iconsFolder.getAbsolutePath() + "/1389137507_Gnome-Audio-Volume-High-48.png"));
		try
		{
			albumIcon = ImageIO.read(new File(Save.iconsFolder.getAbsolutePath() + "/1389381141_HP-iTunes-Dock-512.png"));
		}
		catch (IOException e2)
		{
			e2.printStackTrace();
		}

	}

	private void extractZip(File fileZip, String destination)
	{
		try
		{
			ZipFile zipFile = new ZipFile(fileZip);
			zipFile.extractAll(destination);
		}
		catch (ZipException e)
		{
			e.printStackTrace();
		}
	}

	private boolean resourcesNotPresent()
	{
		if (Save.iconsFolder.exists() && Save.settingFile.exists() &&
				(iconFile != null) && (playIcon != null) && (pauseIcon != null) && (nextIcon != null) && (prevIcon != null) && (repeatIcon != null) && (shuffleIcon != null)) {
			return false;
		}
		return true;
	}

	public static void main(String[] args)
	{
		launch(args);
	}

	public static int getPlaying()
	{
		return playing;
	}

	public static void setPlaying(int playing2)
	{
		playing = playing2;
	}

	public static ArrayList<String> getMusicLibrary()
	{
		return musicLibrary;
	}

	public static void setMusicLibrary(ArrayList<String> musicLibrary2)
	{
		musicLibrary = musicLibrary2;
	}

	public static Image getIconFile()
	{
		return iconFile;
	}

	public static ImageIcon getRepeatIcon()
	{
		return repeatIcon;
	}

	public static ImageIcon getPrevIcon()
	{
		return prevIcon;
	}

	public static ImageIcon getPlayIcon()
	{
		return playIcon;
	}

	public static ImageIcon getPauseIcon()
	{
		return pauseIcon;
	}

	public static ImageIcon getNextIcon()
	{
		return nextIcon;
	}

	public static ImageIcon getShuffleIcon()
	{
		return shuffleIcon;
	}

	public static BufferedImage getAlbumIcon()
	{
		return albumIcon;
	}

	public static ImageIcon getPlayingIcon()
	{
		return playingIcon;
	}

	public static ImageIcon getFullVolIcon()
	{
		return fullVolIcon;
	}

	public static ImageIcon getLoudVolIcon()
	{
		return loudVolIcon;
	}

	public static ImageIcon getQuietVolIcon()
	{
		return quietVolIcon;
	}

	public static ImageIcon getMuteVolIcon()
	{
		return muteVolIcon;
	}
}



/* Location:           C:\Users\Tom\Dropbox\workspace\Music Player\old\Music Player-Alpha.jar

 * Qualified Name:     org.Skierz.net.Startup

 * JD-Core Version:    0.7.0.1

 */
