/**
 * Copyright (C) 2015-2017 Mailvelope GmbH
 * Licensed under the GNU Affero General Public License version 3
 */

import mvelo from '../lib/lib-mvelo';
import {SubController} from './sub.controller';
import * as uiLog from '../modules/uiLog';
import {verifyMessage} from '../modules/pgpModel';
import {getPreferredKeyringId} from '../modules/keyring';

export default class VerifyController extends SubController {
  constructor(port) {
    super(port);
    this.keyringId = getPreferredKeyringId();
    this.verifyPopup = null;
    // register event handlers
    this.on('decrypt-message-init', this.onVerifyInit);
    this.on('decrypt-dialog-cancel', this.onCancel);
    this.on('decrypt-inline-user-input', msg => uiLog.push(msg.source, msg.type));
    this.on('vframe-display-popup', this.onDisplayPopup);
    this.on('vframe-armored-message', this.onArmoredMessage);
  }

  onVerifyInit() {
    this.ports.vFrame.emit('armored-message');
  }

  async onDisplayPopup() {
    this.verifyPopup = await mvelo.windows.openPopup(`components/decrypt-message/decryptMessage.html?id=${this.id}&embedded=false`, {width: 742, height: 550});
    this.verifyPopup.addRemoveListener(() => {
      this.ports.vFrame.emit('remove-dialog');
      this.verifyPopup = null;
    });
  }

  async onArmoredMessage(msg) {
    try {
      const {data, signatures} = await verifyMessage({armored: msg.data, keyringId: this.keyringId});
      this.ports.dDialog.emit('verified-message', {
        message: data,
        signers: signatures
      });
    } catch (e) {
      this.ports.dDialog.emit('error-message', {error: e.message});
    }
  }

  onCancel() {
    if (this.ports.vFrame) {
      this.ports.vFrame.emit('remove-dialog');
    }
    this.closePopup();
  }

  closePopup() {
    if (this.verifyPopup) {
      this.verifyPopup.close();
      this.verifyPopup = null;
    }
  }
}

@@ -522,24 +522,49 @@
 pup = null;%0A
+    this.armored = null;%0A
     // regis
@@ -634,24 +634,107 @@
 erifyInit);%0A
+    this.on('decrypt-message', () =%3E this.onArmoredMessage(%7Bdata: this.armored%7D));%0A
     this.on(
@@ -1407,14 +1407,42 @@
 age(
-msg) %7B
+%7Bdata%7D) %7B%0A    this.armored = data;
 %0A   
@@ -1515,16 +1515,20 @@
 ed: 
-msg.data
+this.armored
 , ke

Fix verify message show error on read message button