package com.example.godtemper.db;

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

import com.example.godtemper.model.City;
import com.example.godtemper.model.County;
import com.example.godtemper.model.Province;

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

public class GodTemperDB {
	/**
	 * Êý¾Ý¿âÃû
	 */
	public static final String DB_NAME = "GodTemper";
	
	/**
	 * Êý¾Ý¿â°æ±¾
	 */
	public static final int VERSION = 1;
	private static GodTemperDB godTemperDB;
	private SQLiteDatabase db;
	
	private GodTemperDB(Context context){
		GodTemperOpenHelper dbHelper = new GodTemperOpenHelper(context, DB_NAME, null, VERSION);
		db = dbHelper.getWritableDatabase();
	}
	
	/**
	 * »ñÈ¡godTemperDBµÄÊµÀý
	 * @param context
	 * @return
	 */
	public synchronized static GodTemperDB getInstance(Context context){
		if(godTemperDB == null){
			godTemperDB = new GodTemperDB(context);
		}
		return godTemperDB;
	}
	/**
	 * ½«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;
	}
	/**
	 * ½«CityÊµÀý´æ´¢µ½Êý¾Ý¿â
	 * @param city
	 */
	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);
		}
	}
	/**
	 * ´ÓÊý¾Ý¿â¶ÁÈ¡Ä³Ê¡ÏÂËùÓÐµÄ³ÇÊÐÐÅÏ¢
	 * @param provinceId
	 * @return
	 */
	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")));
				city.setProvinceId(provinceId);
				list.add(city);
			}while(cursor.moveToNext());
		}
		return list;
	}
	/**
	 * ½«CountyÊµÀý´æ´¢µ½Êý¾Ý¿â
	 */
	public void saveCounty(County county){
		if(county != null){
			ContentValues values = new ContentValues();
			values.put("county_name", county.getCountyName());
			values.put("county_code", county.getCountyCode());
			values.put("city_id", county.getCityId());
			db.insert("County", null, values);
		}
	}
	/**
	 * ´ÓÊý¾Ý¿â¶ÁÈ¡Ä³³ÇÊÐÏÂËùÓÐÏØµÄÐÅÏ¢
	 */
	public List<County>loadCounties (int cityId){
		List<County>list = new ArrayList<County>();
		Cursor cursor = db.query("County", null, "city_id = ?", 
				new String[]{String.valueOf(cityId)},  null, null, null);
		if(cursor.moveToFirst()){
			do{
				County county = new County();
				county.setId(cursor.getInt(cursor.getColumnIndex("id")));
				county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
				county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
				county.setCityId(cityId);
				list.add(county);
			}while(cursor.moveToNext());
		}
		return list;
	}
}
