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

import mvelo from "../mvelo";

export default class EncryptedFormContainer {
  constructor(selector, html, signature) {
    this.baseValidate(selector, html, signature);
    this.selector = selector;
    this.id = mvelo.util.getHash();
    this.name = `encryptedFormCont-${this.id}`;
    this.port = mvelo.runtime.connect({name: this.name});
    this.registerEventListener();
    this.parent = null;
    this.signature = signature;
    this.container = null;
    this.html = html;
    this.done = null;
  }

  create(done) {
    this.done = done;
    this.parent = document.querySelector(this.selector);
    this.container = document.createElement('iframe');
    const url = mvelo.runtime.getURL(`components/encrypted-form/encryptedForm.html?id=${this.id}`);
    this.container.setAttribute('id', this.name);
    this.container.setAttribute('src', url);
    this.container.setAttribute('frameBorder', 0);
    this.container.setAttribute('scrolling', 'yes');
    this.container.style.width = '100%';
    this.container.style.height = '150px';
    this.parent.appendChild(this.container);
  }

  processFormDefinition() {
    this.port.postMessage({
      event: 'encrypted-form-definition',
      sender: this.name,
      html: this.html,
      signature: this.signature
    });
  }

  registerEventListener() {
    this.port.onMessage.addListener(msg => {
      switch (msg.event) {
        case 'encrypted-form-ready':
          this.processFormDefinition();
          break;
        case 'encrypted-form-data':
          this.done(null, msg.armoredData);
          break;
        case 'encrypted-form-resize': {
          const offset = 16;
          const newHeight = msg.height + offset;
          this.container.style.height = `${newHeight}px`;
          break;
        }
        case 'destroy':
          this.parent.removeChild(this.container);
          this.port.disconnect();
          this.done(null, this.id);
          break;
        case 'error-message':
          if (this.container) {
            this.parent.removeChild(this.container);
            this.port.disconnect();
          }
          this.done(msg.error);
          break;
        default:
          console.log('unknown event', msg);
      }
    });
  }

  baseValidate(selector, html, signature) {
    if (!selector) {
      throw new mvelo.Error('The pgp encrypted form selector cannot be empty.');
    }
    if (!html) {
      throw new mvelo.Error('The pgp encrypted form html cannot be empty.');
    }
    if (!signature) {
      throw new mvelo.Error('The pgp encrypted form signature cannot be empty.');
    }
    return true;
  }
}

@@ -412,23 +412,28 @@
 = mvelo.
-runtime
+EventHandler
 .connect
@@ -437,25 +437,23 @@
 ect(
-%7B
+this.
 name
-:
+,
  this
-.name%7D
 );%0A 
@@ -1247,35 +1247,13 @@
 ort.
-postMessage(%7B%0A      event: 
+emit(
 'enc
@@ -1276,16 +1276,18 @@
 nition',
+ %7B
 %0A      s
@@ -1378,343 +1378,35 @@
 %0A%0A  
-registerEventListener() %7B%0A    this.port.onMessage.addListener(msg =%3E %7B%0A      switch (msg.event) %7B%0A        case 'encrypted-form-ready':%0A          this.processFormDefinition();%0A          break;%0A        case 'encrypted-form-data':%0A          this.done(null, msg.armoredData);%0A          break;%0A        case 'encrypted-form-resize': %7B%0A      
+onResizeIframe(%7Bheight%7D) %7B%0A
     
@@ -1424,30 +1424,24 @@
 t = 16;%0A    
-   
-   
 const newHei
@@ -1446,20 +1446,16 @@
 eight = 
-msg.
 height +
@@ -1463,22 +1463,16 @@
 offset;%0A
-      
     this
@@ -1521,63 +1521,27 @@
 ;%0A  
-        break;%0A        %7D%0A        case 'destroy':%0A      
+%7D%0A%0A  onDestroy() %7B%0A
     
@@ -1577,30 +1577,24 @@
 container);%0A
-      
     this.por
@@ -1605,30 +1605,24 @@
 sconnect();%0A
-      
     this.don
@@ -1645,59 +1645,32 @@
 ;%0A  
-        break;%0A        case 'error-message':%0A      
+%7D%0A%0A  onError(%7Berror%7D) %7B%0A
     
@@ -1691,22 +1691,16 @@
 iner) %7B%0A
-      
       th
@@ -1740,30 +1740,24 @@
 ner);%0A      
-     
- 
 this.port.di
@@ -1776,22 +1776,10 @@
     
-      %7D%0A      
+%7D%0A
     
@@ -1792,12 +1792,8 @@
 one(
-msg.
 erro
@@ -1802,98 +1802,354 @@
 ;%0A  
-        break;%0A        default:%0A          console.log('unknown event', msg);%0A      %7D%0A    %7D
+%7D%0A%0A  registerEventListener() %7B%0A    this.port.on('encrypted-form-ready', this.processFormDefinition);%0A    this.port.on('encrypted-form-data', (%7BarmoredData%7D) =%3E this.done(null, armoredData));%0A    this.port.on('encrypted-form-resize', this.onResizeIframe);%0A    this.port.on('destroy', this.onDestroy);%0A    this.port.on('error-message', this.onError
 );%0A 

Switch to mvelo.EventHandler