package org.unhack.bip38decrypt.services;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import net.bither.bitherj.crypto.ECKey;

import org.unhack.bip38decrypt.Utils;
import org.unhack.bip38decrypt.createactivity.AddressGenerator;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

import javax.annotation.ParametersAreNonnullByDefault;



public class createService extends IntentService {
    private String phrase = "1";
    private String vanity = null;
    private static ECKey createdKey;
    private static boolean isSet = false;
    private int number_of_wallets;
    public static Thread worker;
    public createService() {
        super("createService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            try {
                vanity = intent.getStringExtra("vanity");
            }
            catch (NullPointerException e){
                //Oooops string was empty
                vanity = null;
            }
            number_of_wallets = intent.getIntExtra("wallets",1);

            phrase = phrase + vanity;
            for (int i = 0; i< number_of_wallets; i++) {
                worker = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        generateAddress(phrase);
                    }
                });
                worker.start();
            }
        }
    }

    private static void generateAddress(final String targetPhrase) {
        final int cores = Runtime.getRuntime().availableProcessors();
        final ListeningExecutorService execService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(cores));
        final long timeStart = System.nanoTime();
        for (int i = 0; i < cores; i++) {
            Callable<ECKey> callable = new AddressGenerator(targetPhrase);
            ListenableFuture<ECKey> future = execService.submit(callable);
            Futures.addCallback(future, new FutureCallback<ECKey>() {
                @Override
                public void onSuccess(ECKey key) {
                    if (key.toAddress().startsWith(targetPhrase)) {
                        setCreatedKey(key);
                    }
                    execService.shutdownNow();
                }
                @Override
                @ParametersAreNonnullByDefault
                public void onFailure(Throwable thrown) {
                    Log.d("generator",thrown.getMessage());
                }
            });
        }
    }
    private static synchronized boolean lockKey(){
        boolean isLocked = isSet;
        isSet = true;
        return isLocked;
    }

    private static  void setCreatedKey(ECKey key){
        if (!lockKey()){
            createdKey = key;
            String lesText = "Address: " + key.toAddress() + "Private Key: " + Utils.encodePrivateKeyToWIF(key.getPrivKeyBytes());
            Log.d("GENERATE", lesText);
        }
    }
    public static Thread getworker(){
        return worker;
    }
}

@@ -898,43 +898,8 @@
 se;%0A
-    private int number_of_wallets;%0A
     
@@ -1315,173 +1315,47 @@
   %7D%0A
-            number_of_wallets = intent.getIntExtra(%22wallets%22,1);%0A%0A            phrase = phrase + vanity;%0A            for (int i = 0; i%3C number_of_wallets; i++) %7B%0A    
+%0A            phrase = phrase + vanity;%0A
     
@@ -1407,36 +1407,32 @@
                 
-    
 @Override%0A      
@@ -1417,36 +1417,32 @@
       @Override%0A
-    
                 
@@ -1481,20 +1481,16 @@
         
-    
 generate
@@ -1518,32 +1518,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -1532,34 +1532,28 @@
             
-  
 %7D);%0A
-    
             
@@ -1568,30 +1568,16 @@
 tart();%0A
-            %7D%0A
         

do not touch create service. do not ever touch what is workable