package de.eightbitboy.hijacr.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;

import de.eightbitboy.hijacr.data.dao.Comic;
import de.eightbitboy.hijacr.data.dao.ComicDao;
import de.eightbitboy.hijacr.data.dao.DaoMaster;

public class Database {
	public static String DB_NAME = "comics.dao.db";

	private static Database instance;

	private Context context;

	private SQLiteDatabase db;

	private DaoMaster master;

	private Database(Context context) {
		this.context = context;
		setUp();
		insertDefaultData();
	}

	public static synchronized Database getInstance(Context context) {
		if (instance == null) {
			instance = new Database(context);
		}

		return instance;
	}

	private void setUp() {
		db = new DaoMaster.DevOpenHelper(context, DB_NAME, null).getWritableDatabase();
		master = new DaoMaster(db);
	}

	/**
	 * Populate the empty database with comic data.
	 */
	private void insertDefaultData() {
		if (getAllComics().isEmpty()) {
			ComicDao dao = createSessionDao();
			Comic comic;

			comic = new Comic(
					1L,
					"xkcd",
					"XKCD",
					"http://xkcd.com/",
					"https://xkcd.com/1/",
					null,
					null,
					"#comic img[src]",
					".comicNav a[rel=prev]",
					".comicNav a[rel=next]",
					false);
			dao.insert(comic);

			comic = new Comic(
					4L,
					"extralife",
					"ExtraLife",
					"http://www.myextralife.com/",
					"http://www.myextralife.com/comic/06172001/",
					null,
					null,
					".comic",
					".prev_comic_link",
					".next_comic_link",
					false);
			dao.insert(comic);

			comic = new Comic(
					5L,
					"explosm",
					"Explosm",
					"http://explosm.net/",
					"http://explosm.net/comics/15",
					null,
					null,
					"#main-comic",
					".previous-comic",
					".next-comic",
					false);
			dao.insert(comic);
		}

		/*
		comics.put("smbc", new ComicData(2, "smbc", "SMBC", "http://www.smbc-comics.com/",
				"http://www.smbc-comics.com/index.php?id=", 1, "#comic"));
		comics.put("vgcats", new ComicData(3, "vgcats", "VgCats", "http://www.vgcats.com/",
				"http://www.vgcats.com/comics/?strip_id=", 0, "tbody div img[width]"));
		comics.put("xkcd", new ComicData(1, "xkcd", "XKCD", "http://xkcd.com/",
				"http://xkcd.com/", 1, "#comic img[src]"));
		*/
	}

	private ComicDao createSessionDao() {
		return master.newSession().getComicDao();
	}

	public List<Comic> getAllComics() {
		return createSessionDao().loadAll();
	}

	public Comic getComicById(long id) {
		return createSessionDao().load(id);
	}

	public void updateComic(Comic comic) {
		createSessionDao().update(comic);
	}
}

@@ -1299,32 +1299,276 @@
 insert(comic);%0A%0A
+%09%09%09comic = new Comic(%0A%09%09%09%09%092L,%0A%09%09%09%09%09%22smbc%22,%0A%09%09%09%09%09%22SMBC%22,%0A%09%09%09%09%09%22http://www.smbc-comics.com/%22,%0A%09%09%09%09%09%22http://www.smbc-comics.com/index.php?id=1%22,%0A%09%09%09%09%09null,%0A%09%09%09%09%09null,%0A%09%09%09%09%09%22#comic%22,%0A%09%09%09%09%09%22.prev%22,%0A%09%09%09%09%09%22.next%22,%0A%09%09%09%09%09false);%0A%09%09%09dao.insert(comic);%0A%0A
 %09%09%09comic = new C
@@ -2091,155 +2091,8 @@
 %09/*%0A
-%09%09comics.put(%22smbc%22, new ComicData(2, %22smbc%22, %22SMBC%22, %22http://www.smbc-comics.com/%22,%0A%09%09%09%09%22http://www.smbc-comics.com/index.php?id=%22, 1, %22#comic%22));
 %0A%09%09c
@@ -2254,130 +2254,8 @@
 ));%0A
-%09%09comics.put(%22xkcd%22, new ComicData(1, %22xkcd%22, %22XKCD%22, %22http://xkcd.com/%22,%0A%09%09%09%09%22http://xkcd.com/%22, 1, %22#comic img%5Bsrc%5D%22));%0A
 %09%09*/

Add smbc comic