/* eslint-disable */
import Jed from './jed';

const l10n = {};
const i18n = new Jed(l10n);
let settings;

function smsCounter(options, chars) {
  settings = Object.assign({
    smsCount: 160,
  }, options);
  return updateSmsData(chars); //this.each(function() {
  //   if (!skipObj) {
  //     this.keyup(updateSmsData);
  //   } else {
  //     updateSmsData();
  //   }
  // });
}

let updateSmsData = (chars) => {
  let skipObj = false;
  let msgContent;
  if (chars !== undefined && chars !== "" && chars !== false && chars !== true) {
    skipObj = true;
  }
  if (skipObj) {
    msgContent = chars;
  } else {
    // msgContent = $(this).val();
  }

  const charset7bit = {
    '@': 1,
    '$': 1,
    "\n": 1,
    "\r": 1,
    '_': 1,
    ' ': 1,
    '!': 1,
    '"': 1,
    '#': 1,
    '%': 1,
    '&': 1,
    "'": 1,
    '(': 1,
    ')': 1,
    '*': 1,
    '+': 1,
    ',': 1,
    '-': 1,
    '.': 1,
    '/': 1,
    '0': 1,
    '1': 1,
    '2': 1,
    '3': 1,
    '4': 1,
    '5': 1,
    '6': 1,
    '7': 1,
    '8': 1,
    '9': 1,
    ':': 1,
    ';': 1,
    '<': 1,
    '=': 1,
    '>': 1,
    '?': 1,
    '¡': 1,
    'A': 1,
    'B': 1,
    'C': 1,
    'D': 1,
    'E': 1,
    'F': 1,
    'G': 1,
    'H': 1,
    'I': 1,
    'J': 1,
    'K': 1,
    'L': 1,
    'M': 1,
    'N': 1,
    'O': 1,
    'P': 1,
    'Q': 1,
    'R': 1,
    'S': 1,
    'T': 1,
    'U': 1,
    'V': 1,
    'W': 1,
    'X': 1,
    'Y': 1,
    'Z': 1,
    'a': 1,
    'b': 1,
    'c': 1,
    'd': 1,
    'e': 1,
    'f': 1,
    'g': 1,
    'h': 1,
    'i': 1,
    'j': 1,
    'k': 1,
    'l': 1,
    'm': 1,
    'n': 1,
    'o': 1,
    'p': 1,
    'q': 1,
    'r': 1,
    's': 1,
    't': 1,
    'u': 1,
    'v': 1,
    'w': 1,
    'x': 1,
    'y': 1,
    'z': 1,
    "\f": 2,
    '^': 2,
    '{': 2,
    '}': 2,
    '\\': 2,
    '[': 2,
    '~': 2,
    ']': 2,
    '|': 2,
    'è': 1,
    'é': 1,
    'ù': 1,
    'ì': 1,
    'ò': 1,
    'Ç': 1,
    'Ø': 1,
    'ø': 1,
    'Å': 1,
    'å': 1,
    'Δ': 1,
    'Φ': 1,
    'Γ': 1,
    'Λ': 1,
    'Ω': 1,
    'Π': 1,
    'Ψ': 1,
    'Σ': 1,
    'Θ': 1,
    'Ξ': 1,
    'Æ': 1,
    'æ': 1,
    'ß': 1,
    'É': 1,
    'Ä': 1,
    'Ö': 1,
    'Ñ': 1,
    'Ü': 1,
    'ä': 1,
    'ö': 1,
    'ñ': 1,
    'ü': 1,
    'à': 1,
    '£': 1,
    '¥': 1,
    '¤': 1,
    '§': 1,
    '¿': 1,
    '€': 2,
  };

  msgContent = msgContent.replace(/\u00a0/g, " ").replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/\\n\\n/g, "\\n").replace(/\\n/g, " ");
  msgContent = msgContent.replace(/\r/g, " ").replace(/\f/g, "  ");

  const matches = msgContent.split("");
  
  let use7bit = true;
  let length7bit = 0;
  let length16bit = 0;
  let parts = {
    sms_count: 1,
    chars_left: 160,
  };

  matches.forEach( (char) => {
    if (use7bit && charset7bit[char] === undefined) {
      use7bit = false;
    }

    if (use7bit) {
      length7bit += charset7bit[char];
    }

    length16bit += 1;
  });

  if (use7bit) {
    parts = smsCount(length7bit, false);
  } else {
    parts = smsCount(length16bit, true);
  }

  if (typeof settings.getCount === 'function') {
    settings.getCount.call(this, parts);
  }
};

let smsCount = (chars, enc16) => {
  let charactersLeft = 0;
  let smsCharCounter = 0;

  if (enc16 === false && chars <= 160) {
    charactersLeft = 160 - chars;
    return {
      sms_count: 1,
      chars_left: charactersLeft,
      chars,
      charset: false,
    };
  }

  if (enc16 === true && chars <= 70) {
    charactersLeft = 70 - chars;
    return {
      sms_count: 1,
      chars_left: charactersLeft,
      chars,
      charset: "Unicode charset",
    };
  }

  if (enc16 === false) {
    smsCharCounter = Math.ceil(chars / 153);
    charactersLeft = (smsCount * 153) - chars;
    return {
      sms_count: smsCharCounter,
      chars_left: charactersLeft,
      chars,
      charset: false,
    };
  }

  smsCharCounter = Math.ceil(chars / 67);
  charactersLeft = (smsCharCounter * 67) - chars;
  return {
    sms_count: smsCharCounter,
    chars_left: charactersLeft,
    chars,
    charset: "Unicode charset",
  };
};

