package com.sabirjan.DrawUyghurWord;
import android.graphics.Paint;
import android.util.DisplayMetrics;


public class ConvertUL {

	
	
	
	
	
	
	
	private char[][]UyLettersUnicodeTab = new char[][]{
			{'\u0627', '\ufe8e', '\ufe8d', '\ufe8e', '\ufe8d','0'}
			,{'\u06d5', '\ufeea', '\ufee9', '\ufeea', '\ufee9','0'}
			,{'\u0628', '\ufe90', '\ufe91', '\ufe92', '\ufe8f','1'},	//b
        {'\u067e', '\ufb57', '\ufb58', '\ufb59', '\ufb56','1'},	//p
        {'\u062a', '\ufe96', '\ufe97', '\ufe98', '\ufe95','1'},	//t
        {'\u062c', '\ufe9e', '\ufe9f', '\ufea0', '\ufe9d','1'},	//J
        {'\u0686', '\ufb7b',  '\ufb7c','\ufb7d', '\ufb7a','1'},	//q
        {'\u062e', '\ufea6', '\ufea7', '\ufea8', '\ufea5','1'},	//H
        {'\u062f', '\ufeaa', '\ufea9', '\ufeaa', '\ufea9','0'},	//d
        {'\u0631', '\ufeae', '\ufead', '\ufeae', '\ufead','0'},	//r
        {'\u0632', '\ufeb0', '\ufeaf', '\ufeb0', '\ufeaf' ,'0'},	//z
        {'\u0698', '\ufb8b', '\ufb8a', '\ufb8b', '\ufb8a' ,'0'},	//D
        {'\u0633', '\ufeb2', '\ufeb3', '\ufeb4', '\ufeb1' ,'1'},	//s
        {'\u0634', '\ufeb6', '\ufeb7', '\ufeb8', '\ufeb5','1'},	//x
        {'\u063a', '\ufece', '\ufecf', '\ufed0', '\ufecd' ,'1'},	//C
        {'\u0641', '\ufed2', '\ufed3', '\ufed4', '\ufed1','1' },	//F
        {'\u0642', '\ufed6', '\ufed7', '\ufed8', '\ufed5','1' },	//j
        {'\u0643', '\ufeda', '\ufedb', '\ufedc', '\ufed9' ,'1'},	//k
        {'\u06af', '\ufb93', '\ufb94', '\ufb95', '\ufb92','1' },	//G
        {'\u06ad', '\ufbd4', '\ufbd5', '\ufbd6', '\ufbd3' ,'1'},	//i
        {'\u0644', '\ufede', '\ufedf', '\ufee0', '\ufedd' ,'1'},	//l
        {'\u0645', '\ufee2', '\ufee3', '\ufee4', '\ufee1' ,'1'},	//m
        {'\u0646', '\ufee6', '\ufee7', '\ufee8', '\ufee5' ,'1'},	//n
        {'\u06be', '\ufbab', '\ufbac', '\ufbad', '\ufbaa' ,'1'},	//a
        {'\u0648', '\ufeee', '\ufeed', '\ufeee', '\ufeed','0' },	//o
        {'\u06c7', '\ufbd8', '\ufbd7', '\ufbd8', '\ufbd7', '0'},	//u
        {'\u06c6', '\ufbda', '\ufbd9', '\ufbda', '\ufbd9', '0'},	//K
        {'\u06c8', '\ufbdc', '\ufbdb', '\ufbdc', '\ufbdb' ,'0'},	//v
        {'\u06cb', '\ufbdf', '\ufbde', '\ufbdf', '\ufbde','0' },	//w
        {'\u06d0', '\ufbe5', '\ufbe6', '\ufbe7', '\ufbe4', '1'},	//e
        {'\u0649', '\ufef0', '\ufbe8', '\ufbe9', '\ufeef', '1' },	//h
        {'\u064a', '\ufef2', '\ufef3', '\ufef4', '\ufef1' , '1'},	//y
        {'\u0626','\ufe8c', '\ufe8b', '\ufe8c', '\ufe8b' , '1'},	///
        {'\ufefb', '\ufefc', '\ufefb', '\ufefc', '\ufefb', '0'},	//lf
        };
	
	  private String uyghurchars = "اەبپتجچخدرزژسشغفقكگڭلمنھوۇۆۈۋېىيلس\u0627\ufe8d\ufe8e\u06D5\u0628\u067E\u062A\u062C\u0686\u062E\u062F\u0631\u0632\u0698\u0633\u0634\u063A\u0641\u0642\u0643\u06AF\u06AD\u0644\u0645\u0646\u06BE\u0648\u06C7\u06C6\u06C8\u06CB\u06D0\u0649\u064A\u0626";
		 private boolean isUyghurWord(char word)
		 {
			 
				if(uyghurchars.indexOf(word)>0)
				{
					return true;
				}
			return false;
		}
		 public String GetULUyghurWord(String word)
			{
				
				MyStringTokenizer cls=new MyStringTokenizer();
				String result=word;
				String []str=cls.GetSplitWord2(word);
				result="";
				Paint paintHead=new Paint();
				String strtmp="";
				for(int i=0;i<str.length;i++)
				{
					try
					{

				//		System.out.println("Word is="+str[i][0]);
						
						if(str[i].trim().length()>0)
						{		
							
							char c=str[i].trim().charAt(0);
							if(isUyghurWord(c))
							{
							
								strtmp=ScriptShape(str[i].toCharArray(),str[i].length());
							//	System.out.println("--Width"+width+"strWidth="+paintHead.measureText(str[i])+"-------HHHHHHHHH-------------"+str[i]+"---------");
								result+=Almaxturux(strtmp)+" ";
								
								
							}
							else
							{
								result+=" "+str[i]+" ";
							}
						}

					}
					catch(Exception ex)
					{
						ex.printStackTrace();
						result+=str[i]+" ****";
					}
				}
			//	System.out.println("1111222222222243333333333333333");
				return result;
			}
		private String Almaxturux(String str)
		{
			StringBuffer sb=new StringBuffer();
			for(int i=str.length()-1;i>=0;i--)
			{
				sb.append(str.charAt(i));
			}
			return sb.toString();
		}
		 public String getShape(String str)
		 {
			 String s="";
			 String []swords=str.split(" ");
			 for(int i=0;i<swords.length;i++)
			 {
				 s+=ScriptShape(swords[i].toCharArray(),swords[i].length())+" ";
			 }
			 return s;
		 }
		 
