package com.cwbuyer.code;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cwbuyer.adapter.ElementAdapter.ElementData;
import com.cwbuyer.format.EMode;
import com.cwbuyer.lib.DBCloud;
import com.cwbuyer.lib.TbName;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

// 成份組合
public class AddElements extends Activity{
	
	private int mMode = 0;
	private int mID = 0;
	private ElementData mData = null;
	
	//	0單機 1On_line 2.Off_line
	int of_line=Utilis.toInt(Utilis.getIni(this, "SYS", "IMPORT", 3));
	String mUid=Utilis.getIni(this, "SYS", "IMPORT", 2);  //ex:3g001
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_elements);
		setTitle("成份組合編輯");
		
		mID = getIntent().getIntExtra("id", 0);
		mMode = getIntent().getIntExtra("mode", 0);
		if(mID != 0 && mMode == EMode.EDIT){
			getData(this);
		}else{
			mData = new ElementData(); 
		}
		
		if(mMode == EMode.EDIT && mData != null){
			for(int i = 0; i < mData.elements.size(); i++){
				String name = mData.elements.get(i);
				String value = String.valueOf(mData.percentages.get(i));
				switch(i){
				case 0:
					((EditText)findViewById(R.id.edit_name1)).setText(name);
					((EditText)findViewById(R.id.edit_value1)).setText(value);
					break;
				case 1:
					((EditText)findViewById(R.id.edit_name2)).setText(name);
					((EditText)findViewById(R.id.edit_value2)).setText(value);
					break;
				case 2:
					((EditText)findViewById(R.id.edit_name3)).setText(name);
					((EditText)findViewById(R.id.edit_value3)).setText(value);
					break;
				case 3:
					((EditText)findViewById(R.id.edit_name4)).setText(name);
					((EditText)findViewById(R.id.edit_value4)).setText(value);
					break;
				case 4:
					((EditText)findViewById(R.id.edit_name5)).setText(name);
					((EditText)findViewById(R.id.edit_value5)).setText(value);
					break;
				case 5:
					((EditText)findViewById(R.id.edit_name6)).setText(name);
					((EditText)findViewById(R.id.edit_value6)).setText(value);
					break;
				}
			}
		}
		
		((Button)findViewById(R.id.btn_save)).setOnClickListener(new MenuClick());
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MenuClick());
	}
	
	class MenuClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			Utilis.runVibrate(AddElements.this);
			switch(id){
			case R.id.btn_save:
				if(saveData() == true){
					finish();
				}
				break;
			case R.id.btn_exit:
				finish();
				break;
			}
		}
	}
	public void getData(Context context){
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = db.rawQuery("select * from qc_quality where _id=" + mID, null);
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					mData = new ElementData();
					mData.id = cursor.getInt(cursor.getColumnIndex("_ID"));
					String strName = null;
					strName = cursor.getString(cursor.getColumnIndex("QUA10"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA11")));
					//}
					strName = cursor.getString(cursor.getColumnIndex("QUA20"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA21")));
					//}
					strName = cursor.getString(cursor.getColumnIndex("QUA30"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA31")));
					//}
					strName = cursor.getString(cursor.getColumnIndex("QUA40"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA41")));
					//}
					strName = cursor.getString(cursor.getColumnIndex("QUA50"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA51")));
					//}
					strName = cursor.getString(cursor.getColumnIndex("QUA60"));
					//if(strName != null && strName.length() > 0){
						mData.elements.add(strName);
						mData.percentages.add(cursor.getString(cursor.getColumnIndex("QUA61")));
					//}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	private boolean saveData(){
		boolean succed=true;
		
		ArrayList<String> listName = new ArrayList<String>();
		ArrayList<String> listValue = new ArrayList<String>();
		String name1 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name1)).getText().toString());
		String value1 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value1)).getText().toString());
		String name2 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name2)).getText().toString());
		String value2 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value2)).getText().toString());
		String name3 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name3)).getText().toString());
		String value3 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value3)).getText().toString());
		String name4 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name4)).getText().toString());
		String value4 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value4)).getText().toString());
		String name5 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name5)).getText().toString());
		String value5 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value5)).getText().toString());
		String name6 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_name6)).getText().toString());
		String value6 = DBCloud.chkStr(((EditText)findViewById(R.id.edit_value6)).getText().toString());
		
//		if(name1 != null && name1.length() > 0 && value1!=null && value1.length() > 0){
			listName.add(name1);
			listValue.add(value1);
