package com.gh.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gh.common.constant.Config;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.RunningUtils;
import com.gh.common.util.ShareUtils;
import com.gh.common.util.Utils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBShowDialog;
import com.gh.gamecenter.listener.OnCallBackListener;
import com.gh.gamecenter.manager.SystemBarTintManager;
import com.gh.gamecenter.manager.SystemBarTintManager.SystemBarConfig;

import java.lang.reflect.Field;
import java.util.ArrayList;

import cn.sharesdk.framework.ShareSDK;
import de.greenrobot.event.EventBus;

public class BaseActivity extends Activity implements OnCallBackListener {

	private SystemBarTintManager tintManager;

	private boolean isPause;

	protected boolean isDestroy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.log(this.getClass().getSimpleName());
		isDestroy = false;
		AppController.getInstance().addActivity(this);
		EventBus.getDefault().register(this);
	}

	protected void init(View contentView, String title) {
		init(contentView);
		TextView actionbar_tv_title = (TextView) findViewById(R.id.actionbar_tv_title);
		actionbar_tv_title.setText(title);
	}

	protected void init(View contentView) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTheme(R.style.AppTheme);
			setTranslucentStatus(true);
			tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.theme);
			SystemBarConfig config = tintManager.getConfig();
			contentView.setPadding(0, config.getPixelInsetTop(false), 0,
					config.getPixelInsetBottom());
		}

		setContentView(contentView);

		int actionbar_height = getSharedPreferences(Config.PREFERENCE,
				Context.MODE_PRIVATE).getInt("actionbar_height",
				DisplayUtils.dip2px(getApplicationContext(), 48));

		RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);
		LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, actionbar_height);
		reuse_actionbar.setLayoutParams(lparams);

		findViewById(R.id.actionbar_rl_back).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});

		//简化findViewById
		try {
			Class<?> clazz = this.getClass();
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				int id = Utils.getId(field.getName());
				if (id != -1) {
					Utils.log("reflect name = " + field.getName());
					field.setAccessible(true);
					Class<?> fieldType = field.getType();
					Object injectedValue = fieldType.cast(findViewById(id));
					field.set(this, injectedValue);
					field.setAccessible(false);
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	protected SystemBarTintManager getTintManager() {
		return tintManager;
	}

	@Override
	public void finish() {
		super.finish();
		AppController.getInstance().removeActivity(this);
	}

	public void toast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	public void toast(int msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	@TargetApi(19)
	protected void setTranslucentStatus(boolean status) {
		Window window = getWindow();
		WindowManager.LayoutParams winParams = window.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (status) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		window.setAttributes(winParams);
	}

	//如果是游戏分享，newsTitle默认为空
	public void showShare(String url, String gameName, String icon, String newsTitle, ArrayList<String> tag, String entrance, String type) {

		ShareSDK.initSDK(this);

		//判断是否是官方版
		boolean isPlugin = false;
		if (tag != null){
			for (String s : tag) {
				if (!"官方版".equals(s)){
					isPlugin = true;
				}
			}
		}

		ShareUtils.getInstance(this).showShareWindows(new View(this), url, gameName, icon, newsTitle, isPlugin);

		if (newsTitle == null){
			DataUtils.onEvent(this, "内容分享", gameName);
		}else {
			DataUtils.onEvent(this, "内容分享", newsTitle);
		}
	}


	public void onEventMainThread(final EBShowDialog showDialog) {
		if (!isPause && this.getClass().getName().equals(RunningUtils.getTopActivity(this))) {
			if ("hijack".equals(showDialog.getType())) {
				DialogUtils.showHijackDialog(this);
			} else if ("plugin".equals(showDialog.getType())) {
				DialogUtils.showPluginDialog(this, new DialogUtils.ConfiremListener(){
					@Override
					public void onConfirem() {
						startActivity(PackageUtils.getUninstallIntent(BaseActivity.this, showDialog.getPath()));
					}
				});
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		isDestroy = true;
		EventBus.getDefault().unregister(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		DataUtils.onPause(this);
		isPause = true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		DataUtils.onResume(this);
		isPause = false;
		DownloadManager.getInstance(this).initGameMap();
	}

	@Override
	public void loadDone() {
		
	}

	@Override
	public void loadDone(Object obj) {

	}

	@Override
	public void loadError() {

	}

	@Override
	public void loadEmpty() {

	}
}
