/*
 * Copyright 2014 http://Bither.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.bither.bitherj.factory;

import net.bither.bitherj.AbstractApp;
import net.bither.bitherj.core.HDMKeychain;
import net.bither.bitherj.crypto.EncryptedData;
import net.bither.bitherj.crypto.SecureCharSequence;
import net.bither.bitherj.crypto.mnemonic.MnemonicCode;
import net.bither.bitherj.qrcode.QRCodeUtil;
import net.bither.bitherj.utils.Utils;

import java.util.List;

public abstract class ImportHDSeed {
    public enum ImportHDSeedType {
        HDMColdSeedQRCode, HDMColdPhrase
    }

    public static final int PASSWORD_IS_DIFFEREND_LOCAL = 0;
    public static final int NOT_HDM_COLD_SEED = 1;
    public static final int PASSWORD_WRONG = 3;
    public static final int IMPORT_FAILED = 4;

    private String content;
    private List<String> worlds;
    protected SecureCharSequence password;

    private ImportHDSeedType importPrivateKeyType;


    public ImportHDSeed(ImportHDSeedType importHDSeedType
            , String content, List<String> worlds, SecureCharSequence password) {
        this.content = content;
        this.password = password;
        this.importPrivateKeyType = importHDSeedType;
        this.worlds = worlds;
    }

    public HDMKeychain importHDSeed() {
        switch (this.importPrivateKeyType) {
            case HDMColdSeedQRCode:
                if (content.indexOf(QRCodeUtil.HDM_QR_CODE_FLAG) == 0) {
                    String keyString = content.substring(1);
                    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keyString);
                    String encreyptString = Utils.joinString(new String[]{passwordSeeds[0], passwordSeeds[1], passwordSeeds[2]}, QRCodeUtil.QR_CODE_SPLIT);
                    EncryptedData encryptedData = new EncryptedData(encreyptString);
                    byte[] result = null;
                    try {
                        result = encryptedData.decrypt(this.password);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;

                    }
                    if (result == null) {
                        if (AbstractApp.bitherjSetting.getPasswordSeed() == null) {
                            importError(ImportPrivateKey.PASSWORD_WRONG);
                        } else {
                            importError(ImportPrivateKey.PASSWORD_IS_DIFFEREND_LOCAL);
                        }
                        return null;
                    } else {
                        try {
                            return new HDMKeychain(new EncryptedData(encreyptString)
                                    , password, null);

                        } catch (Exception e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                } else {
                    importError(NOT_HDM_COLD_SEED);
                    return null;
                }

            case HDMColdPhrase:
                try {
                    byte[] mnemonicCodeSeed = MnemonicCode.instance().toEntropy(worlds);
                    HDMKeychain hdmKeychain = new HDMKeychain(mnemonicCodeSeed, password);
                    return hdmKeychain;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;

        }
        return null;

    }

    public abstract void importError(int errorCode);

}

@@ -762,16 +762,63 @@
 edData;%0A
+import net.bither.bitherj.crypto.PasswordSeed;%0A
 import n
@@ -2333,70 +2333,234 @@
     
-EncryptedData encryptedData = new EncryptedData(encreyptString
+PasswordSeed passwordSeed = AbstractApp.bitherjSetting.getPasswordSeed();%0A                    if (passwordSeed != null && !passwordSeed.checkPassword(password)) %7B%0A                        importError(PASSWORD_IS_DIFFEREND_LOCAL
 );%0A 
@@ -2582,23 +2582,18 @@
     
-byte%5B%5D result =
+    return
  nul
@@ -2594,32 +2594,47 @@
 rn null;%0A       
+    
+         %7D%0A
              try
@@ -2622,32 +2622,39 @@
  %7D%0A             
+       
 try %7B%0A          
@@ -2673,16 +2673,34 @@
   re
-sult = e
+turn new HDMKeychain(new E
 ncry
@@ -2711,30 +2711,67 @@
 Data
-.de
+(en
 cr
+e
 ypt
-(this.
+String)%0A                                , 
 password
 );%0A 
@@ -2758,35 +2758,42 @@
       , password
+, null
 );%0A
+%0A
                 
@@ -2894,33 +2894,32 @@
    return null;%0A
-%0A
                 
@@ -2928,776 +2928,8 @@
   %7D%0A
-                    if (result == null) %7B%0A                        if (AbstractApp.bitherjSetting.getPasswordSeed() == null) %7B%0A                            importError(ImportPrivateKey.PASSWORD_WRONG);%0A                        %7D else %7B%0A                            importError(ImportPrivateKey.PASSWORD_IS_DIFFEREND_LOCAL);%0A                        %7D%0A                        return null;%0A                    %7D else %7B%0A                        try %7B%0A                            return new HDMKeychain(new EncryptedData(encreyptString)%0A                                    , password, null);%0A%0A                        %7D catch (Exception e) %7B%0A                            e.printStackTrace();%0A                            return null;%0A                        %7D%0A                    %7D
 %0A   

improve importHDSeed