package com.parnswir.unmp.media;

import java.io.File;
import java.io.IOException;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;

import android.content.ContentValues;

import com.parnswir.unmp.core.C;
import com.parnswir.unmp.core.DatabaseUtils;

public class MP3Handler extends FileHandler {
	
	@Override
	public void saveToDatabase(File file) {
		MP3File f = readAudioFile(file);
		MediaInformation info = new MediaInformation(f);
		insertTitle(info);
		upsertAttributes(info);
		insertRelations(info);
		insertAlbumArt(info);
	}
	
	@Override
	public void updateDatabaseEntryFor(File file) {

	}

	@Override
	public void deleteFromDatabase(File file) {

	}

	private void insertTitle(MediaInformation info) {
		ContentValues cv = info.toContentValuesForTable(C.TAB_TITLES);
		cv.put(C.COL_ID, DatabaseUtils.getNextIDForTable(C.TAB_TITLES, getDb()));
		cv.put(C.COL_LAST_PLAYED, "0");
		cv.put(C.COL_PLAY_COUNT, "0");
		getDb().insert(C.TAB_TITLES, null, cv);		
	}
	
	private void upsertAttributes(MediaInformation info) {
		for (int i = 0; i < C.TABLENAMES.length; i++) {
			String tableName = C.TABLENAMES[i];
			ContentValues cv = info.toContentValuesForTable(tableName);
			if (DatabaseUtils.tupleAlreadyInTable(tableName, cv, getDb())) {
				getDb().update(tableName, cv, C.COL_ID + " = ?", new String[] {DatabaseUtils.getIDForTuple(tableName, cv, getDb())});
			} else {
				DatabaseUtils.insertWithNewID(tableName, cv, getDb());
			}
		}
	}
	
	private void insertRelations(MediaInformation info) {
		ContentValues titleCV = info.toContentValuesForTable(C.TAB_TITLES);
		for (int i = 0; i < C.TABLENAMES.length; i++) {
			String tableName = C.TABLENAMES[i];
			String relationName = C.getRelationNameFor(tableName);
			ContentValues relationCV = generateRelationTuple(info, titleCV, tableName);
			DatabaseUtils.insertRelationWithNewID(relationName, relationCV, getDb());
		}		
	}

	private ContentValues generateRelationTuple(MediaInformation info, ContentValues titleCV, String tableName) {
		ContentValues cv = info.toContentValuesForTable(tableName);
		ContentValues relationCV = new ContentValues();
		relationCV.put(C.idNameFrom(tableName) + C.COL__ID, DatabaseUtils.getIDForTableEntryByName(tableName, cv.getAsString(C.idNameFrom(tableName)), getDb()));
		relationCV.put(C.COL_TITLE_ID, DatabaseUtils.getIDForTuple(C.TAB_TITLES, titleCV, getDb()));
		return relationCV;
	}
	
	private void insertAlbumArt(MediaInformation info) {
		ContentValues titleCV = info.toContentValuesForTable(C.TAB_TITLES);
		String titleID = DatabaseUtils.getIDForTuple(C.TAB_TITLES, titleCV, getDb());
		
		ContentValues cv = info.toContentValuesForTable(C.TAB_IMAGES);
		if (cv.get(C.COL_IMAGE_TYPE) == C.ALBUM_ART_BLOB) {
			cv = info.toContentValuesForTable(C.TAB_IMAGE_DATA);
			cv.remove("image_blob");
			if (! DatabaseUtils.tupleAlreadyInTable(C.TAB_IMAGE_DATA, cv, getDb())) {
				cv = info.toContentValuesForTable(C.TAB_IMAGE_DATA);
				DatabaseUtils.insertWithNewID(C.TAB_IMAGE_DATA, cv, getDb());
			}
		}
		
		cv = info.toContentValuesForTable(C.TAB_IMAGES);
		cv.put(C.COL_TITLE_ID, titleID);
		DatabaseUtils.insertWithNewID(C.TAB_IMAGES, cv, getDb());
	}

	private MP3File readAudioFile(File file) {
		MP3File f = null;
		try {
			f = (MP3File)AudioFileIO.read(file);
		} catch (CannotReadException e) {
			
		} catch (IOException e) {
			
		} catch (TagException e) {
			
		} catch (ReadOnlyFileException e) {
			
		} catch (InvalidAudioFrameException e) {
			
		}
		return f;
	}

}

@@ -567,16 +567,99 @@
 ler %7B%0A%09%0A
+%09private long titleID;%0A%09private long%5B%5D tableIDs = new long%5BC.TABLENAMES.length%5D;%0A%09%0A
 %09@Overri
@@ -1135,84 +1135,8 @@
 S);%0A
-%09%09cv.put(C.COL_ID, DatabaseUtils.getNextIDForTable(C.TAB_TITLES, getDb()));%0A
 %09%09cv
@@ -1200,16 +1200,26 @@
 %220%22);%0A%09%09
+titleID = 
 getDb().
@@ -1662,73 +1662,157 @@
 %0A%09%09%09
-%7D else %7B%0A%09%09%09%09DatabaseUtils.insertWithNewID(tableName, cv, getDb()
+%09tableIDs%5Bi%5D = Integer.valueOf(DatabaseUtils.getIDForTuple(tableName, cv, getDb()));%0A%09%09%09%7D else %7B%0A%09%09%09%09tableIDs%5Bi%5D = getDb().insert(tableName, null, cv
 );%0A%09
@@ -1883,78 +1883,8 @@
 ) %7B%0A
-%09%09ContentValues titleCV = info.toContentValuesForTable(C.TAB_TITLES);%0A
 %09%09fo
@@ -2083,74 +2083,28 @@
 le(i
-nfo, titleCV, tableName);%0A%09%09%09DatabaseUtils.insertRelationWithNewID
+);%0A%09%09%09getDb().insert
 (rel
@@ -2113,16 +2113,22 @@
 ionName,
+ null,
  relatio
@@ -2130,25 +2130,16 @@
 lationCV
-, getDb()
 );%0A%09%09%7D%09%09
@@ -2192,136 +2192,21 @@
 ple(
-MediaInformation info, ContentValues titleCV, String
+int
  table
-Name
 ) %7B%0A
-%09%09ContentValues cv = info.toContentValuesForTable(tableName);%0A
 %09%09Co
@@ -2281,25 +2281,35 @@
 ameFrom(
+C.TABLENAMES%5B
 table
-Name
+%5D
 ) + C.CO
@@ -2319,202 +2319,66 @@
 ID, 
-Da
 tab
-aseUtils.getIDForTableEntryByName(tableName, cv.getAsString(C.idNameFrom(tableName)), getDb()));%0A%09%09relationCV.put(C.COL_TITLE_ID, DatabaseUtils.getIDForTuple(C.TAB_TITLES, titleCV, getDb())
+leIDs%5Btable%5D);%0A%09%09relationCV.put(C.COL_TITLE_ID, titleID
 );%0A%09

Fix inconsistent IDs in relation tables