package com.cwbuyer.main;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.cwbuyer.code.CodeList;
import com.cwbuyer.code.CodeMode;
import com.cwbuyer.lib.DBCloud;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

// 代碼設定
@SuppressLint("InflateParams")
public class ACode extends Activity {
	
	String myImportPath =android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+Utilis.getIni(this, "SYS", "YEAR",7);	
	//For Cloud
	private String uriAPIpath = "http://"+Utilis.getIni(ACode.this, "SYS", "IMPORT", 1)+"/"+Utilis.getIni(ACode.this, "SYS", "IMPORT", 2);
	private String uriAPI =null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.code_layout);
		
		CodeAdapter adapter = new CodeAdapter(this);
		ListView listview = (ListView)findViewById(R.id.list_param);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long arg3) {
				Utilis.runVibrate(ACode.this);
				Intent intent = new Intent();
				intent.putExtra("code_mode", getAction(pos));
				intent.setClass(ACode.this, CodeList.class);
				startActivity(intent);
			}
		});
		
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MenuClick());
		
		int of_line=Utilis.toInt(Utilis.getIni(ACode.this, "SYS", "IMPORT", 3));
		
		if (Utilis.haveInternet(this)){  //有網路的狀態
			if (of_line!=0){ 
				//雲端 && 總倉(店) 需先download them
				//0.update   1.download
				String mUid=Utilis.getIni(ACode.this, "SYS", "IMPORT", 2);  //ex:3g001
				uriAPI = uriAPIpath+"/mupdate_yn.php";
				mupdate_qc(mUid,"0","130101");  //可以傳回server_Q1~Q8的update時間130801165959
			} 
		}
		
	}
	
	
	//3g001,Q1-Q30,Local_(SYS,ALBUN,n)LAST_DAY
	  private 	void mupdate_qc(String gUid,String Id,String UPD){
					StringBuffer bfstr = new StringBuffer();
					StringBuffer bfstrF = new StringBuffer();
						bfstr.append(gUid).append(";");
						bfstrF.append("UID").append(";");
						bfstr.append(Id).append(";");
						bfstrF.append("ID").append(";");
						bfstr.append(UPD);
						bfstrF.append("LUPDATE");
					Thread t = new Thread(new DBCloud.sendPostRunnable(ACode.this,bfstr.toString(),bfstrF.toString(),uriAPI,1));
					t.start();
	  }
  
	class MenuClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			Utilis.runVibrate(ACode.this);
			switch(id){
			case R.id.btn_exit:
				finish();
				break;
			}
		}
	}
	
	private int getAction(int pos){
		int nMode = 0;
		switch(pos){
		case 0:
			nMode = CodeMode.COUNTRY;
			break;
		case 1:
			nMode = CodeMode.CATEGORY;
			break;
		case 2:
			nMode = CodeMode.COLOR;
			break;
		case 3:
			nMode = CodeMode.SIZE;
			break;
		case 4:
			nMode = CodeMode.PRICING;
			break;
		case 5:
			nMode = CodeMode.ELEMENTS;
			break;
		case 6:
			nMode = CodeMode.WASHING;
			break;
		case 7:
			nMode = CodeMode.MACC;
			break;
		case 8:
			nMode = CodeMode.PPRI;
			break;
			
			default:
				break;
		}
		return nMode;
	}

	class CodeAdapter extends BaseAdapter {
		
		private LayoutInflater mInflater;
		private String[] mNames = new String[]{"產地國別", "商品類別", "商品顏色", "尺寸規格", "訂價策略", "成份組合","洗滌組合","收支代碼","特價對照表"};
		private String[] mExplain1 = new String[]{"採購國別,匯率,旗幟", "商品產地,季節,管銷運稅", "紅,灰,粉,紫,黑,藍....", "S,M,L,XL,26,28,30,32....", "外幣,成本,中盤價", "聚酯纖維,羊毛,人造絲", "手洗,不可機洗,熨燙", "雜項,水電,運費", "降價對照表"};
		private String[] mExplain2 = new String[]{"","","","5-15,3-13,15-21","批發價,零售價,網路價","棉,金屬纖維,呢絨","不可擰乾,常規乾洗","等,營業外支出收入項目設定","並以售價,年度,季節,做為比對"};
		public CodeAdapter(Context context) {
		      /* 參數初始化 */
		      mInflater = LayoutInflater.from(context);
		}
		
		public int getCount() {
			return mNames.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_param, null);
				holder = new ViewHolder();
				holder.textName = (TextView) convertView.findViewById(R.id.text_title);
				holder.textExplain1 = (TextView) convertView.findViewById(R.id.text_1);
				holder.textExplain2 = (TextView) convertView.findViewById(R.id.text_2);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textName.setText(mNames[position]);
			try{
				holder.textExplain1.setText(mExplain1[position]);
				
				if(position > 2){
					holder.textExplain2.setText(mExplain2[position]);
				}else{
					if(position == 0){
						SpannableString spanStr = new SpannableString("1 2 3 4");
						ImageSpan imageSpan1 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.country_twn));
						ImageSpan imageSpan2 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.country_chi));
						ImageSpan imageSpan3 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.country_jpn));
						ImageSpan imageSpan4 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.country_koe));
						spanStr.setSpan(imageSpan1, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan2, 2, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan3, 4, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan4, 6, 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						holder.textExplain2.setText(spanStr);
					}else if(position == 1){
						SpannableString spanStr = new SpannableString("1 2 3");
						ImageSpan imageSpan1 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.cat_001));
						ImageSpan imageSpan2 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.cat_002));
						ImageSpan imageSpan3 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.cat_003));
						spanStr.setSpan(imageSpan1, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan2, 2, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan3, 4, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						holder.textExplain2.setText(spanStr);
					}else if(position == 2){
						SpannableString spanStr = new SpannableString("1 2 3 4");
						ImageSpan imageSpan1 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.color1));
						ImageSpan imageSpan2 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.color2));
						ImageSpan imageSpan3 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.color3));
						ImageSpan imageSpan4 = new ImageSpan(ACode.this, getBmpResource(ACode.this,R.drawable.color4));
						spanStr.setSpan(imageSpan1, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan2, 2, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan3, 4, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						spanStr.setSpan(imageSpan4, 6, 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
						holder.textExplain2.setText(spanStr);
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			return convertView;
		}
		private class ViewHolder {
			TextView textName;
			TextView textExplain1;
			TextView textExplain2;
		}
	}
	
	// 2012.05.16  
	  @SuppressWarnings("unused")
	public static Bitmap getBmpResource(Context cnt, int id){
	  	int i = (int)(cnt.getResources().getDisplayMetrics().density * 160.0F);
	      BitmapFactory.Options localOptions = new BitmapFactory.Options();
	      localOptions.inSampleSize = 4;
	      localOptions.inPurgeable = true;
	      localOptions.inInputShareable = true;
	      Bitmap bmp = BitmapFactory.decodeResource(cnt.getResources(), id, localOptions);
	      Bitmap rescaleBmp = Bitmap.createScaledBitmap(bmp, 40, 30, true);
	  	return rescaleBmp;
	  }
}
