import namehash from 'eth-ens-namehash';

/*global web3*/
let __embarkENS = {};

// registry interface for later
__embarkENS.registryInterface = [
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      }
    ],
    "name": "resolver",
    "outputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      }
    ],
    "name": "owner",
    "outputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "resolver",
        "type": "address"
      }
    ],
    "name": "setResolver",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "label",
        "type": "bytes32"
      },
      {
        "name": "owner",
        "type": "address"
      }
    ],
    "name": "setSubnodeOwner",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "owner",
        "type": "address"
      }
    ],
    "name": "setOwner",
    "outputs": [],
    "type": "function"
  }
];

__embarkENS.resolverInterface = [
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      }
    ],
    "name": "addr",
    "outputs": [
      {
        "name": "",
        "type": "address"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      }
    ],
    "name": "content",
    "outputs": [
      {
        "name": "",
        "type": "bytes32"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      }
    ],
    "name": "name",
    "outputs": [
      {
        "name": "",
        "type": "string"
      }
    ],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "kind",
        "type": "bytes32"
      }
    ],
    "name": "has",
    "outputs": [
      {
        "name": "",
        "type": "bool"
      }
    ],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "addr",
        "type": "address"
      }
    ],
    "name": "setAddr",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "hash",
        "type": "bytes32"
      }
    ],
    "name": "setContent",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "name",
        "type": "string"
      }
    ],
    "name": "setName",
    "outputs": [],
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [
      {
        "name": "node",
        "type": "bytes32"
      },
      {
        "name": "contentType",
        "type": "uint256"
      }
    ],
    "name": "ABI",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      },
      {
        "name": "",
        "type": "bytes"
      }
    ],
    "payable": false,
    "type": "function"
  }
];

__embarkENS.registryAddresses = {
  // Mainnet
  "1": "0x314159265dd8dbb310642f98f50c066173c1259b",
  // Ropsten
  "3": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
  // Rinkeby
  "4": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"
};

__embarkENS.setProvider = function () {
  // get network id and then assign ENS contract based on that 
  let registryAddresses = this.registryAddresses;
  this.ens = web3.eth.net.getId().then(id => {
    if (registryAddresses[id] !== undefined) {
      return new web3.eth.Contract(this.registryInterface, registryAddresses[id]);
    }
    // todo: deploy at this point
    return undefined;
  });  
};

__embarkENS.resolve = function(name) {
  const self = this;

  if (self.ens === undefined) return undefined;

  let node = namehash(name);
  
  self.ens.methods.resolver(node).call().then((resolverAddress) => {
    let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
    return resolverContract.methods.addr(node).call();
  }).then((addr) => {
    return addr;
  }).catch(err => err);
};

__embarkENS.lookup = function(address) {
  const self = this;

  if (self.ens === undefined) return undefined;

  if (address.startsWith("0x")) address = address.slice(2);

  let node = namehash(address.toLowerCase() + ".addr.reverse");

  self.ens.methods.resolver(node).call().then((resolverAddress) => {
    let resolverContract = new web3.eth.Contract(self.resolverInterface, resolverAddress);
    return resolverContract.methods.name(node).call();
  }).then((name) => {
    if (name === "" || name === undefined) throw Error("ENS name not found");
    return name;
  }).catch(err => err);
};

@@ -4031,16 +4031,37 @@
 on () %7B%0A
+  const self = this;%0A
   // get
@@ -4178,16 +4178,23 @@
 is.ens =
+ null%0A 
  web3.et
@@ -4273,22 +4273,26 @@
 %7B%0A      
-return
+self.ens =
  new web
@@ -4306,20 +4306,20 @@
 ontract(
-this
+self
 .registr
@@ -4559,16 +4559,21 @@
 namehash
+.hash
 (name);%0A
@@ -5051,16 +5051,21 @@
 namehash
+.hash
 (address

fix reference to ens object