/*
 * Copyright (C) 2013 Reese Wilson | Shiny Mayhem

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.shinymayhem.radiopresets;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DbContractRadio {
	
	
	private static final String TEXT_TYPE = " TEXT";
	private static final String COMMA_SEP = ",";
	private static final String SQL_CREATE_STATIONS =
	    "CREATE TABLE " + DbContractRadio.EntryStation.TABLE_NAME + " (" +
		DbContractRadio.EntryStation._ID + " INTEGER PRIMARY KEY AUTOINCREMENT" + COMMA_SEP +
		DbContractRadio.EntryStation.COLUMN_NAME_PRESET_NUMBER + " INTEGER NOT NULL" + COMMA_SEP +
	    DbContractRadio.EntryStation.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
	    DbContractRadio.EntryStation.COLUMN_NAME_URL + TEXT_TYPE + " NOT NULL" +
	    " )";
	//TODO insert sample stations?

	private static final String SQL_DELETE_STATIONS = "DROP TABLE IF EXISTS " + DbContractRadio.EntryStation.TABLE_NAME;
	
	private DbContractRadio() {}
	
	public static abstract class EntryStation implements BaseColumns {
		public static final String TABLE_NAME = "stations";
	    public static final String COLUMN_NAME_PRESET_NUMBER = "preset_number";
	    public static final String COLUMN_NAME_TITLE = "title";
	    public static final String COLUMN_NAME_URL = "url";
	    
	}
	
	public static class DbHelperRadio extends SQLiteOpenHelper {

		public static final String DATABASE_NAME = "Radio.db";
		public static final int DATABASE_VERSION = 3; //if this is changed, update onUpgrade() to not delete data
		
		public DbHelperRadio(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(SQL_CREATE_STATIONS);
			Log.i(getClass().toString(), "Created database");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	        // discard the data and start over
	        db.execSQL(SQL_DELETE_STATIONS);
	        onCreate(db);

		}

	}

	
}

@@ -1471,44 +1471,680 @@
 %22 )%22
-;%0D%0A%09//TODO insert sample stations?%0D%0A
+ +%0D%0A%09    %22;%22;%0D%0A%09%0D%0A%09private static final String ADD_SAMPLE_STATIONS = %0D%0A%09%09%22insert into %22 + DbContractRadio.EntryStation.TABLE_NAME + %22 (%22 +%0D%0A%09%09    DbContractRadio.EntryStation.COLUMN_NAME_PRESET_NUMBER + COMMA_SEP +%0D%0A%09%09    DbContractRadio.EntryStation.COLUMN_NAME_TITLE + COMMA_SEP +%0D%0A%09%09    DbContractRadio.EntryStation.COLUMN_NAME_URL +%0D%0A%09%09    %22 )%22 + %0D%0A%09%09    %22  %22 +%0D%0A%09%09    %22select 1, 'ElectroSwing Revolution', 'http://streamplus17.leonex.de:39060' union %22 +%0D%0A%09%09    %22select 2, 'Jazz Radio Electroswing', 'http://jazz-wr04.ice.infomaniak.ch/jazz-wr04-128.mp3' union %22 +%0D%0A%09%09    %22select 3, 'Bart&Baker', 'http://jazz-wr14.ice.infomaniak.ch/jazz-wr14-128.mp3' %22 + %0D%0A%09%09    %22;%22;
 %0D%0A%09p
@@ -3073,16 +3073,53 @@
 IONS);%0D%0A
+%09%09%09db.execSQL(ADD_SAMPLE_STATIONS);%0D%0A
 %09%09%09Log.i

add sample stations on install