package com.moonweather.app.activity;

import com.moonweather.app.R;
import com.moonweather.app.service.AutoUpdateService;
import com.moonweather.app.util.HttpCallbackListener;
import com.moonweather.app.util.HttpUtil;
import com.moonweather.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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherActivity extends Activity {
     private LinearLayout weatherInfoLayout;
     private TextView  cityNameText;//ÏÔÊ¾³ÇÊÐÃû
     private TextView publishtext;//ÓÃÓÚÏÔÊ¾·¢²¼Ê±¼ä
     private TextView weatherDespText;//ÓÃÓÚÏÔÊ¾ÌìÆøÃèÊöÐÅÏ¢
     private TextView temp1Text;//ÓÃÓÚÏÔÊ¾ÆøÎÂ1
     private TextView temp2Text;//ÓÃÓÚÏÔÊ¾ÆøÎÂ2
     private TextView currentDateText;//ÓÃÓÚÏÔÊ¾µ±Ç°µÄÈÕÆÚ
     
     private Button switchCity;
     private Button refreshWeather;
     
     
     @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.city_name);
    	publishtext=(TextView) findViewById(R.id.publish_text);
    	weatherDespText=(TextView) findViewById(R.id.weather_desp);
    	temp1Text=(TextView) findViewById(R.id.temp1);
    	temp2Text=(TextView) findViewById(R.id.temp2);
    	currentDateText=(TextView) findViewById(R.id.current_data);
    	String countyCode=getIntent().getStringExtra("county_code");
    	
    	switchCity=(Button) findViewById(R.id.switch_city);
    	refreshWeather=(Button) findViewById(R.id.refresh_weather);
    	
    	//ÊÖ¶¯ÇÐ»»³ÇÊÐºÍ¸üÐÂÌìÆø
    	switchCity.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(WeatherActivity.this,ChooseAreaActivity.class);
				intent.putExtra("from_weather_activity", true);
				startActivity(intent);
				finish();
			}
		});
    	refreshWeather.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			publishtext.setText("Í¬²½ÖÐ....");
			SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this);
			String weatherCode=prefs.getString("weather_code", "");
			if (!TextUtils.isEmpty(weatherCode)) {
				queryWeatherInfo(weatherCode);
			  }
			}
		});
    	
    	
    	if (!TextUtils.isEmpty(countyCode)) {
			//ÓÐÏØ¼¶´úºÅÊ±¾ÍÈ¥²éÑ¯ÌìÆø
    		publishtext.setText("Í¬²½ÖÐ....");
    		weatherInfoLayout.setVisibility(View.INVISIBLE);
    		cityNameText.setVisibility(View.VISIBLE);
    		queryWeatherCode(countyCode);
		}else {
			//Ã»ÓÐÏØ¼¶´úºÅÊ±£¬Ö±½ÓÏÔÊ¾±¾µØÌìÆø
			showWeather();
		}
    }
     //²éÑ¯ÏØ¼¶´úºÅËù¶ÔÓ¦µÄÌìÆø´úÂë
	private void queryWeatherCode(String countyCode){
    	 String address="http://www.weather.com.cn/data/list3/city"+countyCode+".xml";
    	 queryFromServer(address,"countyCode");
     }
	//²éÑ¯ÌìÆø´úºÅËù¶ÔÓ¦µÄÌìÆø
	private void queryWeatherInfo(String weatherCode){
		String address="http://www.weather.com.cn/data/cityinfo/"+weatherCode+".html";
		queryFromServer(address,"weatherCode");
	}
	//¸ù¾Ý´«ÈëµÄµØÖ·ºÍÀàÐÍÈ¥Ïò·þÎñÆ÷²éÑ¯ÌìÆø´úºÅºÍÌìÆøÐÅÏ¢
	private void queryFromServer(final String address,final String type){
		HttpUtil.sendHttpResquest(address, new HttpCallbackListener() {
			
			@Override
			public void onFinish(final String response) {
				// TODO Auto-generated method stub
				if("countyCode".equals(type)){
					if(!TextUtils.isEmpty(response)){
						//´Ó·þÎñÆ÷·µ»ØµÄÊý¾ÝÖÐ½âÎö³öÌìÆø´úºÅ
						String[] array=response.split("\\|");
						if (array!=null&&array.length==2) {
							String weatherCode=array[1];
							queryWeatherInfo(weatherCode);
						}
					}//ÕâÀïÔø¾­ÒòÎªÀ¨ºÅµÄÎÊÌâÌìÆø²»ÄÜÏÔÊ¾£¬¾À½á´¦¡£¡£¡£¡£¡£¡£
				}else if ("weatherCode".equals(type)) {
						//´¦Àí·þÎñÆ÷·µ»ØµÄÌìÆøÐÅÏ¢
						Utility.handleWeatherResponse(WeatherActivity.this, response);
						runOnUiThread( new Runnable() {
							public void run() {
							showWeather();	
							}
						});
					}
			}
			
			@Override
			public void onError(Exception e) {
				// TODO Auto-generated method stub
				runOnUiThread(new Runnable() {
					public void run() {
					publishtext.setText("Í¬²½Ê§°Ü");	
					}
				});
			}
		});
	}
	//´ÓSharedPreferencesÎÄ¼þÖÐ¶ÁÈ¡´æ´¢µÄÌìÆøÐÅÏ¢£¬²¢ÇÒÏÔÊ¾ÔÚ½ÖÃæÉÏ
	private void showWeather() {
		SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
		cityNameText.setText(prefs.getString("city_name", ""));
		temp1Text.setText(prefs.getString("temp1",""));
		temp2Text.setText(prefs.getString("temp2",""));
		weatherDespText.setText(prefs.getString("weather_desp", ""));
		publishtext.setText("½ñÌì"+prefs.getString("publish_time", "")+"·¢²¼");
		currentDateText.setText(prefs.getString("current_date", ""));
		weatherInfoLayout.setVisibility(View.VISIBLE);
		cityNameText.setVisibility(View.VISIBLE);
		Intent intent=new Intent(this,AutoUpdateService.class);
		startService(intent);
	}
}
