package com.cwbuyer.lib;

import java.util.LinkedList;

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

import com.cwbuyer.lib.Calculator.Arith;
import com.cwbuyer.main.ManageAccount.AccountData;

public class AccountUtil {
	
	// 新增 Item 的值寫入到Account DB 
	public static void insertValue(Context cnt, int itemId, int accountId, int addValue){
		SQLiteDatabase db = Utilis.getDB(cnt);
		if(db != null){
			try{
				// 取得帳戶的 accmoney
				double money = getMoney(cnt, db, accountId);
				double rate = getAccountRate(db,accountId);
//				int finalMoney = money + (int)((double)addValue / rate);
				double finalMoney = (int)(money + Arith.round(Arith.div((double)addValue, rate), 4));
				if(updateMoney(cnt, db, accountId, finalMoney) == true){
//					Toast.makeText(cnt, "新增帳戶金額成功", Toast.LENGTH_SHORT).show();
				}else{
//					Toast.makeText(cnt, "新增帳戶金額失敗", Toast.LENGTH_SHORT).show();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				db.close();
			}
		}
	}
	
	public static double getAccountRate(SQLiteDatabase db, int accountId){
		double rate = 0;
		try{
			Cursor cursor = db.rawQuery("Select accrate from account where _id="+ accountId, null);
			if(cursor != null){
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					rate = cursor.getDouble(0);
				}
				cursor.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return rate;
	}
	
	// Spinner 使用
	public static int getAccountIndex(LinkedList<AccountData> list, int nID){
		boolean bFind = false;
		int index = 0;
		try{
			for(int i = 0; i < list.size() && bFind == false; i++){
				if(list.get(i).nID == nID){
					index = i;
					bFind = true;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return index;
	}
	
	// 修改資料的值
	public static void updateValue(Context cnt, int itemId, int oldAccountId, int newAccountId, int oldValue, int newValue){
		SQLiteDatabase db = Utilis.getDB(cnt);
		try{
			// 取得帳戶的 accmoney
			double oldBankMoney = getMoney(cnt, db, oldAccountId);
			double newBankMoney = getMoney(cnt, db, newAccountId);
			double oldRate = getAccountRate(db, oldAccountId);
			double newRate = getAccountRate(db, newAccountId);
			if(oldAccountId != newAccountId){
				// 舊的帳號就先扣掉舊的錢
//				oldBankMoney -= ((double)oldValue / oldRate);
				oldBankMoney -= Arith.round(Arith.div((double)oldValue, oldRate), 4);
				updateMoney(cnt, db, oldAccountId, oldBankMoney);
				
//				newBankMoney += ((double)newValue / newRate);
				newBankMoney += Arith.round(Arith.div((double)newValue, newRate), 4);
				updateMoney(cnt, db, newAccountId, newBankMoney);
			}else{
				// 先扣掉舊的再加新的
//				newBankMoney -= ((double)oldValue / oldRate);
//				newBankMoney += ((double)newValue / newRate);
				newBankMoney -= Arith.round(Arith.div((double)oldValue, oldRate), 4);
				newBankMoney += Arith.round(Arith.div((double)newValue, newRate), 4);
				updateMoney(cnt, db, newAccountId, newBankMoney);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(db != null){
			db.close();
			db = null;
		}
	}
	// 修改資料的值
	public static void updateValue(Context cnt, SQLiteDatabase db, int oldAccountId, int newAccountId, int oldValue, int newValue){
//		SQLiteDatabase db = Utilis.getDB(cnt);
		try{
			// 取得帳戶的 accmoney
			double oldBankMoney = getMoney(cnt, db, oldAccountId);
			double newBankMoney = getMoney(cnt, db, newAccountId);;
			if(oldAccountId != newAccountId){
				// 舊的帳號就先扣掉舊的錢
				oldBankMoney -= oldValue;
				updateMoney(cnt, db, oldAccountId, oldBankMoney);
				
				newBankMoney += newValue;
				updateMoney(cnt, db, newAccountId, newBankMoney);
			}else{
				// 先扣掉舊的再加新的
				newBankMoney -= oldValue;
				newBankMoney += newValue;
				updateMoney(cnt, db, newAccountId, newBankMoney);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(db != null){
			db.close();
			db = null;
		}
	}
	
	public static double getMoney(Context cnt, SQLiteDatabase db, int accountId){
		double money = 0;
		Cursor cursor = db.rawQuery("Select accmoney from account where _id=" + accountId, null);
		if(cursor != null){
			try{
				cursor.moveToFirst();
				money = cursor.getDouble(0);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
			}
		}
		return money;
	}
	
	public static boolean updateMoney(Context cnt, SQLiteDatabase db, int accountId, double money){
		boolean bOK = false;
		try{
			ContentValues value = new ContentValues();
			value.put("accmoney", money);
				int success = db.update(com.cwbuyer.lib.TbName.ACCOUNT, value,
						"_id=?", new String[]{"" + accountId});
				if(success > 0){
//					Toast.makeText(cnt, "修改Account存款成功", Toast.LENGTH_SHORT).show();
					bOK = true;
				}else{
//					Toast.makeText(cnt, "修改Account存款失敗", Toast.LENGTH_SHORT).show();
				}
		}catch(Exception e){
			e.printStackTrace();
//			Toast.makeText(cnt, "修改Account存款失敗", Toast.LENGTH_SHORT).show();
		}
		return bOK;
	}
}
