package com.example.test.myweather.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.myweather.R;
import com.example.test.myweather.httputil.CallBackListener;
import com.example.test.myweather.httputil.HttpUtil;
import com.example.test.myweather.httputil.Utility;

import java.text.DateFormat;
import java.util.Date;

public class WeatherInfoActivity extends AppCompatActivity {
    private TextView title;
    private Button switch_city;
    private Button refresh;
    private TextView publish_time;
    private TextView current_date;
    private TextView weatherInfo_dec;
    private TextView temp_show;
    private Date currentDate;
    private Intent intent;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_info);

        title = (TextView) findViewById(R.id.title_textView);
        switch_city = (Button) findViewById(R.id.switch_city_button);
        refresh = (Button) findViewById(R.id.refresh_button);
        publish_time = (TextView) findViewById(R.id.publish_time_textView);
        current_date = (TextView) findViewById(R.id.current_data_textView);
        weatherInfo_dec = (TextView) findViewById(R.id.weather_des_textView);
        temp_show = (TextView) findViewById(R.id.temp_show_textView);

        intent = getIntent();
        String county_name = intent.getStringExtra("county_name");
        String county_code = intent.getStringExtra("county_code");
        title.setText(county_name);
        queryWeatherCode(county_code);
    }

    private void queryWeatherInfo(String weatherCode) {
        String weatherInfo_address = "http://www.weather.com.cn/data/cityinfo/" + weatherCode + ".html";
        HttpUtil.sendRequest(weatherInfo_address, new CallBackListener() {
            @Override
            public void onFinish(String response) {
//                showProgress();
                Utility.handleWeatherInfo(WeatherInfoActivity.this, response);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        closeProgress();
                        SharedPreferences weatherInfo = PreferenceManager
                                .getDefaultSharedPreferences(WeatherInfoActivity.this);
                        current_date.setText(weatherInfo.getString("current_date", ""));
                        publish_time.setText(weatherInfo.getString("publish_time", "") + " 发布");
                        weatherInfo_dec.setText(weatherInfo.getString("weather_des", ""));
                        temp_show.setText(weatherInfo.getString("low_temp", "")
                                + " ~ " + weatherInfo.getString("high_temp", ""));
                    }
                });
            }

            @Override
            public void onError(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        closeProgress();
                        Toast.makeText(WeatherInfoActivity.this, "加载失败", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    private void queryWeatherCode(String county_code) {
        String address = "http://www.weather.com.cn/data/list3/city" + county_code + ".xml";
        HttpUtil.sendRequest(address, new CallBackListener() {
            @Override
            public void onFinish(String response) {
                if (response != null) {
                    String[] array = response.split("\\|");
                    String weatherCode = array[1];
//                    Log.d("bbbbb", weatherCode);
                    queryWeatherInfo(weatherCode);
                }
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }

    private void closeProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    private void showProgress() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("正在加载");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_weather_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}

@@ -379,16 +379,42 @@
 nuItem;%0A
+import android.view.View;%0A
 import a
@@ -813,16 +813,47 @@
 ctivity 
+implements View.OnClickListener
 %7B%0A    pr
@@ -1090,51 +1090,26 @@
 ate 
-Date currentDate;%0A    private Intent intent
+String county_code
 ;%0A  
@@ -1825,16 +1825,23 @@
         
+Intent 
 intent =
@@ -1925,31 +1925,24 @@
 %22);%0A        
-String 
 county_code 
@@ -2051,24 +2051,113 @@
 ounty_code);
+%0A%0A        switch_city.setOnClickListener(this);%0A        refresh.setOnClickListener(this);
 %0A    %7D%0A%0A    
@@ -5690,11 +5690,364 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onClick(View v) %7B%0A        switch (v.getId()) %7B%0A            case R.id.switch_city_button:%0A                Intent intent = new Intent(this, AreaActivity.class);%0A                startActivity(intent);%0A                break;%0A            case R.id.refresh_button:%0A                queryWeatherCode(county_code);%0A        %7D%0A    %7D
 %0A%7D%0A

create button listener