package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.lobby.LobbyActivity;
import com.backfire.doudizhu.userinfo.UserInfo;
import com.backfire.doudizhu.userinfo.UserMessageCenter;

public class UserInfoPop extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private TextView user_nick;
	private TextView user_sex;
	private TextView user_money;
	private TextView user_email;
	private TextView user_uid;
	private TextView user_win_count;
	private TextView user_sign;
	
	private TextView user_lottery;
	private TextView user_exchange;
	
	private ImageView info_icon;
	private ImageView info_close;
	private ImageView vip_level;
	private ImageView buy_money;
	
	
	private ActivityBase activity;
	/**
	 * @param activity
	 */
	public UserInfoPop(ActivityBase activity) {
		super(activity);
		this.activity=activity;
		this.initSetting();
	}
	private void initSetting(){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.userinfo_pop;
		view = mLayoutInflater.inflate(layoutRourse, null);
		
		view.setFocusableInTouchMode(true);
		 
		user_nick = (TextView) view.findViewById(R.id.user_nick);
		user_money = (TextView) view.findViewById(R.id.user_money);
		user_email = (TextView) view.findViewById(R.id.user_email);
		user_uid = (TextView) view.findViewById(R.id.user_uid);
		user_sex = (TextView) view.findViewById(R.id.user_sex);
		user_win_count = (TextView) view.findViewById(R.id.user_win_count);
		user_sign = (TextView) view.findViewById(R.id.user_sign);
		user_lottery = (TextView) view.findViewById(R.id.user_lottery);
		user_exchange = (TextView) view.findViewById(R.id.user_exchange);
		
		info_icon = (ImageView) view.findViewById(R.id.info_icon);
		vip_level = (ImageView) view.findViewById(R.id.vip_level);
		info_close= (ImageView) view.findViewById(R.id.info_close);
		buy_money= (ImageView) view.findViewById(R.id.buy_money);
		
		
		UserInfo myself = UserMessageCenter.getInstance().getUserInfo();
		
		if(myself.getVip()>0){
			vip_level.setBackgroundResource(R.drawable.vip_has);
		}else{
			vip_level.setBackgroundResource(R.drawable.vip_null);
		}
		user_uid.setText("UID:"+myself.getUid());
		if(myself.getNick()!=null && myself.getNick().trim().length()>0){
			user_nick.setText("昵称："+myself.getNick());
		}else{
			user_nick.setText("昵称：未知");
		}
		user_money.setText("金币 : "+UserMessageCenter.getItemMoney(myself.getMoney()));
		user_lottery.setText("彩券 : "+UserMessageCenter.getLobbyMoney(myself.getLottery()));
		user_exchange.setText("无敌卡 : "+UserMessageCenter.getLobbyMoney(myself.getExchangeCard()));
		
		String uname = myself.getEmail();
		if(uname!=null && uname.trim().length()>0){
			uname = "邮箱："+uname;
		}else{
			uname = "邮箱：未知";
		}
		user_email.setText(uname);
		info_icon.setImageResource(UserMessageCenter.getUserIcon(myself.getIcon()));
		
		if(myself.getSex()==2){
			user_sex.setText("性别：女");
		}else{
			user_sex.setText("性别：男");
		}
		
		user_win_count.setText("战绩："+myself.getWinCount()+"胜");
		if(myself.getSignature()!=null && myself.getSignature().trim().length()>0){
			user_sign.setText("个性签名："+myself.getSignature());
		}else{
			user_sign.setText("个性签名：这家伙很屌丝，什么都没留下！");
		}
		
		this.setFocusable(true);
		this.setConsumeKeyEvent(true);
		ImageView popupview = (ImageView) view.findViewById(R.id.info_bg);
		width = popupview.getLayoutParams().width;
		height = popupview.getLayoutParams().height;
		this.createPopupWindow(view, width, height);
	}
	
	/**
	 * 为各组件设置监听器
	 */
	public void setListens(final LobbyActivity context) {
//		modify_nick.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View arg0) {
//				CloseWindow();
//				ModifyUserMsgPopupWindow modify = new ModifyUserMsgPopupWindow(context);
//				modify.setListens();
//				modify.ShowWindowInCenter();
//			}
//			
//		});
		info_icon.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
//				IconSelectPopupWindow iconselect = new IconSelectPopupWindow(context);
//				iconselect.setListens(context);
//				iconselect.ShowWindowInCenter();
				
				ModifyUserInfoPop modify = new ModifyUserInfoPop(context,UserMessageCenter.getInstance().getUserInfo().getIcon());
				modify.setListens(context);
				modify.ShowWindowInCenter();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				CloseWindow();
				return false;
			}
		});
		info_close.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		buy_money.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				context.quickBuy(1);
				//Toast.makeText(activity, "功能暂未开放", Toast.LENGTH_SHORT).show();
				
				mActivity.playBtnPressMusic();
			}
			
		});
	}
	
	@Override
	protected boolean popupWindowWillShow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void popupWindowDidShow() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void popupWindowWillDissmiss() {
		// TODO Auto-generated method stub
		
	}

}
