package com.lianxin.pubqq.main;

import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

//import com.lianxin.panqq.common.Constants;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.pubqq.main.SettingActivity;
import com.lianxin.pubqq.main.WebViewActivity;
import com.lianxin.pubqq.setup.SetupBasicActivity;
import com.lianxin.pubqq.setup.SetupContactActivity;
import com.lianxin.pubqq.setup.SetupExtendActivity;
import com.lianxin.pubqq.setup.SetupLogoffActivity;
import com.lianxin.pubqq.setup.SetupMoneyPswActivity;
import com.lianxin.pubqq.setup.SetupPasswordActivity;
import com.lianxin.pubqq.setup.SetupMoneyActivity;
import com.lianxin.pubqq.setup.SetupRegoutActivity;
import com.lianxin.panqq.main.PathUtil;
//import com.lianxin.panqq.utils.ImageLoader;
import com.lianxin.panqq.utils.PreferenceUtils;

//我
public class Fragment_Setup extends Fragment implements OnClickListener {
	private Activity ctx;
	private View layout;
	private TextView tvname, tv_accout;
	private ImageView img_userpic,img_sex;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_setup,
					null);
			initViews();
			initData();
			setOnListener();
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		return layout;
	}

	private void initViews() {
		img_userpic = (ImageView)layout.findViewById(R.id.head);
		img_sex = (ImageView)layout.findViewById(R.id.iv_sex);	
		tvname = (TextView) layout.findViewById(R.id.tvname);
		tv_accout = (TextView) layout.findViewById(R.id.tvmsg);
	
		String name = GloableParams.m_szUserName;
		int id = GloableParams.m_szUserId;
		if (name != null && !TextUtils.isEmpty(name))
			tvname.setText(name);
		tv_accout.setText( "杳信号：" + id);
		int iSex = GloableParams.myUserInfo.m_iSex;	
		if(iSex==2)
			img_sex.setImageResource(R.drawable.ic_sex_female);	
		else
			img_sex.setImageResource(R.drawable.ic_sex_male);	
	}

	void refreshNewUser() {
		//img_userpic = (ImageView)layout.findViewById(R.id.head); 
		//img_sex = (ImageView)layout.findViewById(R.id.iv_sex);		
		//tvname = (TextView) layout.findViewById(R.id.tvname);
		//tv_accout = (TextView) layout.findViewById(R.id.tvmsg);
	
		String name = GloableParams.m_szUserName;
		int id = GloableParams.m_szUserId;
		if (name != null && !TextUtils.isEmpty(name))
			tvname.setText(name);
		tv_accout.setText( "杳信号：" + id);
		int iSex = GloableParams.myUserInfo.m_iSex;	
		if(iSex==2)
			img_sex.setImageResource(R.drawable.ic_sex_female);	
		else
			img_sex.setImageResource(R.drawable.ic_sex_male);		
	}	
	
	private void setOnListener() {
		layout.findViewById(R.id.view_user).setOnClickListener(this);
		layout.findViewById(R.id.txt_basicsetup).setOnClickListener(this);
		layout.findViewById(R.id.txt_extendsetup).setOnClickListener(this);
		layout.findViewById(R.id.txt_contactsetup).setOnClickListener(this);
		layout.findViewById(R.id.txt_editpsw).setOnClickListener(this);		
		layout.findViewById(R.id.txt_album).setOnClickListener(this);
		layout.findViewById(R.id.txt_collect).setOnClickListener(this);
		layout.findViewById(R.id.txt_money).setOnClickListener(this);
		layout.findViewById(R.id.txt_card).setOnClickListener(this);
		layout.findViewById(R.id.txt_smail).setOnClickListener(this);
		layout.findViewById(R.id.txt_setting).setOnClickListener(this);
		layout.findViewById(R.id.txt_admin).setOnClickListener(this);		
		layout.findViewById(R.id.txt_check).setOnClickListener(this);
		layout.findViewById(R.id.txt_logoff).setOnClickListener(this);
		layout.findViewById(R.id.txt_regout).setOnClickListener(this);
		layout.findViewById(R.id.txt_exit).setOnClickListener(this);			
	}
	
	private void initData() {
		// TODO Auto-generated method stub
		//String UserPic = PreferenceUtils.getInstance(getActivity()).getSettingUserPic();		
		//if(UserPic != null&&new File(UserPic).exists())
	    //    ImageLoader.getInstance().displayImage(UserPic, img_userpic);
		PathUtil.curPlayId = GloableParams.m_szUserId;		
		String UserPic = PathUtil.getInstance().getHeadPath()+ "/"+GloableParams.m_szUserId+".jpg";	
		if(UserPic != null&&new File(UserPic).exists())
			UserheadUtils.displayImage(UserPic, img_userpic);		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.view_user) {
			Utils.start_Activity(getActivity(), MyCodeActivity.class);
		} else if (id == R.id.txt_basicsetup) {// 基本资料
			Utils.start_Activity(getActivity(), SetupBasicActivity.class);
		} else if (id == R.id.txt_extendsetup) {// 扩展资料
			Utils.start_Activity(getActivity(), SetupExtendActivity.class);
		} else if (id == R.id.txt_contactsetup) {// 联系方式
			Utils.start_Activity(getActivity(), SetupContactActivity.class);
		} else if (id == R.id.txt_money) {// 钱包
			//Utils.start_Activity(getActivity(), ContactWayActivity.class);
			Utils.start_Activity(getActivity(), SetupMoneyActivity.class);
		} else if (id == R.id.txt_album) {// 相册
			ContentValues contents = new ContentValues();
			contents.put("Title", getString(R.string.collection));
			contents.put("URL", "http://www.iqiyi.com/");
			Utils.start_Activity(getActivity(), WebViewActivity.class, contents);
		} else if (id == R.id.txt_collect) {// 收藏

			ContentValues contents2 = new ContentValues();
			contents2.put("Title", getString(R.string.collection));
			contents2.put("URL", "https://www.taobao.com/");
			Utils.start_Activity(getActivity(), WebViewActivity.class, contents2);

			//Utils.start_Activity(getActivity(), WebViewActivity.class);
		} else if (id == R.id.txt_card) {// 相册
			ContentValues contents3 = new ContentValues();
			contents3.put("Title", getString(R.string.games));
			contents3.put("URL", "https://www.taobao.com/");
			Utils.start_Activity(getActivity(), WebViewActivity.class, contents3);
		} else if (id == R.id.txt_smail) {// 表情
			ContentValues contents4 = new ContentValues();
			contents4.put("Title", getString(R.string.games));
			contents4.put("URL", "https://www.taobao.com/");
			Utils.start_Activity(getActivity(), WebViewActivity.class, contents4);
		} else if (id == R.id.txt_editpsw) {// 修改密码
			Utils.start_Activity(getActivity(), SetupPasswordActivity.class);
		} else if (id == R.id.txt_setting) {// 设置
			Utils.start_Activity(getActivity(), SettingActivity.class);
		} else if (id == R.id.txt_check) {// 手机验证
			Utils.start_Activity(getActivity(), SetupMoneyPswActivity.class);
		} else if (id == R.id.txt_logoff) {// 退出登录
			Utils.start_Activity(getActivity(), SetupLogoffActivity.class);
		} else if (id == R.id.txt_regout) {// 注销用户
			Utils.start_Activity(getActivity(), SetupRegoutActivity.class);
		} else if (id == R.id.txt_admin) {// 服务器管理
			Utils.start_Activity(getActivity(), AdminLogActivity.class);
		} else if (id == R.id.txt_exit) {// 退出程序
			MyApp.getInstance().exit();
			//Utils.finish(getActivity());
		}
	}

}