package com.coolweather.app.activity;

import org.apache.http.impl.client.EntityEnclosingRequestWrapper;

import com.coolweather.app.R;
import com.coolweather.app.service.AutoUpdateService;
import com.coolweather.app.util.HttpCallbackListener;
import com.coolweather.app.util.HttpUtil;
import com.coolweather.app.util.Utility;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeatherActivity extends Activity {

	private LinearLayout weatherInfoLayout;
	private TextView cityNameText;
	private TextView publishText;
	private TextView temp1Text;
	private TextView temp2Text;
	private TextView currentDateText;
	private String countyCode;
	private TextView weatherDespText;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.weather_layout);
		
		weatherInfoLayout = (LinearLayout) findViewById(R.id.weather_info_layout);
		cityNameText = (TextView) findViewById(R.id.tv_city_name);
		publishText = (TextView) findViewById(R.id.tv_publish);
		weatherDespText = (TextView) findViewById(R.id.tv_weather_desp);
		temp1Text = (TextView) findViewById(R.id.tv_temp1);
		temp2Text = (TextView) findViewById(R.id.tv_temp2);
		currentDateText = (TextView) findViewById(R.id.tv_current_date);
		countyCode = getIntent().getStringExtra("county_code");
		if(!TextUtils.isEmpty(countyCode)){
			publishText.setText("Í¬²½ÖÐ...");
			weatherInfoLayout.setVisibility(View.INVISIBLE);
			cityNameText.setVisibility(View.INVISIBLE);
			queryWeatherCode(countyCode);
		}else {
			showWeather();
		}
	}
	/**
	 * ²éÑ¯ÏØ¼¶´úºÅ¶ÔÓ¦ÌìÆø´úºÅ
	 * @param countyCode
	 */
	private void queryWeatherCode(String countyCode) {
		// TODO Auto-generated method stub
		String address="http://www.weather.com.cn/data/list3/city"+countyCode+".xml";
		queryFromServer(address,"countyCode");
	}
	/**
	 * ¸ù¾ÝÌìÆø´úºÅ²éÑ¯¶ÔÓ¦ÌìÆø
	 * @param weatherCode
	 */
	
	private void queryWeatherInfo(String weatherCode){
		String address="http://www.weather.com.cn/data/cityinfo/"+weatherCode+".html";
		queryFromServer(address, "weatherCode");
	}
	
	
	private void queryFromServer(String address, final String code) {
		// TODO Auto-generated method stub
		HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
			
			@Override
			public void onFinish(String response) {
				// TODO Auto-generated method stub
				if ("countyCode".equals(code)) {
					if(!TextUtils.isEmpty(response)){
						//´Ó·þÎñÆ÷·µ»Ø½á¹ûÖÐ½âÎö³öÌìÆø´úºÅ
						String[] array=response.split("\\|");
						if(array!=null&&array.length==2){
							String weatherCode=array[1];
							queryWeatherInfo(weatherCode);
						}
					}
				}else if ("weatherCode".equals(code)) {
					//´¦Àí·µ»ØµÄÌìÆøÐÅÏ¢
					Utility.handleWeatherResponse(WeatherActivity.this, response);
					runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							showWeather();
						}
					});
				}
			}
			
			@Override
			public void onError(Exception e) {
				// TODO Auto-generated method stub
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						publishText.setText("Í¬²½Ê§°Ü");
					}
				});
			}
		});
	}
	/**
	 * ´ÓSPÖÐ¶ÁÈ¡´æ´¢µÄÌìÆøÐÅÏ¢£¬²¢ÏÔÊ¾µ½½çÃæÉÏ
	 */
	private void showWeather() {
		SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(this);
		cityNameText.setText(preferences.getString("city_name", ""));
		temp1Text.setText(preferences.getString("temp1", ""));
		temp2Text.setText(preferences.getString("temp2", ""));
		weatherDespText.setText(preferences.getString("weather_desp", ""));
		publishText.setText("½ñÌì"+preferences.getString("publish_time", "")+"·¢²¼");
		currentDateText.setText(preferences.getString("current_date", ""));
		weatherInfoLayout.setVisibility(View.VISIBLE);
		cityNameText.setVisibility(View.VISIBLE);
		Intent intent=new Intent(this,AutoUpdateService.class);
		startService(intent);
	}
	/**
	 * ÇÐ»»³ÇÊÐ°´Å¥Âß¼­
	 * @param v
	 */
	public void switchCity(View v){
		Intent intent=new Intent(this,ChooseAreaActivity.class);
		intent.putExtra("from_weather_activity", true);
		startActivity(intent);
		finish();
	}
	public void refresh(View v){
		publishText.setText("Í¬²½ÖÐ...");
		SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(this);
		String weatherCode=preferences.getString("weather_code", "");
		if (!TextUtils.isEmpty(weatherCode)) {
			queryWeatherInfo(weatherCode);
		}
	}
}
