package com.cwbuyer.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import com.cwbuyer.lib.DataImports;
import com.cwbuyer.lib.MakeTransDetail;
import com.cwbuyer.lib.QRep_08;
import com.cwbuyer.lib.SysSetup;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

// 上傳下載
@SuppressLint({ "SdCardPath", "InflateParams" })
@SuppressWarnings("unused")
public class TransManage  extends Activity {

	public ProgressDialog myDialog = null;
	private int PSWD=Utilis.toInt(Utilis.getIni(this,"SYS","DEPT",5)); //0OK 1密碼不符
	private String roots=android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/cwbuyer/";
    File dnPath = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+Utilis.getIni(this, "SYS", "BACKOUT", 5));
	private String[] mNames=new String[]{"CSV匯入資料庫","上傳下載設定","刪單改單異樣報告","返回"};
	Bundle bundle;
	private int mChoice=0;
	private String listCsv [];//匯入檔案清單

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage_layout);
		
		DataAdapter adapter = new DataAdapter(this);
		ListView listview = (ListView)findViewById(R.id.listview);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
			long arg3) {
				Utilis.runVibrate(TransManage.this);
				Intent intent = new Intent();
				switch(pos){
					case 0: { 
						/*CSV匯入資料庫
						 * 規格 明細貨號 件數 單價
						*/
			        	int loc=-1;
				        if (!dnPath.exists()){
				        	dnPath.mkdirs();  //若不存在 則建立之
				        }else{
				        	mChoice=0;
				        	File file[] = dnPath.listFiles();
							for (int i=0; i < file.length; i++)
							{
								if (file[i].getName().indexOf(".csv")>=0){
									mChoice+=1;
								}	
							}
							listCsv =new String[mChoice];
							for (int i=0; i < file.length; i++)
							{
								if (file[i].getName().indexOf(".csv")>=0){
									loc+=1;
									listCsv[loc]=file[i].toString().substring(dnPath.toString().length()+1);
								}	
							}
				        }
					if (loc>=0){
						mChoice=0;
						AlertDialog.Builder sortDlg = new AlertDialog.Builder(TransManage.this);
				        sortDlg.setTitle("請選擇匯入的CSV檔 或 返回")
				        .setSingleChoiceItems(listCsv, 0, new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface dialog, int item) {
				            	mChoice = item;
				            	
		            	/*檔案資料夾清單顯示,選取確認後
		            	 * 直接開啟CSV檔,並一一檢查明細貨號是否OK
						 * 如果有任何異狀,將列示錯誤的清冊
		            	 *不OK的將顯示在LIST中,提示以外,能否存入另一個檔案
						 * 再度確認後,開出所有條件 
						 * 單別 門市出貨單 門市轉貨單,銷貨單,廠商進貨單
						 * 以RadioGroupBox 唯一選項,決定欄位一欄位二名稱
						 *門市轉貨單 需要 [轉出門市](參數預設) [轉入門市](參數預設)
						 *門市出貨單(一定是[總倉出貨]出給 [門市](參數預設)  
						 *廠商進貨單 一定是[總倉進貨] [廠商](參數預設)
						 *銷貨單 選擇[銷貨門市](參數預設) 選擇[會員](參數預設)
						 *折數 帶入 (可以編輯)
						 *經手人(參數預設) 
		            	 * 
		            	 */
				            }
				        }) 
				        .setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface dialog, int which) { //確認
								Toast.makeText(TransManage.this, dnPath+File.separator+listCsv[mChoice], Toast.LENGTH_LONG).show();
				            	
								Intent intent = new Intent();
								intent.setClass(TransManage.this, DataImports.class);
								intent.putExtra("make_mode",dnPath+File.separator+listCsv[mChoice]);  //傳入確認的csv檔  
								startActivity(intent);
													
				            }
				          })
				        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
				            public void onClick(DialogInterface dialog, int which) { //取消

				            }
				          }).show();
					}	
						break;				
					}
					case 1: { 
						if (PSWD!=1 ) { //上傳下載設定
							intent.setClass(TransManage.this, SysSetup.class);
							startActivity(intent);
						}
						break;				
					}
					case 2:{
						intent.setClass(TransManage.this, QRep_08.class);
						startActivity(intent);
						break;	
					}
					case 3: //返回
						finish();
						break;
				} //end_switch
			}
		});
		
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MainClick());
		
		//碰到須重傳的ZIP檔,可以列出清單為 1.已傳送的功能
		listview.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Utilis.runVibrate(TransManage.this);
				Intent intent = new Intent();
				
				switch (arg2){
					case 0:
						Toast.makeText(TransManage.this, "功能保留!!", Toast.LENGTH_LONG).show();
						break;	
					case 1:
					{
					      myDialog = ProgressDialog.show(TransManage.this,"資料處理中.....","請稍候.............",true);//		setAutoRotationMode();			  
					      new Thread()
					      { 
					        public void run()
					        { 
					          try
					          { 
									SQLiteDatabase db = Utilis.getDB(TransManage.this);
									//Toast.makeText(TransManage.this, "圖檔路徑 =  "+roots, Toast.LENGTH_LONG).show();
									
									if (roots.equalsIgnoreCase("/mnt/sdcard/0/cwbuyer/")){
										//本機可能為Sumsung
										db.execSQL("update qdetail set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/mnt/sdcard/cwbuyer/','"+roots+"') where PICTURE like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										
										db.execSQL("update qdetail set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/storage/emulated/0/cwbuyer/','"+roots+"') where PICTURE like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
									}else if (roots.equalsIgnoreCase("/mnt/sdcard/cwbuyer/")){
										db.execSQL("update qdetail set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PICTURE like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										
										db.execSQL("update qdetail set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/storage/emulated/0/cwbuyer/','"+roots+"') where PICTURE like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/storage/emulated/0/cwbuyer/','"+roots+"') where PIC like '%/storage/emulated/0/cwbuyer/%'");
									}else{//storage/emulated/0/
										db.execSQL("update qdetail set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PICTURE like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/mnt/sdcard/0/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/0/cwbuyer/%'");
										
										db.execSQL("update qdetail set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qitems set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qdetail set PICTURE=replace(PICTURE,'/mnt/sdcard/cwbuyer/','"+roots+"') where PICTURE like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qcust set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qfact set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qc_type set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qmacc set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										db.execSQL("update qc_macc set PIC=replace(PIC,'/mnt/sdcard/cwbuyer/','"+roots+"') where PIC like '%/mnt/sdcard/cwbuyer/%'");
										
									}
									db.close();
									Toast.makeText(TransManage.this, "所有圖檔已更新路徑成功!!", Toast.LENGTH_LONG).show();
					          }
					          catch (Exception e)
					          {
					            e.printStackTrace();
					          }
					          finally
					          {
					            // 卸載所建立的myDialog物件。
					            myDialog.dismiss();
					          }
					        }
					      }.start(); /* 開始執行執行緒 */
					break;	
					}
				}
				
				return true;
			}
			
		});
	}  //END 主程式
	
	class DataAdapter extends BaseAdapter{
		
		private LayoutInflater mInflater;
		
		public DataAdapter(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);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.textName.setText(mNames[position]);
			return convertView;
		}

		/* class ViewHolder */
		private class ViewHolder {
			TextView textName;
		}
	}
	
	class MainClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			Utilis.runVibrate(TransManage.this);
			int id = v.getId();
			switch (id) {
				case R.id.btn_exit:
				{
					finish();
					break;
				}
			}
		}
	}
}