	   public String ScriptShape(char[] chars, int iLen)
       {
           char curInputChar;
           char chLastLink = ' ';
           char chSecondLastLink = '\0';
           int iLastCharIndex = 0;
           int iLastShape = 0;
           int i, k;
          
           StringBuffer strUniExt = new StringBuffer();
       //    System.out.println("Word ="+chars.toString());
           for (k = 0; k < chars.length; k++)
           {
        	   try
        	   {
               curInputChar = chars[k];
               for (i = 0; i < 34; i++)
               {
                   if (curInputChar == UyLettersUnicodeTab[i][0])
                   {
                       if (k == 0)
                       {
                           iLastShape = 2;
                           strUniExt.append(UyLettersUnicodeTab[i][iLastShape]);
                           chLastLink = UyLettersUnicodeTab[i][5];
                           iLastCharIndex = i;
                           break;
                       }
                       else
                       {
                           //��������һ���ַ����ǰ����ַ���lʽ�ַ�����Ҫ����ĸ�����l����ʽ
                           if (k == chars.length - 1 && chLastLink == '1')
                           {
                               iLastShape = 1;
                               if (i == 0 && iLastCharIndex == 20)
                               {
                                   if (chSecondLastLink == '1')
                                       iLastShape = 1;
                                   else
                                       iLastShape = 2;
                                   strUniExt.deleteCharAt(strUniExt.length() - 1);
                                   strUniExt.append(UyLettersUnicodeTab[33][iLastShape]);
                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[33][5];
                                   iLastCharIndex = 33;
                               }
                               else
                               {
                                   strUniExt.append(UyLettersUnicodeTab[i][iLastShape]);
                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[i][5];
                                   iLastCharIndex = i;
                               }
                               break;
                           }
                           //��������һ���ַ����ǰ����ַ��ǲ�lʽ�ַ�����Ҫ����ĸ�Ķ�b��ʽ
                           else if (k == chars.length - 1 && chLastLink == '0')
                           {
                               iLastShape = 4;
                               if (i == 0 && iLastCharIndex == 20)
                               {
                                   if (chSecondLastLink == '1')
                                       iLastShape = 1;
                                   else
                                       iLastShape = 2;

                                   strUniExt.deleteCharAt(strUniExt.length() - 1);
                                   strUniExt.append(UyLettersUnicodeTab[33][iLastShape]);
                                   chLastLink = UyLettersUnicodeTab[33][5];
                                   iLastCharIndex = 33;
                               }
                               else
                               {
                                   strUniExt.append(UyLettersUnicodeTab[i][iLastShape]);
                                   chLastLink = UyLettersUnicodeTab[i][5];
                                   iLastCharIndex = i;
                               }
                               break;
                           }
                           else if (chLastLink == '1')
                           {
                               iLastShape = 3;
                               if (i == 0 && iLastCharIndex == 20)
                               {
                                   if (chSecondLastLink == '1')
                                       iLastShape = 1;
                                   else
                                       iLastShape = 2;
                                   strUniExt.deleteCharAt(strUniExt.length() - 1);
                                   strUniExt.append(UyLettersUnicodeTab[33][iLastShape]);
                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[33][5];
                                   iLastCharIndex = 33;
                               }
                               else
                               {
                                   strUniExt.append(UyLettersUnicodeTab[i][iLastShape]);
                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[i][5];
                                   iLastCharIndex = i;
                               }
                               break;
                           }
                           else if (chLastLink == '0')
                           {
                               iLastShape = 2;
                               //���� l  �� f
                               if (i == 0 && iLastCharIndex == 20)
                               {
                                   if (chSecondLastLink == '1')
                                       iLastShape = 1;
                                   else
                                       iLastShape = 2;

                                   strUniExt.deleteCharAt(strUniExt.length() - 1);
                                   strUniExt.append(UyLettersUnicodeTab[33][iLastShape]);
                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[33][5];
                                   iLastCharIndex = 33;
                               }
                               else
                               {
                                   strUniExt.append(UyLettersUnicodeTab[i][iLastShape]);

                                   //������һ����ĸ��l������
                                   if (k >= 1)
                                       chSecondLastLink = chLastLink;
                                   else
                                       chSecondLastLink = '\0';

                                   chLastLink = UyLettersUnicodeTab[i][5];
                                   iLastCharIndex = i;
                               }
                               break;
                           }
                       }//else
                   }//end if
               }//for i
               if (i >= 34)
                   strUniExt.append(chars[k]);
        	   }
        	   catch(Exception ex)
        	   {
        		   System.out.println("Eception is:"+k+ex.toString()+"Wordis:"+chars.toString());
        	   }
           }//for k
           
         
           
           return strUniExt.toString();
       }
}
