package com.cwbuyer.lib;

import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.cwbuyer.format.KeySet;
import com.pwbuyer.main.R;

// 該App獨有的 util
@SuppressLint({ "DefaultLocale", "InflateParams" })
public class AppUtil {
	
	public interface Format{
		public int SourceNo = 1;
		public int Batch = 2;
		public int Company = 3;
		public int System = 4;
		public int BatchSourceNo = 6;
		public int YearSeason = 7;
	}
	
	// 傳回最近一次採購的 BATCH
	public static String getRecentBatch(Context context){
		String recentBatch = null;
			SQLiteDatabase db = Utilis.getDB(context);
			if(db != null){
				try{
					Cursor cursor = db.rawQuery("Select BATCH from qdetail Order by BATCH Desc limit 1", null);
					if(cursor != null){
						if(cursor.getCount() > 0){
							cursor.moveToFirst();
							recentBatch = cursor.getString(0);
						}
						cursor.close();
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					db.close();
				}
		}
		return recentBatch;
	}
	
	// 傳回上次採購的時間，沒有則回傳今天
		public static String getRecentDate(Context context){
			String recentDate = null;
			SQLiteDatabase db = Utilis.getDB(context);
			if(db != null){
				try{
					Cursor cursor = db.rawQuery("Select CREATEDATETIME from qdetail Order by CREATEDATETIME Desc limit 1", null);
					if(cursor != null){
						if(cursor.getCount() > 0){
							cursor.moveToFirst();
							recentDate = cursor.getString(0);
							recentDate = recentDate.replace("-", "/");
						}
						cursor.close();
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					db.close();
				}
			}
			if(recentDate == null){
				recentDate = DateUtil.getCurrentDate();
			}
			return recentDate;
		}
	
	// 確保來源編號保持在3位數
	// 也可以用 Utilis.formatNumber(String,int)
	public static String formatSource(String s) {
		if (s.length() == 0){
			s = "000";
		}else if (s.length() == 1){
			s = "00" + s;
		}else if(s.length() == 2){
			s = "0" + s;
		}
		return s;
	}
	
	public static String getYearString(String date){
		String year = null;
		try{
			year = date.substring(2, 4);
		}catch(Exception e){
			e.printStackTrace();
		}
		return year;
	}
	
	public static String getCurrentBatch(Context cnt){
		String batch = null;
		SQLiteDatabase db = Utilis.getDB(cnt);
		if (db != null) {
			try {
				Cursor cursor = db.rawQuery("Select BATCH from qdetail order by CREATEDATETIME DESC Limit 1", null);
				if (cursor != null) {
					if (cursor.getCount() > 0) {
						cursor.moveToFirst();
						batch = cursor.getString(0);
					}
					cursor.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				db.close();
			}
		}
		return batch;
	}
	
	public static String getCurrentBatchDate(Context cnt){
		String batch = null;
		SQLiteDatabase db = Utilis.getDB(cnt);
		if (db != null) {
			try {
				Cursor cursor = db.rawQuery("Select CREATEDATETIME from qdetail order by CREATEDATETIME DESC Limit 1", null);
				if (cursor != null) {
					if (cursor.getCount() > 0) {
						cursor.moveToFirst();
						batch = cursor.getString(0);
					}
					cursor.close();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				db.close();
			}
		}
		return batch;
	}
	
	
	public static void writeVerify(String strInfos){
//		StringBuffer buf = new StringBuffer();
//		for(int x = 0; x < strInfos.length; x ++){
//			buf.append(strInfos[x]);
//			if((x + 1) < strInfos.length){
//				buf.append(",");
//			}
//		} //FileUtil.FILE_COMPARE 相當於 ../cwbuyer/ini.sys  keyset.key=="cwbuyer123"
		FileUtil.writeToFile(FileUtil.FILE_COMPARE, AESHelper.encrypt(strInfos,KeySet.KEY));
	}
	
	public static void compareVarify(Context cnt){
		String verifyText = AESHelper.decrypt(FileUtil.getFileText(FileUtil.FILE_COMPARE), KeySet.KEY);
		try{
			if(verifyText != null && verifyText.length() > 0){
				// do nothing
				Utilis.setStringSet(cnt, PrefKey.LIMIT_TIME_FIRST, verifyText);
			    String limit  = DateUtil.addDate(verifyText,PrefKey.ADD_DATE);//15 天
			    Utilis.setStringSet(cnt, PrefKey.LIMIT_TIME_LAST, limit);
			}else{
				String date = Utilis.getStringSet(cnt, PrefKey.LIMIT_TIME_FIRST, "");
				if(date != null && date.length() > 0){
					writeVerify(date);
				}else{
					String strDate = DateUtil.getCurrentDate();
					writeVerify(strDate);
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	// 需要加上 001 002 ...
	// 需判斷 DB 裡面相同的 batch or date 的最後一筆 然後再加 1
	public static String getItemNo(Context cnt, int rule, String value) {
		StringBuffer query = new StringBuffer();
		StringBuffer buf = new StringBuffer();
		String strFindValue = null;
		
		if(value == null || value.length() <= 0){
			return "";
		}
		
		boolean bFind = false;
		SQLiteDatabase db = Utilis.getDB(cnt);
		if (db != null) {
			try {
				switch(rule){
					case Format.SourceNo:
					{
//						length(GOODSNO)=7  [R]將所有的Order By MID DESC LIMIT 1 全改為 Order By GOODSNO DESC LIMIT 1
						//因為若用 MID 結果 追加時用了舊貨號 結果流水號有回到從前了
						int length = value.length() + 3; // 來源號 ＋ 3碼的流水號
						query.append("SELECT GOODSNO from qdetail where length(GOODSNO)=" + length + " and GOODSNO like '" + value + "%' Order By GOODSNO DESC LIMIT 1");
						break;
					}
					case Format.Batch:
					{
						int length = value.length() + 3; // 批號 ＋ 3碼的流水號
						query.append("SELECT GOODSNO from qdetail where length(GOODSNO)=" + length + " and GOODSNO like '" + value + "%' Order By GOODSNO DESC LIMIT 1");
						break;
					}
					case Format.Company:
					{
						int length = value.length() + 5; // 廠商編號 ＋ 5碼的流水號
						query.append("SELECT GOODSNO from qdetail where length(GOODSNO)=" + length + " and GOODSNO like '" + value + "%' Order By GOODSNO DESC LIMIT 1");
						break;
					}
					case Format.System:
					{
						query.append("SELECT GOODSNO from qdetail where length(GOODSNO)=7 and GOODSNO like '"+ value + "%' order by GOODSNO desc limit 1");
						break;
					}
					case Format.BatchSourceNo:
					{
						int length = value.length() + 3; // 廠商編號+批號 ＋ 3碼的流水號
						query.append("SELECT SOURCENO from qdetail where length(SOURCENO)=" + length + " and SOURCENO like '" + value + "%' Order By SOURCENO DESC LIMIT 1");
						break;
					}
					case Format.YearSeason:
					{
						int length = value.length() + 4; //  4碼的流水號
						query.append("SELECT GOODSNO from qdetail where length(GOODSNO)=" + length + " and GOODSNO like '" + value + "%' Order By GOODSNO DESC LIMIT 1");
						break;
					}
				}
				Cursor cursor = db.rawQuery(query.toString(), null);
				if (cursor != null) {
					if (cursor.getCount() > 0) {
						cursor.moveToFirst();
						strFindValue = cursor.getString(0);
						bFind = true;
					}
					cursor.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				db.close();
			}
		}
		buf.append(value);
		
		if (bFind == true) {
			int length = strFindValue.length();
			int lastNoLength = 0;
			
			// 取最後幾碼的數值[R]將批號改為流水號3
			if(rule == Format.SourceNo || rule == Format.Batch || rule == Format.BatchSourceNo){  // 3
				lastNoLength = 3;
			}else if(rule == Format.Company || rule == Format.System){ // 5
				lastNoLength = 5;
			}else if(rule == Format.YearSeason ){ // 5
				lastNoLength = 4;
			}
			String strFlue = strFindValue.substring(length - lastNoLength, length);
			buf.append(Utilis.formatNumber("" + (Utilis.toInt(strFlue) + 1), lastNoLength));
		} else {
			if(rule == Format.SourceNo  || rule == Format.Batch || rule == Format.BatchSourceNo){  // 3
				buf.append("001");
			}else if(rule == Format.Company || rule == Format.System){ // 5
				buf.append("00001");
			}else if(rule == Format.YearSeason ){ // 4
				buf.append("0001");
			}
		}
		return buf.toString().toUpperCase();
	}
	
	public static void regDialog(final Activity activity){
		LayoutInflater factory = LayoutInflater.from(activity);
	    final View v1 = factory.inflate(R.layout.register, null);
	    final EditText edtReg = (EditText) v1.findViewById(R.id.edtReg);
	    edtReg.setText(Utilis.getStringSet(activity, PrefKey.LIMIT_TIME_ZONB, ""));
	    new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.sys_msg_regtitle)+Utilis.getStringSet(activity, PrefKey.LIMIT_TIME_ZONA,"")).setView(v1).setNeutralButton("OK", new DialogInterface.OnClickListener() {
	      public void onClick(DialogInterface dialog, int which) {
	        Utilis.setStringSet(activity, PrefKey.LIMIT_TIME_ZONB, edtReg.getText().toString());
	        if (edtReg.getText().toString().length() != 13) {
	          new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.sys_msg_title)).setMessage(activity.getString(R.string.sys_msg_reg_plzinput)).setPositiveButton("OK", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            	regDialog(activity);
	            }
	          }).setCancelable(false).show();
	        }else{
	      	  // 判斷 註冊碼是否正確
	      	  if(AppUtil.isRegSuccess(activity) == true){
	      		  new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.sys_msg_title)).setMessage(activity.getString(R.string.sys_msg_reg_success)).setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	Utilis.setIntSet(activity, PrefKey.PAY_STATE, 1);
	                    }
	                  }).setCancelable(false).show();
	      	  }else{
	      		  new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.sys_msg_title)).setMessage(activity.getString(R.string.sys_msg_reg_plzinput)).setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	regDialog(activity);
	                    }
	                  }).setCancelable(false).show();
	      	  }
	        }
	      }
	    }).setNegativeButton("離開", new DialogInterface.OnClickListener() {
	      public void onClick(DialogInterface dialog, int which) {
	    	  activity.finish();
	      }
	    }).setPositiveButton("購買", new DialogInterface.OnClickListener() {
		      public void onClick(DialogInterface dialog, int which) {
		    	  if(Utilis.haveInternet(activity)){
		    		  try{
		    		      Intent goToBuy = null;
		    		      goToBuy = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.cwbuyer.net"));
		    		      activity.startActivity(goToBuy);
		    		  }catch(Exception e){
		    			  e.printStackTrace();
		    		  }finally{
//		    			  regDialog(activity);
		    			  activity.finish();
		    		  }
		    	  }else{
		    		  Toast.makeText(activity, "需開啟3G or Wifi無線網路", Toast.LENGTH_SHORT).show();
		    		  regDialog(activity);
		    	  }
		      }
		})
	    .setCancelable(false).show();
  }
	
	public static void goRegistAt(Context context,String Regday){
	    //String aDay[]=Utilis.getMonthDate().split("~");
	    String begins=DateUtil.getSystemTime().substring(8);
	    String limit=DateUtil.addDate(Regday, PrefKey.ADD_YEAR);//到期日
	    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_LAST, limit);
	    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_FIRST,Regday);		            		    
	    String limits =limit.substring(3, 4)+limit.substring(5,7)+limit.substring(8,10)+begins;
	    StringBuffer buf = new StringBuffer(); //make a ZONA
	    buf.append(""+((Utilis.toInt(limits.substring(0,1))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(1,2))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(2,3))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(3,4))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(4,5))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(5,6))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(6,7))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(7,8))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(8,9))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(9,10))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(10,11))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(11))+5) %10));
	    int limitn=0;
	    for (int i=0;i<12;i++) {limitn=limitn+Utilis.toInt(buf.toString().substring(i,i+1));}
	    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_ZONA, (""+(limitn %10))+buf.toString());
	    
	    limits =Regday.substring(3, 4)+Regday.substring(5,7)+Regday.substring(8,10)+begins;
	    buf = new StringBuffer(); //make a ZONA
	    buf.append(""+((Utilis.toInt(limits.substring(0,1))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(1,2))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(2,3))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(3,4))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(4,5))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(5,6))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(6,7))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(7,8))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(8,9))+5) %10))
	    .append(""+((Utilis.toInt(limits.substring(9,10))+1) %10))
	    .append(""+((Utilis.toInt(limits.substring(10,11))+3) %10))
	    .append(""+((Utilis.toInt(limits.substring(11))+5) %10));
	     limitn=0;
	    for (int i=0;i<12;i++) {limitn=limitn+Utilis.toInt(buf.toString().substring(i,i+1));}
	    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_ZONB, (""+(limitn %10))+buf.toString());
	    
          try{   //只有在 swbuyer全新安裝
	      File file = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+PrefKey.INI_SDB);
	      SQLiteDatabase dbc = SQLiteDatabase.openOrCreateDatabase(file, null);
	      
	      StringBuffer mySqlBuf = new StringBuffer();
			mySqlBuf.append("update  system ");
			mySqlBuf.append("set  T5='"+Utilis.getStringSet(context, PrefKey.LIMIT_TIME_FIRST,"")+"'");
			mySqlBuf.append(", T1='9999999999'");
			mySqlBuf.append(", T6='"+Utilis.getStringSet(context, PrefKey.LIMIT_TIME_LAST,"")+"'");
			mySqlBuf.append(", T8='"+Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONA,"")+"'");
			mySqlBuf.append(" where _ID="+PrefKey.INI_DATE  );
			
			dbc.execSQL(mySqlBuf.toString());			      
			dbc.close();
            Utilis.setIntSet(context, PrefKey.PAY_STATE, 1);									
	}catch(Exception e){
			e.printStackTrace();
	}
		
	}

	public static int isRegSucceed(Context context, int mode) {
		  int isError=0;
		  if (Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONA, "").length() == 13) {
	    		String LimitA =Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONA, "87654321");
	    		String LimitB =Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONB, "12345678");    		
				StringBuffer bufa = new StringBuffer();
				StringBuffer bufb = new StringBuffer();
				StringBuffer bufc = new StringBuffer();
				StringBuffer bufd = new StringBuffer();
				
				String Stre=Utilis.getStringSet(context, PrefKey.LIMIT_TIME_LAST, "TEST1");
	    		if (LimitA.length()==13){  //
	    			bufa.append(""+((Utilis.toInt(LimitA.substring(1,2))+9)%10) )
	    			.append("/")    			
	    			.append(""+((Utilis.toInt(LimitA.substring(2,3))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(3,4))+5)%10) )
	    			.append("/")    			
	    			.append(""+((Utilis.toInt(LimitA.substring(4,5))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(5,6))+7)%10) );
	    		}
	    		bufc.append(""+((Utilis.toInt(LimitA.substring(6,7))+5)%10))
	    		.append(""+((Utilis.toInt(LimitA.substring(7,8))+9)%10))
	    		.append(""+((Utilis.toInt(LimitA.substring(8,9))+7)%10))
	    		.append(""+((Utilis.toInt(LimitA.substring(9,10))+5)%10))
	    		.append(""+((Utilis.toInt(LimitA.substring(10,11))+9)%10))
	    		.append(""+((Utilis.toInt(LimitA.substring(11,12))+7)%10));

	    		if (LimitB.length()==13){
	    			bufb.append(""+((Utilis.toInt(LimitB.substring(0,1))+5)%10) )
	    			.append("/")    			
	    			.append(""+((Utilis.toInt(LimitB.substring(1,2))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(2,3))+9)%10) )
	    			.append("/")    			
	    			.append(""+((Utilis.toInt(LimitB.substring(3,4))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(4,5))+7)%10) );
	    			
	    			bufd.append(""+((Utilis.toInt(LimitB.substring(5,6))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(6,7))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(7,8))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(8,9))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(9,10))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(10,11))+7)%10) );

	    			if (DateUtil.compareTime(DateUtil.addDate(Utilis.getStringSet(context, PrefKey.LIMIT_TIME_FIRST, "TEST1").toString(),PrefKey.ADD_YEAR),Stre)!=0 && DateUtil.compareTime(DateUtil.addDate(Utilis.getStringSet(context, PrefKey.LIMIT_TIME_FIRST, "TEST1").toString(),PrefKey.ADD_DATE),Stre)!=0){
	    			//表示LASTDAY被異動與註冊碼不符
	    				isError=3;
	    			}
	    			
	    		} //否則表示尚未註冊
	    		
		   		if (!Stre.substring(3).equalsIgnoreCase(bufa.toString()) && mode==1) {isError=4; }	
		   		else {  //表示合法
		   			if (DateUtil.compareTime(Stre, DateUtil.getCurrentDate()) < 0  && mode==1) { //已到期
	   					isError=6;
	   				} else if(DateUtil.compareTime(DateUtil.addDate(DateUtil.getCurrentDate(), 14),Stre) >= 0	 && mode==1){
	   					//表示再14天就要到期
	   					isError=5;
	   				}  
	   			}
		  } else {isError=7;}
		 return isError; 
	  }
	
	  //如果第一次就寫入初始化
    public static  int initPref_err(Context context,String mac,String currentD,int mode){
    	int isError=0;
       	if((Utilis.getBooleanSet(context, "first_init_preference", false) == false) || mode==1) {  //全新安裝 或 移除程式後重新安裝
       		//String BGDATE=Utilis.getCurrentDate();       		
		    if((Utilis.getStringSet(context, PrefKey.LIMIT_TIME_FIRST,"").length()<1) || mode==1){  //若有值表示非法不予更新
		    	//再來是否應取得,安裝日期
		    	Utilis.setStringSet(context, PrefKey.LIMIT_TIME_FIRST,currentD);
		    	Utilis.setStringSet(context, PrefKey.BEGIN_SSGN,PrefKey.BEGIN_SIGN);
		    	Utilis.setStringSet(context, PrefKey.BEGIN_MAC,mac);
		    } else {
		    	isError=1;
		    }
		    
				    if((Utilis.getStringSet(context, PrefKey.LIMIT_TIME_LAST, "").length()<1) || mode==1){  //若有值表示非法不予更新
					    String limit  = DateUtil.addDate(currentD,PrefKey.ADD_DATE);  
					    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_LAST, limit);		    
					    String begins=DateUtil.getSystemTime().substring(8);
					    String limits =limit.substring(3, 4)+limit.substring(5,7)+limit.substring(8,10)+begins;
					    
					    StringBuffer buf = new StringBuffer(); //make a ZONA
					    buf.append(""+((Utilis.toInt(limits.substring(0,1))+1) %10))
					    .append(""+((Utilis.toInt(limits.substring(1,2))+3) %10))
					    .append(""+((Utilis.toInt(limits.substring(2,3))+5) %10))
					    .append(""+((Utilis.toInt(limits.substring(3,4))+1) %10))
					    .append(""+((Utilis.toInt(limits.substring(4,5))+3) %10))
					    .append(""+((Utilis.toInt(limits.substring(5,6))+5) %10))
					    .append(""+((Utilis.toInt(limits.substring(6,7))+1) %10))
					    .append(""+((Utilis.toInt(limits.substring(7,8))+3) %10))
					    .append(""+((Utilis.toInt(limits.substring(8,9))+5) %10))
					    .append(""+((Utilis.toInt(limits.substring(9,10))+1) %10))
					    .append(""+((Utilis.toInt(limits.substring(10,11))+3) %10))
					    .append(""+((Utilis.toInt(limits.substring(11))+5) %10));
					    int limitn=0;
					    for (int i=0;i<12;i++) {limitn=limitn+Utilis.toInt(buf.toString().substring(i,i+1));}
					    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_ZONA, (""+(limitn %10))+buf.toString());		    
					    Utilis.setStringSet(context, PrefKey.LIMIT_TIME_ZONB, "12345678");					    
				    } else {isError=2;}
		    Utilis.setIntSet(context, PrefKey.PAY_STATE, 0);
		    Utilis.setIntSet(context, "language_preference",0);
		    if (mode==1) Utilis.putIni(context, "SYS", "IMEI","9999999999",1);
		    Utilis.setStringSet(context, PrefKey.TIME_CHECK_DAY, Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONA,""));
		    
    	  
		    Utilis.setBooleanSet(context, "first_init_preference",true);		    
       	}	else {  //表示非初始安裝
       		isError=AppUtil.isRegSucceed(context,1);
    	}
       	return isError;
    }       	
	
	public static boolean isRegSuccess(Context context) {
		  boolean myEXRegCheck = false;
		  if (Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONB, "").length() == 13) {
	    		String LimitA =Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONA, "2010010100001");
	    		String LimitB =Utilis.getStringSet(context, PrefKey.LIMIT_TIME_ZONB, "2010010111111");
				StringBuffer bufa = new StringBuffer();
				StringBuffer bufb = new StringBuffer();
				String Stre=Utilis.getStringSet(context, PrefKey.LIMIT_TIME_LAST, "TEST1");
	    			bufa.append(""+((Utilis.toInt(LimitA.substring(1,2))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(2,3))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(3,4))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(4,5))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(5,6))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(6,7))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(7,8))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(8,9))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitA.substring(9,10))+5)%10))
	    			.append(""+((Utilis.toInt(LimitA.substring(10,11))+9)%10))
	    			.append(""+((Utilis.toInt(LimitA.substring(11,12))+7)%10));
	    			
	    			bufb.append(""+((Utilis.toInt(LimitB.substring(0,1))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(1,2))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(2,3))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(3,4))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(4,5))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(5,6))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(6,7))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(7,8))+7)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(8,9))+9)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(9,10))+5)%10) )
	    			.append(""+((Utilis.toInt(LimitB.substring(10,11))+7)%10) );
	    		if (bufa.toString().equalsIgnoreCase(bufb.toString()) && bufa.toString().substring(0,5).equalsIgnoreCase((Stre.substring(3,4)+Stre.substring(5,7)+Stre.substring(8,10) ))){  //表示全合法且已註冊
		            myEXRegCheck = true;
		            Utilis.setIntSet(context, PrefKey.PAY_STATE, 1);
		        } else {
		            myEXRegCheck = false;
		        }
		        return myEXRegCheck;
		  }else{
			  Utilis.setStringSet(context, PrefKey.LIMIT_TIME_ZONB, "");
			  return false;
		  }
	  }
	
}
