package com.itheima.mobileguard.db.dao;

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 android.os.SystemClock;

import com.itheima.mobileguard.db.BlackNumberDBOpenHelper;
import com.itheima.mobileguard.domain.BlackNumberInfo;

/**
 * ºÚÃûµ¥Êý¾Ý¿âµÄÔöÉ¾¸Ä²é¹¤¾ßÀà
 * 
 * @author Administrator
 * 
 */
public class BlackNumberDao {
	private BlackNumberDBOpenHelper helper;

	public BlackNumberDao(Context context) {
		helper = new BlackNumberDBOpenHelper(context);
	}

	/**
	 * Ìí¼ÓºÚÃûµ¥ºÅÂë
	 * 
	 * @param number
	 *            ºÅÂë
	 * @param mode
	 *            À¹½ØÄ£Ê½
	 * @return ÊÇ·ñÌí¼Ó³É¹¦
	 */
	public boolean add(String number, String mode) {
		// »ñÈ¡µ½¿ÉÐ´µÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("number", number);
		values.put("mode", mode);
		long rowid = db.insert("blackinfo", null, values);
		if (rowid == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * É¾³ýºÚÃûµ¥ºÅÂë
	 * 
	 * @param number
	 *            ºÅÂë
	 * @return ÊÇ·ñÉ¾³ý³É¹¦
	 */
	public boolean delete(String number) {
		// »ñÈ¡µ½¿ÉÐ´µÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getWritableDatabase();
		int rownumber = db.delete("blackinfo", "number=?",
				new String[] { number });
		if (rownumber == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * ÐÞ¸ÄºÚÃûµ¥ºÅÂëµÄÀ¹½ØÄ£Ê½
	 * 
	 * @param number
	 *            ºÅÂë
	 * @param newmode
	 *            ÐÂµÄÀ¹½ØÄ£Ê½
	 * @return ÊÇ·ñÐÞ¸Ä³É¹¦
	 */
	public boolean changeBlockMode(String number, String newmode) {
		// »ñÈ¡µ½¿ÉÐ´µÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("mode", newmode);
		int rownumber =db.update("blackinfo", values, "number=?", new String[]{number});
		if (rownumber == 0) {
			return false;
		} else {
			return true;
		}
	}
	/**
	 * ·µ»ØÒ»¸öºÚÃûµ¥ºÅÂëÀ¹½ØÄ£Ê½
	 * @param number Òª²éÑ¯µÄºÚÃûµ¥ºÅÂë
	 * @return 0²»ÊÇºÚÃûµ¥ºÅÂë²»À¹½Ø 1È«²¿À¹½Ø 2¶ÌÐÅÀ¹½Ø 3µç»°À¹½Ø 
	 */
	public String findBlockMode(String number){
		String mode = "0";
		// »ñÈ¡µ½¿É¶ÁµÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("blackinfo", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
		if(cursor.moveToNext()){
			mode = cursor.getString(0);
		}
		cursor.close();
		db.close();
		return mode;
	}
	
	
	/**
	 * ²éÑ¯È«²¿µÄºÚÃûµ¥ºÅÂë
	 */
	public List<BlackNumberInfo> findAll(){
		// µÃµ½¿É¶ÁµÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("blackinfo", new String[]{"number","mode"}, null, null, null, null, null);
		List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while(cursor.moveToNext()){
			BlackNumberInfo info = new BlackNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setMode(mode);
			info.setNumber(number);
			blackNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(3000);
		return blackNumberInfos;
	}
	
	/**
	 * ·ÖÒ³²éÑ¯Êý¾Ý¿âµÄ¼ÇÂ¼
	 * @param pagenumber µÚ¼¸Ò³£¬Ò³Âë ´ÓµÚ0Ò³¿ªÊ¼
	 * @param pagesize   Ã¿Ò»¸öÒ³ÃæµÄ´óÐ¡
	 */
	public List<BlackNumberInfo> findPart(int pagenumber,int pagesize){
		// µÃµ½¿É¶ÁµÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select number,mode from blackinfo limit ? offset ?", new String[]{String.valueOf(pagesize),
				String.valueOf(pagesize*pagenumber)
		});
		List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while(cursor.moveToNext()){
			BlackNumberInfo info = new BlackNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setMode(mode);
			info.setNumber(number);
			blackNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(30);
		return blackNumberInfos;
	}
	
	
	/**
	 * ·ÖÅú¼ÓÔØÊý¾Ý
	 * @param startIndex ´ÓÄÄ¸öÎ»ÖÃ¿ªÊ¼¼ÓÔØÊý¾Ý
	 * @param maxCount   ×î¶à¼ÓÔØ¼¸ÌõÊý¾Ý
	 */
	public List<BlackNumberInfo> findPart2(int startIndex,int maxCount){
		// µÃµ½¿É¶ÁµÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select number,mode from blackinfo order by _id desc limit ? offset ?", new String[]{String.valueOf(maxCount),
				String.valueOf(startIndex)
		});
		List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
		while(cursor.moveToNext()){
			BlackNumberInfo info = new BlackNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setMode(mode);
			info.setNumber(number);
			blackNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(30);
		return blackNumberInfos;
	}
	
	
	/**
	 * »ñÈ¡Êý¾Ý¿âµÄ×ÜÌõÄ¿¸öÊý
	 * @param pagenumber µÚ¼¸Ò³£¬Ò³Âë ´ÓµÚ0Ò³¿ªÊ¼
	 * @param pagesize   Ã¿Ò»¸öÒ³ÃæµÄ´óÐ¡
	 */
	public int getTotalNumber(){
		// µÃµ½¿É¶ÁµÄÊý¾Ý¿â
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from blackinfo",null);
		cursor.moveToNext();
		int count = cursor.getInt(0);
		cursor.close();
		db.close();
		return count;
	}
}