function _camp1(s) {
  try {
    return i18n.dgettext("camp1", s) !== '' ? this.i18n.dgettext("camp1", s) : s;
  } catch (err) {
    
    return s;
  }
}

/**
 * returns no of char an SMS contains in
 * messaging of campaign based on ascii and
 * non-ascii characters.
 * @param message
 * @returns
 */
function smsCharCount(message) {
  // const that = this;
  let msgcount = 1;
  let countLbl = 0;
  const props = {
    chars_left: 160,
  };

  smsCounter({
    getCount: (parts) => {
      props.chars_left = parts.chars_left;
      props.total_chars = parts.chars;
      props.charset = parts.charset;
      msgcount = parts.sms_count;
      countLbl = parts.chars;
      if (parts.charset !== false) {
        countLbl = `${parts.chars} ${_camp1("Unicode")}`;
      }
    },
  }, message);
  return [countLbl, msgcount, props];
}

// function escapeRegex(text) {
//   return text.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&").replace(/\s/g, "\\s+").replace(/'|"/g, "(?:'|\")");
// }

export function checkUnicode(str, msg) {
  const non_unicode = {'@': 1, '$': 1, "\n":1, "\r":1, '_': 1, ' ': 1, '!': 1, '"': 1, '#': 1, '%': 1, '&': 1, "'":1, '(': 1, ')': 1, '*': 1, '+': 1, ',': 1, '-': 1, '.': 1, '/': 1, '0': 1, '1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1, '9': 1, ':': 1, ';': 1, '<': 1, '=': 1, '>': 1, '?': 1, '¡': 1, 'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E': 1, 'F': 1, 'G': 1, 'H': 1, 'I': 1, 'J': 1, 'K': 1, 'L': 1, 'M': 1, 'N': 1, 'O': 1, 'P': 1, 'Q': 1, 'R': 1, 'S': 1, 'T': 1, 'U': 1, 'V': 1, 'W': 1, 'X': 1, 'Y': 1, 'Z': 1, 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1, 'l': 1, 'm': 1, 'n': 1, 'o': 1, 'p': 1, 'q': 1, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 1, 'w': 1, 'x': 1, 'y': 1, 'z': 1, "\f": 2, '^': 2, '{': 2, '}': 2, '\\': 2, '[': 2, '~': 2,']':2,'|':2,'è':1,'é':1,'ù':1,'ì':1,'ò':1,'Ç':1,'Ø':1,'ø':1,'Å':1,'å':1,'Δ':1,'Φ':1,'Γ':1,'Λ':1,'Ω':1,'Π':1,'Ψ':1,'Σ':1,'Θ':1,'Ξ':1,'Æ':1,'æ':1,'ß':1,'É':1,'Ä':1,'Ö':1,'Ñ':1,'Ü':1,'¿':1,'ä':1,'ö':1,'ñ':1,'ü':1,'à':1,'£': 1,'¥': 1,'¤': 1,'§': 1,'¿': 1,'€':2};
  var isUnicode=0;
  msg = typeof msg !== 'undefined' ? msg : "This message contains Unicode Characters. Do you want to proceed?";

  str = str.replace(/\u00a0/g, " ").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/\\n\\n/g, "\\n").replace(/\\n/g, " ");
  str = str.replace(/\r/g," ").replace(/\f/g,"  ");
  var matches = str.split("");
  for (var i in matches) {
    const char = matches[i];
    if (!non_unicode[char]) {
      isUnicode = 1;
    }
  }
  // if (isUnicode === 1) {
  //   const r = confirm(msg);
  //   if (r === true) {
  //     return true;
  //   }
  //   return false;
  // }
  //
  return isUnicode === 1;
}

export function updateCharCount(boxVal) {
  // let tagHtml = "<span class='editor_tag' title='%desc%' contenteditable='false'>%name%</span>";
  // let tagText = "{{%name%}}";

  // let boxVal = html.replace(new RegExp(escapeRegex(tagHtml).replace("%name%", "(.*?)").replace(/%.*?%/g, "(?:.*?)"), "g"), tagText.replace(/%name%/g, "$1")).replace(/\<(p|div|br).*?\>/g, "\\n").replace(/\<.*?\>/g, "");
  // boxVal = "" + boxVal;

  // let unicodeFlag = 0;
  // let extraChars = 0;
  
  let msgCount = 0;
  const lenCount = smsCharCount(boxVal);
  const len = lenCount[0];
  msgCount = lenCount[1];
  const charLeft = lenCount[2].chars_left;
  const totalChars = lenCount[2].total_chars;
  const charset = lenCount[2].charset;
  if (!charset && totalChars > 1000) {
    
  } else if (charset && totalChars > 500) {
    
  }

  // return `${len} characters = ${msgCount} Messages*, Characters left: ${charLeft}`;
  return {
    totalLength: len,
    msgCount,
    charLeft,
  };
}
