package com.bonait.bnframework.common.db.file;

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

import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_ALERTLOG;
import com.bonait.bnframework.common.db.mode.BPA_CRAFT;
import com.bonait.bnframework.common.db.mode.BPA_CRAFTPROCESS;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_LOG;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_MENU;
import com.bonait.bnframework.common.db.mode.BPA_MENUANDUSER;
import com.bonait.bnframework.common.db.mode.BPA_ORDER;
import com.bonait.bnframework.common.db.mode.BPA_ORDERLOG;
import com.bonait.bnframework.common.db.mode.BPA_ORDERLOGDESC;
import com.bonait.bnframework.common.db.mode.BPA_PROCESS;
import com.bonait.bnframework.common.db.mode.BPA_PROCESSModel;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SUBORDER;
import com.bonait.bnframework.common.db.mode.BPA_SUGAR;
import com.bonait.bnframework.common.db.mode.BPA_SYSTEMSET;
import com.bonait.bnframework.common.db.mode.BPA_USER;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据库帮助类
 */
public class DBHelper extends SQLiteOpenHelper {
	private static DBHelper instance;
	private static int version = 4;
	public DBHelper(Context context) {
		super(context, ConfigName.getInstance().dbPath, null, version);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		System.out.println("-----------------------------------------来了");
		createTables(db, prepareTableInfo());
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		System.out.println("-----------------------------------------更新来了");
	}
	public static DBHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DBHelper(context);
		}
		return instance;
	}
	private ArrayList<Table> prepareTableInfo() {
		ArrayList<Table> tables = new ArrayList<Table>();
		Table t;
		//t = new Table(TABLE_FOOD); // 通讯录
//		t.addFeild(new Feild("NAME", "text")); // 姓名
//		t.addFeild(new Feild("TYPE", "text")); // 号码
//		t.addFeild(new Feild("STAR", "text")); // 号码
//		t.addFeild(new Feild("PRICE", "text")); // 号码
//		tables.add(t);
		return tables;
	}
	/**
	 * 创建数据表
	 *
	 * @param db
	 *            数据库名称
	 * @param tables
	 *            所要创建的数据库表列表
	 */
	private void createTables(SQLiteDatabase db, ArrayList<Table> tables) {
		db.beginTransaction();
		for (Table table : tables) {
			db.execSQL(table.createSQL(""));
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}

	/**
	 * 创建数据库表
	 */
	public void CreateTables()
	{
		CreateTablesAll(BPA_MATERIAL.class,"id");//物料表
		CreateTablesAll(BPA_SILOS.class,null);//料仓管理表

		Map<String,String> smMap = new HashMap<String,String>();
		smMap.put("materialID","BPA_MATERIAL (id)");
		CreateRelationshipTables(BPA_SILOSANDMATERIAL.class,smMap);//料仓物料关联表

		CreateTablesAll(BPA_GOODS.class,null);//商品表
		CreateTablesAll(BPA_GOODSRECIPE.class,null);//商品配方明细表

		CreateTablesAll(BPA_ORDER.class,null);//订单表
		CreateTablesAll(BPA_SUBORDER.class,null);//订单子表

		CreateTablesAll(BPA_ALERTLOG.class,null);//预警日志表
		CreateTablesAll(BPA_LOG.class,null);//日志表
		CreateTablesAll(BPA_ORDERLOG.class,null);//订单制作记录表
		CreateTablesAll(BPA_ORDERLOGDESC.class,null);//订单制作记录描述表

		CreateTablesAll(BPA_SYSTEMSET.class,null);//系统设置表

		CreateTablesAll(BPA_USER.class,null);//账户表
		CreateTablesAll(BPA_MENU.class,null);//菜单表
		CreateTablesAll(BPA_MENUANDUSER.class,null);//菜单用户关联表
		CreateTablesAll(BPA_SUGAR.class,null);//果糖表

		CreateTablesAll(BPA_CRAFT.class,null);//工艺基础信息表
		CreateTablesAll(BPA_CRAFTPROCESS.class,null);//工艺流程表

		CreateTablesAll(BPA_PROCESS.class,null);//工序表
		CreateTablesAll(BPA_PROCESSModel.class,null);//工序模型表
		ForeignKeys();
	}
	/**
	 * 创建数据库表
	 * @param c
	 */
	private void CreateTablesAll(Class c,String premarykey)
	{
		if (premarykey == null){

			premarykey="";
		}
		SQLiteDatabase db =this.getWritableDatabase();
		db.beginTransaction();
		String TabName= c.getSimpleName();//表名称
		ArrayList<Table> tables = new ArrayList<Table>();
		Table t;
		t = new Table(TabName); // 创建表名称
		for (Field field : c.getFields()){
			String name=field.getName();
			String type=field.getType().getSimpleName();
			if(type.equals("String") || type.equals("Date"))
			{
				type="text";
			}
			t.addFeild(new Feild(name, type)); // 字段名称
		}
		tables.add(t);
		for (Table table : tables) {
			db.execSQL(table.createSQL(premarykey));
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}

	private void CreateRelationshipTables(Class c, Map<String,String> map){
		SQLiteDatabase db =this.getWritableDatabase();
		db.beginTransaction();
		String TabName= c.getSimpleName();//表名称
		ArrayList<Table> tables = new ArrayList<Table>();
		Table t;
		t = new Table(TabName); // 创建表名称
		for (Field field : c.getFields()){
			String name=field.getName();
			String type=field.getType().getSimpleName();
			if(type.equals("String") || type.equals("Date"))
			{
				type="text";
			}
			t.addFeild(new Feild(name, type)); // 字段名称
		}
		tables.add(t);
		for (Table table : tables) {
			db.execSQL(table.createSQL(map));
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	/**
	 * 开启外键约束
	 */
	private  void ForeignKeys(){
		SQLiteDatabase db =this.getWritableDatabase();
        db.beginTransaction();
		db.execSQL("PRAGMA foreign_keys = 1");

        db.setTransactionSuccessful();
		db.endTransaction();
	}

	/**
	 * 删除在创建
	 * @param c
	 * @param premarykey
	 */
	public void DeleteCreateTables(Class c,String premarykey)
	{
		if (premarykey == null){

			premarykey="";
		}
		SQLiteDatabase db =this.getWritableDatabase();
		db.beginTransaction();
		String TabName= c.getSimpleName();//表名称
		ArrayList<Table> tables = new ArrayList<Table>();
		Table t;
		t = new Table(TabName); // 创建表名称
		for (Field field : c.getFields()){
			String name=field.getName();
			String type=field.getType().getSimpleName();
			if(type.equals("String") || type.equals("Date"))
			{
				type="text";
			}
			t.addFeild(new Feild(name, type)); // 字段名称
		}
		tables.add(t);
		for (Table table : tables) {
			db.execSQL(table.deleteSql());
			db.execSQL(table.createSQL(premarykey));
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}
}
