import React from 'react';
import * as Api from './../services/api';
let optoutUrl;
let showOptoutCount = true;
let responseState = "REQUEST";

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}
var charset7bit = ['@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', "\n", 'Ø', 'ø', "\r", 'Å', 'å', 'Δ', '_', 'Φ', 'Γ', 'Λ', 'Ω', 'Π', 'Ψ', 'Σ', 'Θ', 'Ξ', 'Æ', 'æ', 'ß', 'É', ' ', '!', '"', '#', '¤', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '¡', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§', '¿', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ', 'ü', 'à'];
var charset7bitext = ["\f", '^', '{', '}', '\\', '[', '~', ']', '|', '€'];
var udh_template = '<span className="udh"><span className="udh-total-length" title="UDH length">0x05</span><span className="udh-multipart-header" title="UDH header - Multipart SMS">0x00</span><span className="udh-multipart-length" title="Multipart SMS - UDH length">0x03</span><span className="udh-multipart-id" title="Multipart SMS - Unique ID">0x0a</span><span className="udh-total" title="Multipart SMS - Total number of parts"></span><span className="udh-part" title="Multipart SMS - Part order"></span></span>';

function charToSpan(chr, coding, isUnicodeEnabled) {
  var ret;
  var clss = '';
  if (coding == '7bit') {
    clss = 'c7b';
    if (charset7bitext.indexOf(chr) >= 0) {
      //ret = <div className="c7eb">ESC</div>;
    }
  }
  let isUnicode = false;
  if (coding == '16bit') {
    clss = 'c16b';
    if (charset7bit.indexOf(chr) == -1 && charset7bitext.indexOf(chr) == -1 && !isUnicodeEnabled) {
      isUnicode = true;
    }
  }
  return  <span className={isUnicode ? 'unicode-disabled' : ''}>{isUnicode ? `  \u2588  ` : chr}</span>
}

export function checkUnicode(content) {
  var chars_arr = content.split("");
  for (let i = 0; i < chars_arr.length; i++) {
    let chr = chars_arr[i];
    if (charset7bit.indexOf(chr) == -1 && charset7bitext.indexOf(chr) == -1) {
      return true;
    }
  }
  return false;
}
export function updateCharCount(content = '', isUnicodeEnabled) {
  if (showOptoutCount && optoutUrl === undefined && responseState === 'REQUEST') {
    Api.getUnsubscribeUrl().then((res) => {
      optoutUrl = res.response.response;
      responseState = "SUCCESS";
    }).catch((err) => {
      showOptoutCount = false;
      responseState = "FAIL";
      optoutUrl = undefined;
    });
  }
  const optoutTag = /{{optout}}/g;
  let optoutTagLength = 0;
  const contentOriginal = content;
  const noOfOptout = (content.match(optoutTag) || []).length;
  if (noOfOptout !== 0){
    optoutTagLength = 14;
  }
  content = showOptoutCount ? content.replace(optoutTag, "") : content;
  var chars_arr = contentOriginal.split("");
  var chars_arr_optedOut = content.split("");
  var coding = '7bit';
  var parts = 1;
  var chars_used = 0;
  var chars_sms = 160;
  var partc = [];
  for (let i = 0; i < chars_arr_optedOut.length; i++) {
    if (charset7bit.indexOf(chars_arr_optedOut[i]) >= 0) {
      chars_used = chars_used + 1;
    } else if (charset7bitext.indexOf(chars_arr_optedOut[i]) >= 0) {
      chars_used = chars_used + 2;
    } else {
      coding = '16bit';
      chars_used = chars_arr_optedOut.length;
      break;
    }
  }
  chars_used = showOptoutCount && optoutUrl && optoutUrl.length ? (chars_used + ((optoutUrl && optoutUrl.length) || 0) * noOfOptout) : chars_used + optoutTagLength;
  if (coding == '7bit') {
    if (chars_used > 160) {
      var parts = Math.ceil(chars_used / 153);
      var part_chars_used = 7;
      chars_sms = 153;
      for (let i = 0; i < chars_arr.length; i++) {
        if (part_chars_used + 1 > 160 || (charset7bitext.indexOf(chars_arr[i]) >= 0 && part_chars_used + 2 > 160)) {
          part_chars_used = 7;
        }
        partc = [...partc, charToSpan(chars_arr[i], coding, isUnicodeEnabled)];
        part_chars_used = charset7bitext.indexOf(chars_arr[i]) == -1 ? part_chars_used + 1 : part_chars_used + 2;
      }
    } else {
      chars_sms = 160;
      for (let i = 0; i < chars_arr.length; i++) {
        partc = [...partc, charToSpan(chars_arr[i], coding, isUnicodeEnabled)];
      }
    }
  } else {
    if (chars_used > 70) {
      parts = Math.ceil(chars_used / 67);
      var part_chars_used = 3;
      chars_sms = 67;
      for (let i = 0; i < chars_arr.length; i++) {
        if (part_chars_used + 1 > 70) {
          part_chars_used = 3;
        }
        partc = [...partc, charToSpan(chars_arr[i], coding, isUnicodeEnabled)];
        part_chars_used = part_chars_used + 1;
      }
    } else {
      chars_sms = 70;
      for (let i = 0; i < chars_arr.length; i++) {
        partc = [...partc, charToSpan(chars_arr[i], coding, isUnicodeEnabled)];
      }
    }
  }
  return {
    unicode: (coding === '7bit' ? false : true),
    parts,
    chars_sms,
    chars_used,
    char_list: partc,
    optouturlLength: (optoutUrl && optoutUrl.length) || 0,
    optoutUrlPresent: noOfOptout > 0,
    responseState,
  };
}
