package com.coolweather.app.model;

import java.util.ArrayList;
import java.util.List;

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

import com.coolweather.app.db.CoolWeatherOpenHelper;

public class CoolWeatherDB {
	/**
	 * ݿ
	 */
	public static final String DB_NAME = "cool_weather";
	
	/**
	 * ݿİ汾
	 */
	public static final int VERSION = 1;
	
	private static CoolWeatherDB coolWeatherDB;
	
	private SQLiteDatabase db;
	
	/**
	 * 췽˽л
	 * @param context	
	 */
	private CoolWeatherDB(Context context) {
		CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION);
		db = dbHelper.getWritableDatabase();
	}
	
	/**
	 * ȡcoolWeatherBDʵ
	 * @return
	 */
	public synchronized static CoolWeatherDB getInstance() {
		if (coolWeatherDB == null) {
			coolWeatherDB = new CoolWeatherDB(context);
		}
		return coolWeatherDB;
	}
	
	/**
	 * Provinceʵ洢ݿ
	 * @param province
	 */
	public void saveProvince(Province province) {
		if (province != null) {
			ContentValues values = new ContentValues();
			values.put("province_name", province.getProvinceName());
			values.put("province_code", province.getProvinceCode());
			db.insert("Province", null, values);
		}
	}
	
	/**
	 * ݿжȡȫʡϢ
	 * @return
	 */
	public List<Province> loadProvinces() {
		List<Province> list = new ArrayList<Province>();
		Cursor cursor = db.query("Province", null, null, null, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Province province = new Province();
				province.setId(cursor.getInt(cursor.getColumnIndex("id")));
				province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
				province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
				list.add(province);
			} while (cursor.moveToNext());
		}
		
		return list;
	}
	
	public void saveCity(City city) {
		if (city != null) {
			ContentValues values = new ContentValues();
			values.put("city_name", city.getCityName());
			values.put("city_code", city.getCityCode());
			values.put("province_id", city.getProvinceId());
			db.insert("City", null, values);
		}
	}
	
	public List<City> loadCities(int provinceId) {
		List<City> list = new ArrayList<City>();
		Cursor cursor = db.query("City", null, "province_id = ?", new String[]{String.valueOf(provinceId)}, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				City city = new City();
				city.setId(cursor.getInt(cursor.getColumnIndex("id")));
				city.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
				city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code")));
				list.add(city);
			} while (cursor.moveToNext());
		}
		
		return list;
	}
	
	public void saveCountry(Country country) {
		if (country != null) {
			ContentValues values = new ContentValues();
			values.put("country_name", country.getCountryName());
			values.put("country_code", country.getCountryCode());
			values.put("city_id", country.getCityId());
			db.insert("Country", null, values);
		}
	}
	
	public List<Country> loadCountries(int cityId) {
		List<Country> list = new ArrayList<Country>();
		Cursor cursor = db.query("Country", null, "city_id = ?", new String[]{String.valueOf(cityId)}, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				Country country = new Country();
				country.setId(cursor.getInt(cursor.getColumnIndex("id")));
				country.setCountryName(cursor.getString(cursor.getColumnIndex("country_name")));
				country.setCountryCode(cursor.getString(cursor.getColumnIndex("country_code")));
				list.add(country);
			} while (cursor.moveToNext());
		}
		
		return list;
	}
}

@@ -323,17 +323,23 @@
 /**%0A%09 * 
-%DD%BF
+%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%90%8D
 %0A%09 */%0A%09p
@@ -405,11 +405,16 @@
 %09 * 
-%DD%BF%C4%B0%E6%B1%BE
+%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E7%89%88%E6%9C%AC
 %0A%09 *
@@ -545,11 +545,16 @@
 %09 * 
-%EC%B7%BD%CB%BD%D0%BB
+%E5%B0%86%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E7%A7%81%E6%9C%89%E5%8C%96
 %0A%09 *
@@ -569,16 +569,19 @@
 context%09
+%E4%B8%8A%E4%B8%8B%E6%96%87
 %0A%09 */%0A%09p
@@ -768,17 +768,18 @@
 /**%0A%09 * 
-%C8%A1
+%E8%8E%B7%E5%8F%96
 coolWeat
@@ -783,17 +783,19 @@
 eatherBD
-%CA%B5
+%E7%9A%84%E5%AE%9E%E4%BE%8B
 %0A%09 * @re
@@ -858,16 +858,31 @@
 nstance(
+Context context
 ) %7B%0A%09%09if
@@ -997,24 +997,25 @@
 /**%0A%09 * 
+%E5%B0%86
 Province
 %CA%B5%E6%B4%A2%DD%BF%0A%09 * 
@@ -1010,11 +1010,16 @@
 ince
-%CA%B5%E6%B4%A2%DD%BF
+%E5%AE%9E%E4%BE%8B%E5%AD%98%E5%82%A8%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93
 %0A%09 *
@@ -1342,14 +1342,21 @@
 %09 * 
-%DD%BF%D0%B6%C8%A1%C8%AB%CA%A1%CF%A2
+%E4%BB%8E%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E8%AF%BB%E5%8F%96%E5%85%A8%E5%9B%BD%E7%9C%81%E4%BB%BD%E4%BF%A1%E6%81%AF
 %0A%09 *

Update CoolWeatherDB.java