package com.backfire.popupwindow;

import java.util.TreeMap;

import net.yeeyo.doudizhu.feiyu.R;

import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.common.DeviceUtil;
import com.backfire.doudizhu.common.JSONUtil;
import com.backfire.doudizhu.common.LoadingProgressDialog;
import com.backfire.doudizhu.common.MyJson;
import com.backfire.doudizhu.lobby.LobbyActivity;
import com.backfire.doudizhu.userinfo.UserInfo;
import com.backfire.doudizhu.userinfo.UserMessageCenter;
import com.backfire.php.PHPCallbackInterface;
import com.backfire.php.PHPMethodName;
import com.backfire.php.PHPRequestPool;
import com.backfire.php.PHPResult;
import com.backfire.php.PHPResult.ErrorCode;
import com.backfire.room.Interface.ISendTips;
import com.backfire.utils.Log;

public class LoginAwardPopupWindow extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private ImageView[] day;
	private ImageView[] light;
	private TextView[] money;
	
	private Button getting;
	private ImageView info_close;
	AlphaAnimation animation;
	
	private TextView awardtip;
	private boolean isGot = false;
	
	private Drawable award_gat_first_1;
	private Drawable award_gat_first_3;
	
	private Drawable award_gat_other_1;
	private Drawable award_gat_other_3;
	/**
	 * @param activity
	 */
	public LoginAwardPopupWindow(ActivityBase activity,long[] dismoney,long[] disLottery) {
		super(activity);
		this.initSetting(dismoney,disLottery);
	}
	private void initSetting(long[] dismoney,long[] disLottery){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.login_award_pop;
		view = mLayoutInflater.inflate(layoutRourse, null);
		
		view.setFocusableInTouchMode(true);
	
		award_gat_first_1 = new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.award_gat_first_1, 0, 0));
		award_gat_first_3 = new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.award_gat_first_3, 0, 0));
		
		award_gat_other_1 = new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.award_gat_other_1, 0, 0));
		award_gat_other_3 = new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.award_gat_other_3, 0, 0));
	
		money = new TextView[5];
		money[0] = (TextView) view.findViewById(R.id.money1);
		money[1] = (TextView) view.findViewById(R.id.money2);
		money[2] = (TextView) view.findViewById(R.id.money3);
		money[3] = (TextView) view.findViewById(R.id.money4);
		money[4] = (TextView) view.findViewById(R.id.money5);
		
		day = new ImageView[5];
		day[0]  = (ImageView) view.findViewById(R.id.day1);
		day[1]  = (ImageView) view.findViewById(R.id.day2);
		day[2]  = (ImageView) view.findViewById(R.id.day3);
		day[3]  = (ImageView) view.findViewById(R.id.day4);
		day[4]  = (ImageView) view.findViewById(R.id.day5);
		
		light = new ImageView[5];
		light[0] = (ImageView) view.findViewById(R.id.light1);
		light[1] = (ImageView) view.findViewById(R.id.light2);
		light[2] = (ImageView) view.findViewById(R.id.light3);
		light[3] = (ImageView) view.findViewById(R.id.light4);
		light[4] = (ImageView) view.findViewById(R.id.light5);
		
		getting= (Button) view.findViewById(R.id.getting);
		((Button)view.findViewById(R.id.getting)).getPaint().setFakeBoldText(true);
		info_close= (ImageView) view.findViewById(R.id.info_close);
		
		awardtip = (TextView) view.findViewById(R.id.awardtip);
		awardtip.setText("");
		
		int loginTimes = UserMessageCenter.getInstance().getLoginTimes();
		for(int i=0;i<5;i++){
			String moneytext = String.valueOf(dismoney[i])+"金币";
			if(disLottery[i]>0){
				moneytext += "<br/>"+String.valueOf(disLottery[i])+"彩券";
			}
			money[i].setText(Html.fromHtml(moneytext));
			if(i<=loginTimes){
				if(i==0){
					day[i].setBackgroundDrawable(award_gat_first_3);
				}else{
					day[i].setBackgroundDrawable(award_gat_other_3);
				} 
			}else{
				if(i==0){
					day[i].setBackgroundDrawable(award_gat_first_1);
				}else{
					day[i].setBackgroundDrawable(award_gat_other_1);
				}
				money[i].setTextColor(Color.rgb(45, 45, 45));
			}
		}
		
		if (light[loginTimes] != null) {
			light[loginTimes].setImageResource(R.drawable.login_light_1);
			final float centerX = DeviceUtil.AdaptScreenHeight(mActivity, 83)/2;
			Log.v(this, "centerX:"+centerX);
			animation = new AlphaAnimation(1.0f,0);
			animation.setRepeatCount(Animation.INFINITE);
			animation.setFillAfter(true);
			animation.setDuration(500);
			animation.setRepeatMode(Animation.REVERSE);  
			light[loginTimes].startAnimation(animation);
		}
		
		this.setFocusable(true);
		this.setConsumeKeyEvent(false);
		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 mContext,final ISendTips iSendTips) {
		info_close.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
				
				if(null!=iSendTips){
					iSendTips.showSendTips();
				}
			}
			
		});
		getting.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				if(!isGot){
					UserInfo self = UserMessageCenter.getInstance().getUserInfo(); 
					TreeMap<String, Object> param = new TreeMap<String, Object>();
					param.put("uid", self.getUid());
					param.put("session",self.getSession());
				
					String json = new MyJson(param).toString();
					PHPRequestPool.getInstance().AddRequest(new LoginAwardCallback(mContext),
							PHPMethodName.CLI_AWARD_REQ, json);
					LoadingProgressDialog.show(mActivity,"领取奖励中，请稍等...");
				}else{
					CloseWindow();
					mContext.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
		
	}
	private class LoginAwardCallback implements PHPCallbackInterface {
		LobbyActivity mContext;
		public LoginAwardCallback(LobbyActivity context){
			mContext = context;
		}
		public void onFinish(PHPResult result) {
			LoadingProgressDialog.dissmissDlg();
			if (result != null && result.code == ErrorCode.SUCCESS) {
				JSONObject res = JSONUtil.parse(result.recvData);
				if(res!=null){
					int ret = JSONUtil.getInt(res, "ret", 2);
					if(ret==1){
						JSONObject money = JSONUtil.getJSONObject(res, "moneyBean");
						UserInfo self = UserMessageCenter.getInstance().getUserInfo(); 
						self.setMoney(self.getMoney()+JSONUtil.getLong(money, "money", 0));
						self.setVip(self.getVip()+JSONUtil.getInt(money, "vip", 0));
						self.setVipTime(self.getVipTime()+JSONUtil.getInt(money, "viptime", 0));
						
						
						self.setLottery(self.getLottery()+JSONUtil.getLong(money, "lottery", 0));
						self.setExchangeCard(self.getExchangeCard()+JSONUtil.getLong(money, "exchange", 0));
						
						String succMsg = JSONUtil.getString(res, "succMsg", "");
						if(succMsg!=null && succMsg.trim().length()>0){
							awardtip.setText(succMsg);
							DdzApplication.getApplication().showMoneyToast(succMsg);
						}
						int loginTimes = UserMessageCenter.getInstance().getLoginTimes();
						if (light[loginTimes] != null) {
							if(animation!=null){
								animation.cancel();
							}
							light[loginTimes].setAnimation(null);
							light[loginTimes].setVisibility(View.GONE);
						}
						if(day[loginTimes] != null){
							if(loginTimes==0){
								day[loginTimes].setBackgroundDrawable(award_gat_first_3);
							}else{
								day[loginTimes].setBackgroundDrawable(award_gat_other_3);
							}
						}
						isGot = true;
						getting.setText("更多金币");
						mContext.notifyModifyMoney();
					}else{
						int retCode = JSONUtil.getInt(res, "retCode", 2);
						String msg =JSONUtil.getString(res, "retMsg", "");
						showMessage(msg+"("+retCode+")");
					}
				}else {
					showMessage(DdzApplication.getApplication().getString(R.string.network_error));
				}
			} else {
				showMessage(DdzApplication.getApplication().getString(R.string.network_error));
			}
		}
	}
	private void showMessage(String msg){
		DdzApplication.getApplication().showToast(msg);
	}
}

