package com.mjsheng.myappstore.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;


import com.bumptech.glide.Glide;
import com.mjsheng.myappstore.comm.CommonDatas;
import com.mjsheng.myappstore.dialog.UpdateDialog;
import com.mjsheng.myappstore.utils.MySQLData;
import com.mjsheng.myappstore.utils.Utils;
import com.tianma.netdetector.lib.NetStateChangeObserver;
import com.tianma.netdetector.lib.NetStateChangeReceiver;
import com.tianma.netdetector.lib.NetworkType;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;


public abstract class BaseFragmentActivity extends FragmentActivity implements OnClickListener, NetStateChangeObserver {

	private UpdateReceiver mReceiver;
	private UpdateDialog updateDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initParameter();
		initViews();
		loadData();

	}
	@Override
	protected void attachBaseContext(Context newBase) {
		super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
	}
	public class UpdateReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (Utils.MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
				String title = intent.getStringExtra(Utils.KEY_TITLE);
				if (title != null && !title.equals("") ){
					if (title.equals("应用更新")) {
						if (updateDialog == null) {
							updateDialog = new UpdateDialog(BaseFragmentActivity.this, true);
							updateDialog.show();
						}
					}
				}
			}
		}
	}
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
//			initViewFull(this);
		}
	}
	public static void initViewFull(final Activity activity) {
		activity.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
			@Override
			public void onSystemUiVisibilityChange(int visibility) {

				new Thread() {
					@Override
					public void run() {
						super.run();
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								initViewFull(activity);

							}
						});
					}
				}.start();
			}
		});
		// //设置无标题
		activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		activity.getWindow().getDecorView().setSystemUiVisibility(
				View.SYSTEM_UI_FLAG_LAYOUT_STABLE
						| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
						| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
						| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
						| View.SYSTEM_UI_FLAG_IMMERSIVE);
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (needRegisterNetworkChangeObserver()) {
			NetStateChangeReceiver.registerObserver(this);
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
//		if (needRegisterNetworkChangeObserver()) {
//			NetStateChangeReceiver.unregisterObserver(this);
//		}
	}

	@Override
	public void onClick(View v) {

	}


	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
		if (level == TRIM_MEMORY_UI_HIDDEN){
			Glide.get(this).clearMemory();
		}
		Glide.get(this).trimMemory(level);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		Glide.get(this).clearMemory();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mReceiver != null) {
			unregisterReceiver(mReceiver);
		}
	}

	// --- 吐司 ---

	protected void showAppToast(String string) {
		Utils.showToast(this, string);
	}

	/**
	 * 监听
	 * @param ids
	 */
	public void setViewsListener(int... ids) {
		View v = null;
		for (int i = 0; i < ids.length; i++) {
			v = findViewById(ids[i]);
			v.setOnClickListener(this);
		}
	}

	protected void initParameter() {
		mReceiver = new UpdateReceiver();
		IntentFilter intent = new IntentFilter();
		intent.addAction(Utils.MESSAGE_RECEIVED_ACTION);
		intent.addAction(CommonDatas.BABYINFO_CHANGE);
		registerReceiver(mReceiver, intent);
	}

	protected void initViews() { }

	protected void loadData() {
	}

	@Override
	public void finish() {
		super.finish();
	}

	/**
	 * 是否需要注册网络变化的Observer,如果不需要监听网络变化,则返回false;否则返回true.默认返回false
	 */
	protected boolean needRegisterNetworkChangeObserver() {
		return true;
	}
	@Override
	public void onNetDisconnected() {
	}

	@Override
	public void onNetConnected(NetworkType networkType) {

	}
}