//		}
//		if(name2 != null && name2.length() > 0 && value2!=null && value2.length() > 0){
			listName.add(name2);
			listValue.add(value2);
//		}
//		if(name3 != null && name3.length() > 0 && value3!=null && value3.length() > 0){
			listName.add(name3);
			listValue.add(value3);
//		}
//		if(name4 != null && name4.length() > 0 && value4!=null && value4.length() > 0){
			listName.add(name4);
			listValue.add(value4);
//		}
//		if(name5 != null && name5.length() > 0 && value5!=null && value5.length() > 0){
			listName.add(name5);
			listValue.add(value5);
//		}
//		if(name6 != null && name6.length() > 0 && value6!=null && value6.length() > 0){
			listName.add(name6);
			listValue.add(value6);
//		}
		
		if(listName.size() <= 0){
			Toast.makeText(this, "請至少建立一筆完整的資料", Toast.LENGTH_SHORT).show();
			return false;
		}
		
		ContentValues value = new ContentValues();
    	String[] dbName = new String[]{"QUA10","QUA20","QUA30","QUA40","QUA50","QUA60"};
    	String[] dbValue = new String[]{"QUA11","QUA21","QUA31","QUA41","QUA51","QUA61"};
    	
    	for(int i = 0; i < listName.size(); i ++){
    		value.put(dbName[i], listName.get(i));
    		value.put(dbValue[i], listValue.get(i));
    	}
    	
    	for(int i = 0; i < 6; i ++){
    		if(i < listName.size()){
    			value.put(dbName[i], listName.get(i));
        		value.put(dbValue[i], listValue.get(i));
    		}else{
    			value.put(dbName[i], "");
    			value.put(dbValue[i], "");
    		}
    	}

		if (Utilis.haveInternet(this) && of_line!=0){  //有網路的狀態且非單機,優先雲端運算,再行Hander維護Client端
			StringBuffer bfstr = new StringBuffer();
			StringBuffer bfstrF = new StringBuffer();
			
					bfstr.append(mUid).append(";");
					bfstrF.append("UID").append(";");
					bfstr.append(mID).append(";");
					bfstrF.append("_ID").append(";");
 					bfstr.append(name1).append(";");
					bfstrF.append("QUA10").append(";");
 					bfstr.append(value1).append(";");
					bfstrF.append("QUA11").append(";");
 					bfstr.append(name2).append(";");
					bfstrF.append("QUA20").append(";");
 					bfstr.append(value2).append(";");
					bfstrF.append("QUA21").append(";");
 					bfstr.append(name3).append(";");
					bfstrF.append("QUA30").append(";");
 					bfstr.append(value3).append(";");
					bfstrF.append("QUA31").append(";");
 					bfstr.append(name4).append(";");
					bfstrF.append("QUA40").append(";");
 					bfstr.append(value4).append(";");
					bfstrF.append("QUA41").append(";");
 					bfstr.append(name5).append(";");
					bfstrF.append("QUA50").append(";");
 					bfstr.append(value5).append(";");
					bfstrF.append("QUA51").append(";");
 					bfstr.append(name6).append(";");
					bfstrF.append("QUA60").append(";");
 					bfstr.append(value6);
					bfstrF.append("QUA61");
					Toast.makeText(this, "正前往雲端編輯_商品類別中,請稍候....", Toast.LENGTH_SHORT).show();
					Thread t = new Thread(new DBCloud.sendPostRunnableQC(this,bfstr.toString(),bfstrF.toString(),6));
					t.start();
    	}else if(mMode == EMode.ADD){
    		SQLiteDatabase db = Utilis.getDB(this);
	    	long result = db.insert(TbName.QC_QUALITY, null, value);
	    	if(result > 0){
	    		Toast.makeText(this, "存取資料成功", Toast.LENGTH_SHORT).show();
	    	}else{
	    		Toast.makeText(this, "存取資料失敗", Toast.LENGTH_SHORT).show();
	    		succed =false;
	    	}
	    	db.close();
    	}else{  // EDIT
    		SQLiteDatabase db = Utilis.getDB(this);
    		String[] args = {String.valueOf(mID)};
			int success = db.update(com.cwbuyer.lib.TbName.QC_QUALITY, value,
					"_ID=?", args);
			if(success > 0){
				Toast.makeText(this, "修改資料成功", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(this, "修改資料失敗", Toast.LENGTH_SHORT).show();
	    		succed =false;
			}
			db.close();
    	}
		value.clear();
		return succed;
	}
}