package com.example.pluginmain;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.plugin.core.PluginThemeHelper;
import com.plugin.core.annotation.AnnotationProcessor;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class TestSkinActivity extends Activity {

	private static final String SKIN_PLUGIN_ID = "com.example.plugintest";
	private static final String SKIN_KEY = "shinId";
	private static final String SKIN_NAME_2 = "PluginTheme2";
	private static final String SKIN_NAME_4 = "PluginTheme4";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		applyTheme();

		super.onCreate(savedInstanceState);

		setContentView(R.layout.shin_activity);

		setTitle("宿主换肤");

		initView();

	}

	private void applyTheme() {
		int skin = PreferenceManager.getDefaultSharedPreferences(getApplication()).getInt(SKIN_KEY, 0);
		if (skin != 0) {
			PluginThemeHelper.applyPluginTheme(this, SKIN_PLUGIN_ID, skin);
		}
	}

	private void initView() {

		findViewById(R.id.allThemes).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				HashMap<String, Integer> themes = PluginThemeHelper.getAllPluginThemes(SKIN_PLUGIN_ID);
				Iterator<Entry<String, Integer>> itr = themes.entrySet().iterator();
				String text = "";
				while (itr.hasNext()) {
					Entry<String, Integer> entry = itr.next();
					text = text + entry.getKey() + ":" + entry.getValue() + "\n";
				}

				AlertDialog.Builder builder = new AlertDialog.Builder(TestSkinActivity.this);
				TextView tv = new TextView(TestSkinActivity.this);
				builder.setView(tv);
				tv.setText(text);
				builder.setTitle("插件可选主题列表");

				AlertDialog dialog = builder.create();
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
			}
		});

		findViewById(R.id.blue).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (AnnotationProcessor.getComponentContainer(TestSkinActivity.class) != null) {
					Toast.makeText(TestSkinActivity.this, "控件级插件和换肤功能不能共存", Toast.LENGTH_SHORT).show();
					return;
				}

				int themeId = PluginThemeHelper.getPluginThemeIdByName(SKIN_PLUGIN_ID, SKIN_NAME_2);
				PreferenceManager.getDefaultSharedPreferences(getApplication()).edit().putInt(SKIN_KEY, themeId).commit();

				if (Build.VERSION.SDK_INT >= 11) {
					//重启使主题生效
					TestSkinActivity.this.recreate();
				}
			}
		});

		findViewById(R.id.red).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (AnnotationProcessor.getComponentContainer(TestSkinActivity.class) != null) {
					Toast.makeText(TestSkinActivity.this, "控件级插件和换肤功能不能共存", Toast.LENGTH_SHORT).show();
					return;
				}

				int themeId = PluginThemeHelper.getPluginThemeIdByName(SKIN_PLUGIN_ID, SKIN_NAME_4);
				PreferenceManager.getDefaultSharedPreferences(getApplication()).edit().putInt(SKIN_KEY, themeId).commit();

				if (Build.VERSION.SDK_INT >= 11) {
					//重启使主题生效
					TestSkinActivity.this.recreate();
				}
			}
		});

		findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PreferenceManager.getDefaultSharedPreferences(getApplication()).edit().remove(SKIN_KEY).commit();
				if (Build.VERSION.SDK_INT >= 11) {
					//重启使主题生效
					TestSkinActivity.this.recreate();
				}
			}
		});

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

@@ -274,37 +274,8 @@
 iew;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -317,63 +317,8 @@
 per;
-%0Aimport com.plugin.core.annotation.AnnotationProcessor;
 %0A%0Aim
@@ -2066,202 +2066,8 @@
  %7B%0A%0A
-%09%09%09%09if (AnnotationProcessor.getComponentContainer(TestSkinActivity.class) != null) %7B%0A%09%09%09%09%09Toast.makeText(TestSkinActivity.this, %22%E6%8E%A7%E4%BB%B6%E7%BA%A7%E6%8F%92%E4%BB%B6%E5%92%8C%E6%8D%A2%E8%82%A4%E5%8A%9F%E8%83%BD%E4%B8%8D%E8%83%BD%E5%85%B1%E5%AD%98%22, Toast.LENGTH_SHORT).show();%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%0A
 %09%09%09%09
@@ -2498,202 +2498,8 @@
  %7B%0A%0A
-%09%09%09%09if (AnnotationProcessor.getComponentContainer(TestSkinActivity.class) != null) %7B%0A%09%09%09%09%09Toast.makeText(TestSkinActivity.this, %22%E6%8E%A7%E4%BB%B6%E7%BA%A7%E6%8F%92%E4%BB%B6%E5%92%8C%E6%8D%A2%E8%82%A4%E5%8A%9F%E8%83%BD%E4%B8%8D%E8%83%BD%E5%85%B1%E5%AD%98%22, Toast.LENGTH_SHORT).show();%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%0A
 %09%09%09%09

update demo