"use strict";
const ipcRenderer = require('electron').ipcRenderer;
const webFrame = require('web-frame');
const menu = require('./menu.js');

const lock = (object, key, value) => Object.defineProperty(object, key, {
  get: () => value,
  set: () => {
  }
});

webFrame.setZoomLevelLimits(1, 1);

//lock(window, 'console', window.console);

let angular = window.angular = {};
let angularBootstrapReal;
Object.defineProperty(angular, 'bootstrap', {
  get: () => angularBootstrapReal ? function (element, moduleNames) {
    const moduleName = 'webwxApp';
    if (moduleNames.indexOf(moduleName) >= 0) {
      let constants;
      angular.injector(['ng', 'Services']).invoke(['confFactory', (confFactory) => (constants = confFactory)]);
      angular.module(moduleName).config([
        '$httpProvider',
        ($httpProvider) => {
          $httpProvider.defaults.transformResponse.push((value) => {
            if (typeof value === 'object' && value !== null && value.AddMsgList instanceof Array) {
              value.AddMsgList.forEach((msg) => {
                switch (msg.MsgType) {
                  case constants.MSGTYPE_EMOTICON:
                    const rec = msg.Content.match(/&lt;msg&gt;&lt;emoji.+cdnurl\s*=\s*"(.+?)".+thumburl\s*=\s*"(.*?)"/);
                    if (rec !== null) {
                      let actualContent = msg.Content;
                      lock(msg, 'MMActualContent', actualContent);
                      lock(msg, 'MMDigest', '[Emoticon resolved by Electronic WeChat]');
                      lock(msg, 'MsgType', constants.MSGTYPE_EMOTICON);
                    }
                    break;
                  case constants.MSGTYPE_RECALLED:
                    lock(msg, 'MsgType', constants.MSGTYPE_SYS);
                    lock(msg, 'MMActualContent', '阻止了一次撤回');
                    lock(msg, 'MMDigest', '阻止了一次撤回');
                    break;
                }
              });
            }
            return value;
          });
        }
      ]);
    }
    return angularBootstrapReal.apply(angular, arguments);
  } : angularBootstrapReal,
  set: (real) => (angularBootstrapReal = real)
});

window.injectBundle = {};
injectBundle.getBadgeJS = () => {
  $(".chat_list.scroll-content").bind('DOMSubtreeModified', () => {
    var count = 0;
    $(".icon.web_wechat_reddot_middle").each(function () {
      count += parseInt(this.textContent);
    });
    if (count > 0) {
      ipcRenderer.send("badge-changed", count.toString());
    } else {
      ipcRenderer.send("badge-changed", "");
    }
  })
};

injectBundle.getProfileNameJS = () => {
  let updateName = ()=> {
    let name = $('.display_name').text();
    ipcRenderer.send("profile-name-changed", name);
  };
  $('.display_name').ready(updateName).change(updateName);
};

menu.create();

@@ -1135,293 +1135,8 @@
 ON:%0A
-                    const rec = msg.Content.match(/&lt;msg&gt;&lt;emoji.+cdnurl%5Cs*=%5Cs*%22(.+?)%22.+thumburl%5Cs*=%5Cs*%22(.*?)%22/);%0A                    if (rec !== null) %7B%0A                      let actualContent = msg.Content;%0A                      lock(msg, 'MMActualContent', actualContent);%0A  
     
@@ -1187,45 +1187,13 @@
 icon
- resolved by Electronic WeChat
 %5D');%0A
-  
     
@@ -1262,30 +1262,8 @@
 N);%0A
-                    %7D%0A
     

Fix emoticon