//~--- non-JDK imports --------------------------------------------------------

import org.jaudiotagger.audio.*;
import org.jaudiotagger.audio.exceptions.*;
import org.jaudiotagger.tag.*;

import Dating.Time;

//~--- JDK imports ------------------------------------------------------------

import java.awt.event.*;
import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;

class adapter extends WindowAdapter {
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}


public class audiobookMaker extends JFrame {
    static Time time = new Time(0, 0, 0);
    String     de[]    = {
        "Ordnername", "Covername", "Anzahl der Editpoints", ".POD Datei erstellen", "1 Titel, 2 Titel, ...",
        "1-1 Titel, 1-2 Titel...3-7 Titel, ...", "Dateinamen selber angeben", "Dateinamen angeben. Datei Nr.",
        "Erfolgreich!\nDas Programm wird beendet...", "Unbekannter Fehler!", "Anzahl der CDs",
        "Anzahl der Titel auf CD ", "Bilddatei wurde nicht gefunden!\nDie Datei wird ohne Cover erstellt!"
    };
    String en[] = {
        "Foldername", "Covername", "Number of Editpoints", "Create .POD file", "1 Title, 2 Title, ...",
        "1-1 Title, 1-2 Title...3-7 Title, ...", "Type in Filenames by yourself", "Type in Filenames. File no.",
        "Build Succesful!\nProgram will exit...", "Unknown error!", "Type in number of CDs", "Number of Tracks on CD ",
        "Image was not found!\nFile will created without Cover!"
    };
    String[]        selectedLanguage       = de;
    public JLabel   editpointLabel         = new JLabel(selectedLanguage[2]);
    public JSpinner editpointField         = new JSpinner(new SpinnerNumberModel(1, 0, 1000, 1));
    public JLabel   createFileStdLabel     = new JLabel(selectedLanguage[4]);
    public JButton  createFileStd          = new JButton(selectedLanguage[3]);
    public JLabel   createFileSelfLabel    = new JLabel(selectedLanguage[6]);
    public JButton  createFileSelf         = new JButton(selectedLanguage[3]);
    public JLabel   createFileMultiCDLabel = new JLabel(selectedLanguage[5]);
    public JButton  createFileMultiCD      = new JButton(selectedLanguage[3]);

//  Objekte inizialisieren
    public JLabel     FolderLabel = new JLabel(selectedLanguage[0]);
    public JTextField Folder      = new JTextField();
    public JLabel     CoverLabel  = new JLabel(selectedLanguage[1]);
    public JTextField CoverField  = new JTextField();
    BufferedImage     CoverImage;

//  \Objekte
    public audiobookMaker() {
        super("iPod audiobook Maker");
        addWindowListener(new adapter());
        this.setSize(600, 300);
        setLayout(null);

//      Objekte einstellen
        FolderLabel.setBounds(55, 22, 100, 20);
        Folder.setBounds(135, 20, 400, 25);
        CoverLabel.setBounds(50, 62, 100, 20);
        CoverField.setBounds(135, 60, 400, 25);
        editpointLabel.setBounds(5, 102, 130, 20);
        editpointField.setBounds(135, 100, 100, 25);
        createFileStdLabel.setBounds(210, 222, 180, 20);
        createFileStd.setBounds(210, 180, 150, 30);
        createFileStd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                createStd();
            }
        });
        createFileSelfLabel.setBounds(400, 222, 180, 20);
        createFileSelf.setBounds(400, 180, 150, 30);
        createFileSelf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                createSelf();
            }
        });
        createFileMultiCDLabel.setBounds(20, 222, 180, 20);
        createFileMultiCD.setBounds(20, 180, 150, 30);
        createFileMultiCD.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                createMultiCD();
            }
        });

