package com.lianxin.pubqq.main;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.pubqq.main.adpter.ContactAdapter;
import com.lianxin.panqq.main.bean.ContactUser;
import com.lianxin.pubqq.main.widght.SideBar;
import com.lianxin.pubqq.sms.ContactActivity;
import com.lianxin.pubqq.sms.SMSActivity;

//通讯录（本来想通过电话号码互相联系，但各种权限太搞人了。没有完工）
public class Fragment_Contact extends Fragment implements OnClickListener,
		OnItemClickListener {
	private Activity ctx;
	private View layout, layout_head, layout_foot;
	private ListView lvContact;
	private SideBar indexBar;
	private TextView mDialogText;
	private WindowManager mWindowManager;
	private String  strCaption = "通讯簿,搜索手机号,手机联系人,群发短信,我的用户,通讯设置,手机联系人,刷新手机联系人"; //服务器各自定制的标题字符串
	private String[]  arrCaption = null; //标题字符串数组 
	private String  strTip = "刷新手机联系人";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_contact,
					null);
			mWindowManager = (WindowManager) ctx
					.getSystemService(Context.WINDOW_SERVICE);		
			initViews();
			initData();
			setOnListener();
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		
		return layout;
	}

	private void initViews() {
		lvContact = (ListView) layout.findViewById(R.id.lvContact);

		mDialogText = (TextView) LayoutInflater.from(getActivity()).inflate(
				R.layout.list_position, null);
		mDialogText.setVisibility(View.INVISIBLE);
		indexBar = (SideBar) layout.findViewById(R.id.sideBar);
		indexBar.setListView(lvContact);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_APPLICATION,
				WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
						| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
				PixelFormat.TRANSLUCENT);
		mWindowManager.addView(mDialogText, lp);
		indexBar.setTextView(mDialogText);
		layout_head = ctx.getLayoutInflater().inflate(
				R.layout.layout_head_contact, null);
		lvContact.addHeaderView(layout_head);

	}

	@Override
	public void onDestroy() {
		mWindowManager.removeView(mDialogText);
		super.onDestroy();
	}

	/**
	 * 刷新页面
	 */
	public void refresh() {
		initData();
	}

	private void initData() {

		if(GloableParams.Contacts != null && GloableParams.Contacts.size() > 0)
			return;			
		// 加入小客服
		ContactUser kefu = new ContactUser();
		kefu.setUserName("kefu");
		kefu.setTelephone("12345");
		kefu.setSex("");
		kefu.setId("1");		
		GloableParams.Contacts.add(0, kefu);
		ContactUser kefu1 = new ContactUser();
		kefu1.setUserName("叶三多");
		kefu1.setTelephone("12345");
		kefu1.setSex("");
		kefu1.setId("2");		
		GloableParams.Contacts.add(0, kefu1);
		ContactUser kefu2 = new ContactUser();
		kefu2.setUserName("fox");
		kefu2.setTelephone("12345");
		kefu2.setSex("");
		kefu2.setId("3");		
		GloableParams.Contacts.add(0, kefu2);
		ContactUser kefu3 = new ContactUser();
		kefu3.setUserName("12345");
		kefu3.setTelephone("12345");
		kefu3.setSex("");
		kefu3.setId("4");		
		GloableParams.Contacts.add(0, kefu3);		
		lvContact.setAdapter(new ContactAdapter(getActivity(),
				GloableParams.Contacts));		
	}

	private void setOnListener() {
		lvContact.setOnItemClickListener(this);
		layout_head.findViewById(R.id.layout_addfriend)
				.setOnClickListener(this);
		layout_head.findViewById(R.id.layout_search).setOnClickListener(this);
		layout_head.findViewById(R.id.layout_group).setOnClickListener(this);
		layout_head.findViewById(R.id.layout_public).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.layout_search) {// 搜索好友及公众号
			//	Utils.start_Activity(getActivity(), SearchActivity.class);
		} else if (id == R.id.layout_addfriend) {// 添加好友
			Utils.start_Activity(getActivity(), ContactActivity.class);
		} else if (id == R.id.layout_group) {// 群聊
			Utils.start_Activity(getActivity(), SMSActivity.class);
		} else if (id == R.id.layout_public) {// 公众号
			//	Utils.start_Activity(getActivity(), PublishUserListActivity.class);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}
}
