package cn.com.wasu.main.systeminfo;


import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.wasu.main.R;

public class DialogSystemInfo extends Dialog {
	
	private long systemGetFocusSelectedPos = 0;
	private long appGetFocusSelectedPos = 0;
	
	private long appLoseFocusSelectedPos = -1;
	private long systemLoseFocusSelectedPos = -1;
	
	public DialogSystemInfo(Context context) {
		super(context);
		
		init(context);
	}
	
	private void init(final Context context) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.dialog_systeminfo);
		
		AppInfo appInfo = new AppInfo(context);
		ArrayList<String> appInfos = (ArrayList<String>) appInfo.getAppInfo();
		ArrayList<String> systemInfos = (ArrayList<String>) SystemInfo.getSystemInfo();
		
		final ListView list_systeminfo = (ListView) this.findViewById(R.id.list_systeminfo);
		list_systeminfo.setAdapter(new ArrayAdapter<String>(context, R.layout.dialog_systeminfo_item, systemInfos));
		
		final ListView list_appinfo = (ListView) this.findViewById(R.id.list_appinfo);
		list_appinfo.setAdapter(new ArrayAdapter<String>(context, R.layout.dialog_systeminfo_item, appInfos));
		
		if (list_appinfo.getChildCount() > 0) {
			list_appinfo.setTag(list_appinfo.getChildAt(0));
		}
		
		if (list_systeminfo.getChildCount() > 0) {
			list_systeminfo.setTag(list_systeminfo.getChildAt(0));
		}
		
		OnItemSelectedListener listener = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int id, long position) {
				if (adapter.hasFocus()) {
					if (adapter.getTag() != null) {
						ScaleAnimation animationS =new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f,
						            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f);
						animationS.setDuration(200);
						animationS.setFillAfter(true);
						((TextView) adapter.getTag()).startAnimation(animationS);
					}
					ScaleAnimation animationB =new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f,
							Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f);
					animationB.setDuration(200);
					animationB.setFillAfter(true);
					((TextView) view).startAnimation(animationB);
					adapter.setTag(view);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
			
		};
		
		OnFocusChangeListener focusListener = new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean focused) {
				if (!focused) {
					ScaleAnimation animationS =new ScaleAnimation(1.1f, 1.0f, 1.1f, 1.0f,
							Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f);
					animationS.setDuration(200);
					animationS.setFillAfter(true);
					if (view.getTag() != null) {
						((TextView) view.getTag()).startAnimation(animationS);
					}
					view.setTag(null);
					
				} else {
					
					if (view.getId() == R.id.list_appinfo) {
						appGetFocusSelectedPos = list_appinfo.getSelectedItemPosition();
					} else {
						systemGetFocusSelectedPos = list_systeminfo.getSelectedItemPosition();
					}
					
					ScaleAnimation animationB =new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f,
							Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f);
					animationB.setDuration(200);
					animationB.setFillAfter(true);
					if (view.getId() == R.id.list_appinfo) {
						if (list_systeminfo.getSelectedItemPosition() == systemGetFocusSelectedPos) {
							(((ListView) view).getSelectedView()).startAnimation(animationB);
							view.setTag(((ListView) view).getSelectedView());
						}
					} else {
						if (list_appinfo.getSelectedItemPosition() == appGetFocusSelectedPos) {
							if(view!=null ){
								View selectedView = ((ListView) view).getSelectedView();
								if(selectedView !=null){
									selectedView.startAnimation(animationB);
									view.setTag(selectedView);
								}
							}
						}
					}
//					view.setTag(((ListView) view).getSelectedView());
				}
			}
		};
		list_systeminfo.setOnItemSelectedListener(listener);
		list_appinfo.setOnItemSelectedListener(listener);
		
		list_systeminfo.setOnFocusChangeListener(focusListener);
		list_appinfo.setOnFocusChangeListener(focusListener);
	}
	
}
