package com.cwbuyer.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.cwbuyer.adapter.ElementAdapter;
import com.cwbuyer.adapter.WashAdapter;
import com.cwbuyer.code.CodeList;
import com.cwbuyer.code.CodeMode;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;


// ¼Ð¥Ü¸ê®Æ
public class AElements  extends Activity {
	
	public static final int WASH = 1001;
	public static final int QUALITY = 1002;

	private int mWashId = 0;
	private int mQualityId = 0;
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == WASH) {
			if (resultCode == RESULT_OK) {
				int id = data.getIntExtra("id", 0);
				try {
					mWashId = id;
					mWashAdapter.getData(AElements.this, id);
					mWashAdapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}else if (requestCode == QUALITY) {
			if (resultCode == RESULT_OK) {
				int id = data.getIntExtra("id", 0);
				try {
					mQualityId = id;
					mEleAdapter.getData(AElements.this, id);
					mEleAdapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	ElementAdapter mEleAdapter = null;
	WashAdapter mWashAdapter = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.element_layout);
		
		mWashId = getIntent().getIntExtra("wash_id", 1);
		mQualityId = getIntent().getIntExtra("quality_id", 1);
		
		mEleAdapter = new ElementAdapter(this, 0);
		mWashAdapter = new WashAdapter(this, 0);
		
		mEleAdapter.getData(this, mQualityId);
		mWashAdapter.getData(this, mWashId);
		
		ListView listview1 = (ListView)findViewById(R.id.listview1);
		ListView listview2 = (ListView)findViewById(R.id.listview2);
		listview1.setAdapter(mEleAdapter);
		listview2.setAdapter(mWashAdapter);
		listview1.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long arg3) {
				Utilis.runVibrate(AElements.this);
				Intent intent = new Intent();
				intent.setClass(AElements.this, CodeList.class);
//				intent.putExtra("id", mWashId);
				intent.putExtra("code_mode", CodeMode.ELEMENTS);
				intent.putExtra("edit_mode", 1);
				startActivityForResult(intent, QUALITY);
			}
		});
		listview2.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long arg3) {
				Utilis.runVibrate(AElements.this);
				Intent intent = new Intent();
				intent.setClass(AElements.this, CodeList.class);
				
				intent.putExtra("code_mode", CodeMode.WASHING);
//				intent.putExtra("id", mQualityId);
				intent.putExtra("edit_mode", 1);
				startActivityForResult(intent, WASH);
			}
		});
		
		((Button)findViewById(R.id.btn_setting)).setOnClickListener(new MainClick());
        ((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MainClick());
	}
	
	class MainClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			Utilis.runVibrate(AElements.this);
			switch (id) {
				case R.id.btn_setting:
					Intent intent = new Intent();
					intent.putExtra("wash_id", mWashId);
					intent.putExtra("quality_id", mQualityId);
					setResult(RESULT_OK,intent);
					finish();
					break;
				case R.id.btn_exit:
					finish();
					break;
			}
		}
	}
}