package com.backfire.popupwindow;

import java.util.ArrayList;
import java.util.List;

import net.yeeyo.doudizhu.feiyu.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.common.JSONUtil;
import com.backfire.doudizhu.exercise.ExerciseInFo;
import com.backfire.doudizhu.userinfo.UserMessageCenter;

public class NoticePopupWindow extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private List<ExerciseInFo> myNotices = null;
	private ListView notice_list;
	private NoticeViewAdapter noticeListAdapter = null;
	private Button select_submit;
	private TextView system_msg_text;
	/**
	 * @param activity
	 */
	public NoticePopupWindow(ActivityBase activity) {
		super(activity);
		this.initSetting();
	}
	private void initData(View view){
		JSONArray res = JSONUtil.parseArray(UserMessageCenter.getInstance().getNotifyMsg());
		if(res!=null){
			myNotices = new ArrayList<ExerciseInFo>();
			try {
				for(int i=0,n=res.length();i<n;i++){
					JSONObject e = res.getJSONObject(i);
					ExerciseInFo info = new ExerciseInFo();
					info.setTitle(JSONUtil.getString(e, "title", ""));
					info.setDesc(JSONUtil.getString(e, "desc", ""));
					myNotices.add(info);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if(myNotices!=null && myNotices.size()>0){
				((ListView)view.findViewById(R.id.notify_message_list)).setVisibility(View.VISIBLE);
				system_msg_text.setVisibility(View.GONE);
				if (notice_list == null) {
					noticeListAdapter = new NoticeViewAdapter(this.mActivity);
					notice_list = (ListView)view.findViewById(R.id.notify_message_list);
					notice_list.setVerticalScrollBarEnabled(true);
					notice_list.setAdapter(noticeListAdapter);
					notice_list.setSelected(true);
					notice_list.setSelection(0);
				}else
				{
					noticeListAdapter.notifyDataSetChanged();
				}
			}else{
				setNoticeZero();
			}
		}else{
			setNoticeZero();
		}
	}
	private void setNoticeZero(){
		system_msg_text.setVisibility(View.VISIBLE);
		((ListView)view.findViewById(R.id.notify_message_list)).setVisibility(View.GONE);
		system_msg_text.setText("暂无公告");
	}
	private void initSetting(){
		try {
			LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			int layoutRourse = R.layout.notice_pop;
			view = mLayoutInflater.inflate(layoutRourse, null);
			
			view.setFocusableInTouchMode(true);
			
			select_submit = (Button) view.findViewById(R.id.select_submit);
			((Button)view.findViewById(R.id.select_submit)).getPaint().setFakeBoldText(true);
			system_msg_text = (TextView) view.findViewById(R.id.system_msg_text);
			
			initData(view);
			
			this.setFocusable(true);
			this.setConsumeKeyEvent(false);
			ImageView popupview = (ImageView) view.findViewById(R.id.select_bg);
			width = popupview.getLayoutParams().width;
			height = popupview.getLayoutParams().height;
			this.createPopupWindow(view, width, height);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 为各组件设置监听器
	 */
	public void setListens(final OnClickListener submit) {
		select_submit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				if(submit!=null)submit.onClick(arg0);
				
				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 NoticeViewAdapter extends BaseAdapter
	{
		
		private LayoutInflater mLayoutInflater;
		public NoticeViewAdapter(Context context)
		{
			this.mLayoutInflater = LayoutInflater.from(context);
		}
		public int getCount() {
			int count = 0;
			if (myNotices != null) {
				count =  myNotices.size();
			}
			return count;
		}
		public Object getItem(int position) {
			return null;
		}
		public long getItemId(int position) {
			return 0;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			NoticeViewHolder holder = null;
			if( convertView == null)
			{
				holder = new NoticeViewHolder();
				convertView = this.mLayoutInflater.inflate(R.layout.exer_list_item, null);
				
				holder.title = (TextView)convertView.findViewById(R.id.title);
				holder.desc = (TextView)convertView.findViewById(R.id.desc);
				convertView.setTag(holder);
			}
			else
			{
				holder = (NoticeViewHolder)convertView.getTag();
			}
			ExerciseInFo exercise = myNotices.get(position);
			if (exercise != null) {
				//holder.icon.setImageBitmap();
				holder.title.setText(Html.fromHtml(exercise.getTitle()));
				holder.desc.setText(Html.fromHtml(exercise.getDesc()));
			}
			return convertView;
		}
	}
	public final class NoticeViewHolder {
		public TextView title; 		// 标题
		public TextView desc; 			//描述
	}
}

