package com.usai.apex;

import java.util.ArrayList;
import java.util.List;

import com.usai.util.dbUtil;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
//import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CustomizeFieldsActivity extends Activity implements OnClickListener
{
	//
	// @Override
	// protected void onCreate(Bundle savedInstanceState)
	// {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.activity_customize_fields);
	// }
	//
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu)
	// {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.customize_fields, menu);
	// return true;
	// }



	private static List<fieldedit> list = null;
	private DragListAdapter adapter = null;

	// public static List<String> groupKey = new ArrayList<String>();
	private List<fieldedit> showList = new ArrayList<fieldedit>();
	private List<fieldedit> hideList = new ArrayList<fieldedit>();
	private String user/* ,password */, function_name;
	private int behavior;

	public class fieldedit
	{
		public String name = null;
		public int _id = -1;

		public fieldedit(String str, int i)
		{
			name = str;
			_id = i;

		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		user = getIntent().getStringExtra("user");
		// password = getIntent().getStringExtra("password");
		function_name = getIntent().getStringExtra("function_name");
		behavior = getIntent().getIntExtra("behavior", -1);
		setContentView(R.layout.activity_customize_fields);
		Button btnok= (Button)findViewById(R.id.btnok);
		btnok.setOnClickListener(this);
		Button btncancel= (Button)findViewById(R.id.btncancel);
		btncancel.setOnClickListener(this);
		

		initData();

		DragListView dragListView = (DragListView) findViewById(R.id.drag_list);
		adapter = new DragListAdapter(this, list);
		dragListView.setAdapter(adapter);
	}

	public void initData()
	{
		SQLiteDatabase db = dbUtil.OpenDB(this, null, false);
		Cursor cursor = db.query("fields_info", new String[] { "aname", "show",
				"_id" }, "function_name='" + function_name + "' and user='"
				+ user + "' and behavior=" + behavior, null, null, null,
				"priority , aname", null);
		while (cursor.moveToNext())
		{
			int show = cursor.getInt(1);
			if (show == 1)
				showList.add(new fieldedit(cursor.getString(0), cursor
						.getInt(2)));
			else
			{
				hideList.add(new fieldedit(cursor.getString(0), cursor
						.getInt(2)));

			}
		}
		// 数据结果
		list = new ArrayList<fieldedit>();

		// groupKey存放的是分组标签
		// groupKey.add("Display fields");
		// groupKey.add("Hide fields");

		// for (int i = 0; i < 5; i++)
		// {
		// navList.add("A选项" + i);
		// }
		list.add(new fieldedit("Display fields", -1));
		list.addAll(showList);

		// for (int i = 0; i <18; i++)
		// {
		// moreList.add("B选项" + i);
		// }
		list.add(new fieldedit("Hide fields", -2));
		list.addAll(hideList);
		dbUtil.CloseCursor(cursor);
		dbUtil.CloseDB(db);
	}

	public static class DragListAdapter extends ArrayAdapter<fieldedit>
	{

		// private Context mContext;

		public DragListAdapter(Context context, List<fieldedit> objects)
		{
			super(context, 0, objects);
			// this.mContext = context;
		}

		public List<fieldedit> getList()
		{
			return list;
		}

		@Override
		public boolean isEnabled(int position)
		{
			if (getItem(position)._id < 0) //_id<0 means a group key
				return false;
			// if (groupKey.contains(getItem(position).name))
			// {
			// // 如果是分组标签，返回false，不能选中，不能点击
			// return false;
			// }
			return super.isEnabled(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{

			View view = convertView;
			if(getItem(position)._id<0) //_id<0 means a group key
			{
				view = LayoutInflater.from(getContext()).inflate(
				R.layout.drag_list_item_tag, null);
			}
//			if (groupKey.contains(getItem(position).name))
//			{
//				// 如果是分组标签，就加载分组标签的布局文件，两个布局文件显示效果不同
//				view = LayoutInflater.from(getContext()).inflate(
//						R.layout.drag_list_item_tag, null);
//			}
			else
			{
				// 如果是正常数据项标签，就加在正常数据项的布局文件
				view = LayoutInflater.from(getContext()).inflate(
						R.layout.drag_list_item, null);
			}

			TextView textView = (TextView) view
					.findViewById(R.id.drag_list_item_text);
			textView.setText(getItem(position).name);

			return view;
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btnok:
		{
//			DragListView dragListView = (DragListView) findViewById(R.id.drag_list);
			int count=0;
			for(int i=0;i<adapter.getCount();i++)
			{
				fieldedit fe=adapter.getItem(i);
				if(fe._id==-2||count>0)
					break;
				if(fe._id>=0)
					count++;
				
			}
			if(count==0)
			{
				new AlertDialog.Builder(CustomizeFieldsActivity.this)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setTitle("Error!")
				.setMessage("You must set at least 1 field visiable!")
				.setPositiveButton("OK",
						new DialogInterface.OnClickListener()
						{
							public void onClick(
									DialogInterface dialog,
									int whichButton)
							{

//								finish();
								/* User clicked OK so do some stuff */
							}
						}).show();
				break;
				
			}
			
			int show = 1;
			SQLiteDatabase db= dbUtil.OpenDB(this, null, true);
			db.beginTransaction();
			for(int i=0;i<adapter.getCount();i++)
			{
				fieldedit fe=adapter.getItem(i);
				if(fe._id==-2)//hide group begin
				{
					show = 0;
				}
				if(show==1)
					db.execSQL("update fields_info set show="+show+" ,priority="+i+" where _id="+fe._id);
				else
					db.execSQL("update fields_info set show="+show+" where _id="+fe._id);
					
			}
			db.setTransactionSuccessful();
			db.endTransaction();
			dbUtil.CloseDB(db);
			finish();
//			Log.d("fields",);
			break;
		}
		case R.id.btncancel:
			finish();
			break;
		default:
			break;
		}
		// TODO Auto-generated method stub
		
	}

}