//      \Objekte
        add(FolderLabel);
        add(Folder);
        add(CoverLabel);
        add(CoverField);
        add(editpointLabel);
        add(editpointField);
        add(createFileStd);
        add(createFileStdLabel);
        add(createFileSelf);
        add(createFileSelfLabel);
        add(createFileMultiCD);
        add(createFileMultiCDLabel);
        this.setVisible(true);
    }

    public void createStd() {
        try {
            FileWriter     creator    = new FileWriter("Podcast.POD");
            BufferedWriter writer     = new BufferedWriter(creator);
            int            Editpoints = (int) Double.parseDouble(editpointField.getValue().toString());
            boolean        isCover;

            writer.write("[Podcast]\n");
            writer.write("altfolder1=" + Folder.getText() + "\n");
            writer.write("basename=" + Folder.getText() + "\n");

            try {
                File CoverFile = new File(Folder.getText() + "\\" + CoverField.getText());

                CoverImage = ImageIO.read(CoverFile);
                writer.write("artwork=" + CoverField.getText() + "\n");
                writer.write("imgwidth=" + CoverImage.getWidth() + "\n");
                writer.write("imgheight=" + CoverImage.getHeight() + "\n");
                isCover = true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, selectedLanguage[12]);
                isCover = false;
            }

            writer.write("editpointcount=" + Editpoints + "\n\n\n\n\n\n");

            int i;

            for (i = 1; i <= Editpoints; i++) {
                writer.write("[Editpoint_" + i + "]\n");

                if (isCover == true) {
                    writer.write("image=" + CoverField.getText() + "\n");
                }

                writer.write("audiofile=" + i + " " + Folder.getText() + ".m4a\n");
                writer.write("chapter=" + i + "\n");
                writer.write("start=" + "\n\n\n");
            }

            writer.close();
            JOptionPane.showMessageDialog(null, selectedLanguage[8]);
            System.exit(0);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, selectedLanguage[9]);
        }
    }

    public void createSelf() {
        try {
            FileWriter     creator    = new FileWriter("Podcast.POD");
            BufferedWriter writer     = new BufferedWriter(creator);
            int            Editpoints = (int) Double.parseDouble(editpointField.getValue().toString());
            boolean        isCover;

            writer.write("[Podcast]\n");
            writer.write("altfolder1=" + Folder.getText() + "\n");
            writer.write("basename=" + Folder.getText() + "\n");
            writer.write("audiobook=true" + "\n");

            try {
                File CoverFile = new File(Folder.getText() + "\\" + CoverField.getText());

                CoverImage = ImageIO.read(CoverFile);
                writer.write("artwork=" + CoverField.getText() + "\n");
                writer.write("imgwidth=" + CoverImage.getWidth() + "\n");
                writer.write("imgheight=" + CoverImage.getHeight() + "\n");
                isCover = true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, selectedLanguage[12]);
                isCover = false;
            }

            writer.write("editpointcount=" + Editpoints + "\n\n\n");

            int i;

            for (i = 1; i <= Editpoints; i++) {
                String Title = JOptionPane.showInputDialog(this, selectedLanguage[7] + i);

                if (Title == null) {
                    throw new Exception("go");
                }

                writer.write("[Editpoint_" + i + "]\n");

                if (isCover == true) {
                    writer.write("image=" + CoverField.getText() + "\n");
                }

                File audioFile = new File(Folder.getText() + "\\" + Title + ".m4a");

                try {
                    AudioFile af = AudioFileIO.read(audioFile);

                    System.out.println(af.getAudioHeader().getTrackLength() + "\n" + af.getFile().getName());
                    System.out.println("||");
                    writer.write("audiofile=" + Title + ".m4a\n");
                    writer.write("chapter=" + i + "\n");
                    writer.write("start="+time.asString()+".00" + "\n\n\n");

                    time.add(0, 0, af.getAudioHeader().getTrackLength());
                    
                } catch (CannotReadException exc) {
                    System.out.println("CannotReadException\n" + audioFile.exists());
                } catch (IOException exc) {
                    System.out.println("IOException");
                } catch (TagException exc) {
                    System.out.println("TagException");
                } catch (ReadOnlyFileException exc) {
                    System.out.println("ReadOnlyFileException");
                } catch (InvalidAudioFrameException exc) {
                    System.out.println("InvalidAudioFrameException");
                }
            }

            writer.close();
            JOptionPane.showMessageDialog(null, selectedLanguage[8]);
            System.exit(0);
        } catch (Exception e) {
            if (!e.getMessage().equals("go")) {
                JOptionPane.showMessageDialog(null, selectedLanguage[9]);
            }
        }
    }

    public void createMultiCD() {
        try {
            FileWriter     creator    = new FileWriter("Podcast.POD");
            BufferedWriter writer     = new BufferedWriter(creator);
            int            Editpoints = (int) Double.parseDouble(editpointField.getValue().toString());
            boolean        isCover;

            writer.write("[Podcast]\n");
            writer.write("altfolder1=" + Folder.getText() + "\n");
            writer.write("basename=" + Folder.getText() + "\n");

            try {
                File CoverFile = new File(Folder.getText() + "\\" + CoverField.getText());

                CoverImage = ImageIO.read(CoverFile);
                writer.write("artwork=" + CoverField.getText() + "\n");
                writer.write("imgwidth=" + CoverImage.getWidth() + "\n");
                writer.write("imgheight=" + CoverImage.getHeight() + "\n");
                isCover = true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, selectedLanguage[12]);
                isCover = false;
            }

            writer.write("editpointcount=" + Editpoints + "\n\n\n\n\n\n");

            String CDs = JOptionPane.showInputDialog(this, selectedLanguage[10]);

            if (CDs == null) {
                throw new Exception("go");
            }

            int CDNumber = (int) Double.parseDouble(CDs);
            int i;
            int allTracks[] = new int[1000];

            for (i = 1; i <= CDNumber; i++) {
                String Tracks = JOptionPane.showInputDialog(this, selectedLanguage[11] + i);

                if (Tracks == null) {
                    throw new Exception("go");
                }

                allTracks[i - 1] = (int) Double.parseDouble(Tracks);
            }

            int i1;
            int i2;
            int i3 = 0;

            for (i1 = 1; i1 <= CDNumber; i1++) {
                for (i2 = 1; i2 <= allTracks[i1 - 1]; i2++) {
                    i3++;
                    writer.write("[Editpoint_" + i3 + "]\n");

                    if (isCover == true) {
                        writer.write("image=" + CoverField.getText() + "\n");
                    }

                    writer.write("audiofile=" + i1 + "-" + i2 + " " + Folder.getText() + ".m4a\n");
                    writer.write("chapter=" + i + "\n");
                    writer.write("start=" + "\n\n\n");
                }
            }

            writer.close();
            JOptionPane.showMessageDialog(null, selectedLanguage[8]);
            System.exit(0);
        } catch (Exception e) {
            if (!e.getMessage().equals("go")) {
                JOptionPane.showMessageDialog(null, selectedLanguage[9]);
            }
        }
    }

    public static void main(String[] args) {
        LAF.LAF.setLAF(LAF.LAF.Windows);
        new audiobookMaker();
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
