package org.unhack.bip38decrypt;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import net.bither.bitherj.crypto.DumpedPrivateKey;
import net.bither.bitherj.crypto.ECKey;
import net.bither.bitherj.crypto.SecureCharSequence;
import net.bither.bitherj.crypto.bip38.Bip38;
import net.bither.bitherj.exception.AddressFormatException;


public class bip38service extends IntentService {
    private String res,wallet,address;
    public static  boolean IAM = false;
    public bip38service() {
        super("bip38service");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        IAM = true;
           wallet = intent.getStringExtra("wallet");
        Log.d("Service W",wallet);
        final String password = intent.getStringExtra("password");
        Log.d("Service P",password);
        final String password2 = intent.getStringExtra("password2");
        final boolean needReEncrypt = intent.getBooleanExtra("reencrypt",false);
        SecureCharSequence result = null;
        try {
            result = Bip38.decrypt(wallet,password);
            res = result.toString();
            DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(res);
            ECKey ecKey = dumpedPrivateKey.getKey();
            address = ecKey.toAddress();
            Log.d("Service addr",address);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (AddressFormatException e) {
            e.printStackTrace();
        } catch (NullPointerException e){

        }
        if (result != null) {
            res = result.toString();
            Log.d("Service res",res);
        }
        if (needReEncrypt){
            try {
                res = Bip38.encryptNoEcMultiply(password2,res);
                Log.d("Service re res",res);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (AddressFormatException e) {
                e.printStackTrace();
            }
        }


        if (res != null) {
            Log.d("Service ", "Before Handler");

            Intent resIntent = new Intent(MainActivity.INTENT_FILTER);
            resIntent.putExtra("result",res);
            resIntent.putExtra("address",address);
            sendBroadcast(resIntent);
            Log.d("Service ", "After Handler");


        }
        else {
        }
        IAM = false;
    }
}

@@ -1042,24 +1042,26 @@
 false);%0A
+%0A%0A
         
 SecureCh
@@ -1056,42 +1056,145 @@
     
-SecureCharSequence result = null;%0A
+try %7B%0A%0A            Thread worker = new Thread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A            
     
@@ -1215,22 +1215,31 @@
         
+  
+          
 res
-ult
  = Bip38
@@ -1255,32 +1255,43 @@
 wallet,password)
+.toString()
 ;%0A            re
@@ -1292,33 +1292,8 @@
     
-res = result.toString();%0A
     
@@ -1375,16 +1375,28 @@
         
+       
+     
 ECKey ec
@@ -1440,16 +1440,28 @@
         
+            
 address 
@@ -1497,313 +1497,8 @@
     
-Log.d(%22Service addr%22,address);%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D catch (AddressFormatException e) %7B%0A            e.printStackTrace();%0A        %7D catch (NullPointerException e)%7B%0A%0A        %7D%0A        if (result != null) %7B%0A            res = result.toString();%0A
     
@@ -1520,24 +1520,29 @@
 Service 
-res%22,
+addr%22,add
 res
+s
 );%0A     
@@ -1544,26 +1544,25 @@
         
-%7D%0A
+ 
         
 if (need
@@ -1549,24 +1549,31 @@
             
+       
 if (needReEn
@@ -1584,38 +1584,32 @@
 t)%7B%0A            
-try %7B%0A
                 
@@ -1676,37 +1676,26 @@
     
+ 
-Log.d(%22Service re res%22,res);%0A
+       %7D%0A        
     
@@ -1745,32 +1745,40 @@
                 
+        
 e.printStackTrac
@@ -1786,32 +1786,40 @@
 ();%0A            
+       
+ 
 %7D catch (Address
@@ -1847,32 +1847,40 @@
                 
+      
+  
 e.printStackTrac
@@ -1888,34 +1888,32 @@
 ();%0A            
-%7D%0A
         %7D%0A%0A%0A    
@@ -1907,17 +1907,28 @@
      %7D%0A%0A
-%0A
+            
         
@@ -1950,32 +1950,44 @@
 ) %7B%0A            
+   
+         
 Log.d(%22Service %22
@@ -2007,17 +2007,28 @@
 dler%22);%0A
-%0A
+            
         
@@ -2094,32 +2094,44 @@
 R);%0A            
+      
+      
 resIntent.putExt
@@ -2152,32 +2152,44 @@
 s);%0A            
+        
+    
 resIntent.putExt
@@ -2223,16 +2223,28 @@
         
+         
+   
 sendBroa
@@ -2265,32 +2265,44 @@
 t);%0A            
+           
+ 
 Log.d(%22Service %22
@@ -2321,26 +2321,81 @@
 dler%22);%0A
-%0A%0A
+                    %7D%0A                    IAM = false;%0A  
         
 %7D%0A      
@@ -2378,32 +2378,38 @@
 alse;%0A          
+      
 %7D%0A        else %7B
@@ -2406,45 +2406,172 @@
     
-else %7B%0A        %7D%0A        IAM = false;
+    %7D);%0A            worker.setPriority(Thread.MAX_PRIORITY);%0A            worker.start();%0A%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

move all expensive computations to thread