package com.hjx.miaohongentry.util;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.provider.Settings;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hjx.miaohongentry.activity.WordActivity;


public class SpanTextViewUtils {

	public static long lastTime;// 上一次点击时间
	public static long currentTime;// 这一次点击时间

	private static View contentView;
	private static Dialog dialog;
	private static AnimationDrawable mAnimationDrawable;

	public static ForegroundColorSpan mForegroundColorSpan = new ForegroundColorSpan(Color.CYAN);



	/**
	 * 进入描红应用 adb shell am force-stop air.com.wyt.HanZiMiaoHong
	 * 
	 * @param context
	 * @param character
	 *            需要描红的汉字
	 */
	public static void gotoMiaohong(Context context, String character) {
		// 一个字的情况下，进入描红
		String pinyins = converterToSpell(character);
		String py = pinyins.split(",")[0].trim();
		boolean isCharacter = false;
		Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
		Matcher m = p.matcher(character);
		while (m.find()) {
			isCharacter = true;
		}
		if (isCharacter) {// 语文科目且是汉字才跳
			//&& Constants.OPENFLAG == 0//语文科目且
			Intent intent = new Intent();
			ComponentName cn = new ComponentName("air.com.wyt.HanZiMiaoHong", "air.com.wyt.HanZiMiaoHong.AppEntry");
			intent.setComponent(cn);
			// 描红传入规则：汉字&_拼音
			intent.putExtra("type", character.trim() + "&_" + py.trim());
			try {
			//	((Activity)context).startActivityForResult(intent, WordActivity.REQUEST_CODE_MIAO_HONG);
				context.startActivity(intent);
			} catch (Exception e) {
				 Toast.makeText(context, "未安装汉字描红！",
				 Toast.LENGTH_SHORT).show();
			//	return;
				//ToastUtil.showLong(context, context.getResources().getString(R.string.apk_none_hanzimiaohong));
			}
			StatisUtils.setEnterMiaohongTime(context, System.currentTimeMillis());
			StatisUtils.addTotalNum(context);
			int count = StatisUtils.getTodayNum(context);
			count ++;
			StatisUtils.setTodayNum(context,count);
		}
	}

	/**
	 * 汉字转换位汉语全拼，英文字符不变，特殊字符丢失
	 * 支持多音字，生成方式如（重当参:zhongdangcen,zhongdangcan,chongdangcen
	 * ,chongdangshen,zhongdangshen,chongdangcan）
	 * 
	 * @param chines
	 *            汉字
	 * @return 拼音
	 */
	public static String converterToSpell(String chines) {
		StringBuffer pinyinName = new StringBuffer();
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					// 取得当前汉字的所有全拼
					String[] strs = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
					if (strs != null) {
						for (int j = 0; j < strs.length; j++) {
							pinyinName.append(strs[j]);
							if (j != strs.length - 1) {
								pinyinName.append(",");
							}
						}
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName.append(nameChar[i]);
			}
			pinyinName.append(" ");
		}
		return pinyinName.toString();
	}

	public static ExecutorService executorService = Executors.newFixedThreadPool(5);



	private static void startAnimation() {
		if (mAnimationDrawable != null) {
			mAnimationDrawable.start();
			Timer mytimer = new Timer();
			TimerTask mytt = new TimerTask() {

				@Override
				public void run() {

					stopAnimation();
				}
			};
			mytimer.schedule(mytt, 1000);
		}
	}

	public static void stopAnimation() {
		if (mAnimationDrawable != null) {
			mAnimationDrawable.stop();
		}
	}

}
