/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package king.flow.action.business;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import king.flow.action.DefaultAction;
import king.flow.common.CommonConstants;
import king.flow.common.CommonUtil;
import king.flow.data.TLSResult;

/**
 *
 * @author LiuJin
 */
public class KeyboardEncryptAction extends DefaultAction<JPasswordField> {

    private final int moneyId;
    private final Integer trigger;

    public KeyboardEncryptAction(int moneyId, Integer trigger) {
        this.moneyId = moneyId;
        this.trigger = trigger;
    }

    @Override
    public void setupListener() {
        owner.addFocusListener(new FocusAdapterImpl());
    }

    private class FocusAdapterImpl extends FocusAdapter {

        @Override
        public void focusGained(FocusEvent e) {
            String bankNum = CommonUtil.retrieveCargo(CommonConstants.VALID_BANK_CARD);
            if (bankNum == null) {
                CommonUtil.showErrorMsg(owner.getTopLevelAncestor(),
                        CommonUtil.getResourceMsg("encryption.keyboard.type.precondition.prompt"));
                owner.setFocusable(false);
                owner.setFocusable(true);
                return;
            }
            if (CommonUtil.openEncryptedKeyboard() != CommonConstants.NORMAL) {
                CommonUtil.showErrorMsg(owner.getTopLevelAncestor(),
                        CommonUtil.getResourceMsg("encryption.keyboard.open.fail.prompt"));
                owner.setFocusable(false);
                owner.setFocusable(true);
                return;
            }
            new ReadEncryptionTask().execute();
        }

        @Override
        public void focusLost(FocusEvent e) {
            CommonUtil.closeEncryptedKeyboard();
        }

    }

    private class ReadEncryptionTask extends SwingWorker<String, Integer> {

        @Override
        protected String doInBackground() throws Exception {
            String encryption = CommonUtil.readEncryptedString(owner);
            if (encryption == null) {
                CommonUtil.showMsg(owner.getTopLevelAncestor(),
                        CommonUtil.getResourceMsg("encryption.keyboard.type.timeout.prompt"));
                owner.setText("");
                owner.setFocusable(false);
                owner.setFocusable(true);
                return null;
            }
            CommonUtil.putCargo(Integer.toString(id), encryption);

            String calculatedMAC = CommonUtil.calculateMAC(
                    CommonConstants.UNION_PAY_TRANSACTION,
                    CommonUtil.retrieveCargo(TLSResult.UNIONPAY_CARD_INFO),
                    encryption,
                    getBlock(moneyId, JTextField.class).getText());
            if (calculatedMAC != null) {
                CommonUtil.putCargo(TLSResult.UNIONPAY_MAC_INFO, calculatedMAC);
            }
            
            return encryption;
        }

        @Override
        protected void done() {
            try {
                String credentials = get();
                if (credentials != null && trigger != null) {
                    JButton next = getBlock(trigger, JButton.class);
                    next.doClick();
                }
            } catch (InterruptedException | ExecutionException ex) {
                Logger.getLogger(KeyboardEncryptAction.class.getName()).log(Level.WARNING,
                        "encryption of keyboard is broken due to : \n{0}", ex);
            }
        }
    }
}

@@ -639,16 +639,144 @@
 onUtil;%0A
+import static king.flow.common.CommonUtil.retrieveBankCardAffiliation;%0Aimport static king.flow.common.CommonUtil.retrieveCargo;%0A
 import k
@@ -2969,16 +2969,243 @@
 tion);%0A%0A
+            String cardNum = retrieveCargo(CommonConstants.VALID_BANK_CARD);%0A            String cardType = retrieveBankCardAffiliation(cardNum);%0A            if (cardType.equals(CommonConstants.CARD_AFFILIATION_EXTERNAL)) %7B%0A    
         
@@ -3272,24 +3272,28 @@
             
+ 
+   
 CommonConsta
@@ -3319,16 +3319,20 @@
 ACTION,%0A
+    
         
@@ -3419,16 +3419,20 @@
         
+ 
+   
 encrypti
@@ -3435,16 +3435,20 @@
 yption,%0A
+    
         
@@ -3515,24 +3515,28 @@
             
+  
+  
 if (calculat
@@ -3544,32 +3544,36 @@
 dMAC != null) %7B%0A
+    
                 
@@ -3637,16 +3637,34 @@
 edMAC);%0A
+                %7D%0A
         
@@ -3669,28 +3669,16 @@
       %7D%0A
-            
 %0A       

update EncryptionKeyboardAction