package com.smart.gtstrvl;

import java.io.UnsupportedEncodingException;

import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.util.StringUtils;
import com.example.mountaintravel.util.ToastUtil;
import com.example.mountaintravel.variable.Variable;
import com.example.mountaintravel.view.LoadingView;
import com.example.mountaintravel.view.TitleBarView;

import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MyCollectActivity extends BaseActivity implements OnClickListener {
	private WebView mycollect_webview;
	private static final String TAG = "MyCollectActivity";
	private String url = "";
	private RelativeLayout mycollect_relative;
	private LoadingView mycollect_loadingview;
	private SharedPreferences sp;
	LinearLayout mycollect_layout;
	private String phone = "";
	private String name = "";
	private boolean flag = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_collect);
		initTitleBar("我的收藏");
		sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
		mycollect_webview = (WebView) findViewById(R.id.mycollect_webview);
		mycollect_layout = (LinearLayout) findViewById(R.id.mycollect_layout);
		addview();
		WebSettings wSet = mycollect_webview.getSettings();
		wSet.setJavaScriptEnabled(true);
		mycollect_webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		mycollect_webview.getSettings().setDomStorageEnabled(false);
		mycollect_webview.getSettings().setAppCacheEnabled(false);
		url = "http://" + Variable.address
				+ "/terminal/member/area_favorite_list?sesskey="
				+ sp.getString("sesskey", "");
		mycollect_webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				mycollect_loadingview.start();

				if (url.contains("scenic/area_info?id")) {
					try {
						url = java.net.URLDecoder.decode(url, "utf-8");
						Intent intent = new Intent(MyCollectActivity.this,
								ScenicActivity.class);
						sp.edit().putBoolean("isMyCollect", true).commit();
						intent.putExtra("scenic", url);
						startActivity(intent);

						return true;

					} catch (UnsupportedEncodingException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();

					}
				}

				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				super.onReceivedError(view, errorCode, description, failingUrl);
				ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(MyCollectActivity.this.CONNECTIVITY_SERVICE);
				State mobile = cwjManager.getNetworkInfo(
						ConnectivityManager.TYPE_MOBILE).getState();
				State wifi = cwjManager.getNetworkInfo(
						ConnectivityManager.TYPE_WIFI).getState();
				if (!(mobile == State.CONNECTED || mobile == State.CONNECTING
						|| wifi == State.CONNECTED || wifi == State.CONNECTING)) {
					ToastUtil.show(MyCollectActivity.this, "网络异常，请检查网络");
				}
				flag = false;
				view.loadUrl("file:///android_asset/loading.html");

			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				flag = false;
				mycollect_loadingview.stop();
				mycollect_webview.setVisibility(View.VISIBLE);
				mycollect_relative.setVisibility(View.GONE);
				super.onPageFinished(view, url);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				flag = true;
				new Thread() {
					@Override
					public void run() {
						// super.run();
						int i = 0;
						while (flag) {
							i++;
							Log.d(TAG, i + "");
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							if (i > 60) {
								runOnUiThread(new Runnable() {
									public void run() {
										mycollect_webview
												.loadUrl("file:///android_asset/loading.html");
										flag = false;
									}
								});

							}

						}
					}

				}.start();
				mycollect_relative.setVisibility(View.VISIBLE);
				mycollect_webview.setVisibility(View.GONE);
				super.onPageStarted(view, url, favicon);
				if (url.contains("area_favorite_list")) {
					initTitleBar("我的收藏");
				}
				Log.d(TAG, url);
			}

		});

		mycollect_webview.loadUrl(url);
	}

	@SuppressLint("NewApi")
	public void addview() {
		WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		DisplayMetrics outMetrics = new DisplayMetrics();
		display.getRealMetrics(outMetrics);

		mycollect_relative = (RelativeLayout) findViewById(R.id.mycollect_relative);
		mycollect_loadingview = new LoadingView(getBaseContext(),
				outMetrics.widthPixels);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 6);
		// lp.addRule(RelativeLayout.ALIGN_BASELINE);
		mycollect_relative.setOnClickListener(this);
		// lp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		mycollect_relative.addView(mycollect_loadingview, lp);

	}

	public void onResume() {
		// TODO Auto-generated method stub
		mycollect_loadingview.start();
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		mycollect_loadingview.stop();
		super.onPause();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@SuppressLint("NewApi")
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		flag = false;
		if (mycollect_webview != null) {
			mycollect_layout.removeView(mycollect_webview);
			mycollect_webview.removeAllViews();
			mycollect_webview.onPause();
			mycollect_webview.destroy();
		}
	}
}
