package cn.net.caoyao.cpzshandset.db.dal.ythImage;

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

import com.bytebeats.py4j.Py4j;

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

import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.db.YthImageDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ProductDalImpl;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class YthImageProductDal extends ProductDalImpl{
	private YthImageDBHelper helper;

	public YthImageProductDal(Context context) {
		helper = new YthImageDBHelper(context);
	}

	/**
	 * 插入产品列表到数据库
	 * 
	 * @param products
	 */
	public void insertProductList(List<Product> products) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Py4j py4j = new Py4j();
		if (products != null) {
			for (int i = 0; i < products.size(); i++) {
				Product p = products.get(i);
				String rawSql = "select choiceCount from "+YthImageDBHelper.tb_product_choice_count +" where id=?";
				Cursor c = db.rawQuery(rawSql, new String[]{p.getId()});
//				Cursor c = db.rawQuery(rawSql, null);
				if (c.moveToNext()) {
					p.setChoiceCount(c.getInt(0));
				}
				c.close();
				
				String pNamePy = py4j.getPreLetterPY(p.getName());
				String sql = "insert into " + YthImageDBHelper.tb_product_info + "(id,name,pNamePy,nid,p_image_url,choiceCount) values(?,?,?,?,?,?)";
				db.execSQL(sql, new Object[] { p.getId(), p.getName(),pNamePy, p.getNid(),p.getImage() ,p.getChoiceCount()});
			}
		}
		py4j = null;
		db.close();
	}

	/**
	 * 删除所有的产品信息
	 */
	public void deleteAllProduct() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + YthImageDBHelper.tb_product_info;
		db.execSQL(delSql);
		db.close();
	}

	/**
	 * 获取查询结果
	 * 
	 * @param msg
	 * @return
	 */
	public List<Product> getPSearch(String msg) {
		msg = msg.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		List<Product> productInfos = new ArrayList<Product>();
		String sql = "SELECT * FROM " + YthImageDBHelper.tb_product_info +" WHERE id LIKE '%"+msg+"%' OR name LIKE '%"+msg+"%' OR nid LIKE '%"+msg+"%' OR pNamePy LIKE '%"+msg+"%'";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			Product pInfo = getProduct(cursor);
			productInfos.add(pInfo);
		}

		cursor.close();
		db.close();
		return productInfos;
	}

	/**
	 * 通过产品id获取产品信息
	 * 
	 * @param id
	 * @return
	 */
	public Product queryProductById(String id) {
		Product p = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthImageDBHelper.tb_product_info + " where id=?";
		Cursor c = db.rawQuery(sql, new String[] { id });
		if (c.moveToNext()) {
			p = getProduct(c);
		}
		c.close();
		db.close();
		return p;
	}

	/**
	 * 获取所有的产品列表信息
	 * 
	 * @return
	 */
	public List<Product> queryAllProductInfo() {
		List<Product> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthImageDBHelper.tb_product_info +" order by choiceCount DESC, id ASC";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getProduct(c));

		}
		c.close();
		db.close();
		return l;
	}

	public Product getProduct(Cursor c) {
		Product p = new Product();
		p.setId(c.getString(c.getColumnIndex("id")));
		p.setName(c.getString(c.getColumnIndex("name")));
		p.setNid(c.getString(c.getColumnIndex("nid")));
		p.setImage(c.getString(c.getColumnIndex("p_image_url")));
		p.setChoiceCount(c.getInt(c.getColumnIndex("choiceCount")));
		return p;
	}

	@Override
	public List<String> queryAllImages() {
		List<String> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthImageDBHelper.tb_product_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			String imageUrl = getProduct(c).getImage();
			if (!StringUtils.isEmpty(imageUrl)) {
				l.add(imageUrl);
			}
		}
		c.close();
		db.close();
		return l;
	}

	@Override
	public void updateChoiceCountById(String id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		//labelMode=(-1)*labelMode
		String updateSql = "UPDATE " + YthImageDBHelper.tb_product_info + " SET choiceCount=choiceCount+1 where id=?";
		db.execSQL(updateSql, new Object[]{id});
		String rawSql = "select count(id) as count from " + YthImageDBHelper.tb_product_choice_count +" where id = ?";
		Cursor c = db.rawQuery(rawSql, new String[]{id});
		int count = 0;
		if (c.moveToNext()) {
			count = c.getInt(0);
			LogUtil.i(TConfig.Echo, "count:" + count);
		}
		c.close();
		if (count > 0) {
			String updateCount = "UPDATE "+YthImageDBHelper.tb_product_choice_count+" SET choiceCount=choiceCount+1 where id=?";
			db.execSQL(updateCount, new Object[]{id});
		} else {
			String insertSql = "insert into "+YthImageDBHelper.tb_product_choice_count+" ( id,choiceCount) values (?,?)";
			db.execSQL(insertSql, new Object[]{id,1});
		}
		db.close();
	}

	
}
