// 名前空間
var CHAT_UTIL = {};

CHAT_UTIL.formatDate = function (date) {
  const REFERENCE = moment();
  const TODAY = REFERENCE.clone().startOf("day");
  let createdAt = moment(date);
  if (date.length == 14) {
    createdAt = moment(date, "YYYYMMDDhhmmss");
  } else {
    createdAt = moment(date);
  }

  // #36171
  const createdAtDay = createdAt.format("MMM Do");
  const createdAtTime = createdAt.format("HH:mm");
  if (createdAt.isSame(TODAY, "d")) {
    createdAt = createdAtTime;
  } else {
    createdAt = createdAtDay;
  }
  let rntDate = new Object();
  rntDate.createdAtDay = createdAtDay;
  rntDate.createdAtTime = createdAtTime;
  rntDate.createdAt = createdAt;
  return rntDate;
};

CHAT_UTIL.htmlDecode = function (input) {
  var e = document.createElement("textarea");
  e.innerHTML = input;
  // handle case of empty input
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
};

//generates a unique id, not obligator a UUID
CHAT_UTIL.generateUUID = function () {
  var d = new Date().getTime();
  var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
    /[xy]/g,
    function (c) {
      var r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
    }
  );
  return uuid;
};

CHAT_UTIL.systemDay = function (date) {
  let createdAt = moment(date);
  createdAt = moment(date, "YYYYMMDDhhmmss");
  const createdAtDow = createdAt.format("YYYY-MM-DD");

  let systemDay = new Object();
  systemDay.year = createdAt.format("YYYY");
  systemDay.month = createdAt.format("MM");
  systemDay.day = createdAt.format("DD");
  systemDay.dow = moment(createdAtDow).day();

  return systemDay;
};

CHAT_UTIL.findDow = function (data) {
  let dow = "";
  if (data == 0) {
    dow = getLocalizedString("sunday");
  } else if (data == 1) {
    dow = getLocalizedString("monday");
  } else if (data == 2) {
    dow = getLocalizedString("thusday");
  } else if (data == 3) {
    dow = getLocalizedString("wednesday");
  } else if (data == 4) {
    dow = getLocalizedString("thursday");
  } else if (data == 5) {
    dow = getLocalizedString("friday");
  } else if (data == 6) {
    dow = getLocalizedString("sunday");
  }
  return dow;
};

function msToTime(s) {
  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;

  return hrs + ":" + mins + ":" + secs;
}

function updateDuration() {
  var now = new Date();
  $(".collaboration_duration").each(function (index, item) {
    var insertDateString = $(item).data("insertdate");
    insertDateString = insertDateString.toString();
    var year = insertDateString.substring(0, 4);
    var month = insertDateString.substring(4, 6);
    var day = insertDateString.substring(6, 8);
    var hour = insertDateString.substring(8, 10);
    var min = insertDateString.substring(10, 12);
    var sec = insertDateString.substring(12, 14);
    var insertDate = new Date(year, month - 1, day, hour, min, sec);
    $(item).html(msToTime(now - insertDate));
  });
}

CHAT_UTIL.getCollaborationType = function (key) {
  switch (key) {
    case CollaborationTypeKey.AUDIO:
      return COLLABORATION_TYPE.AUDIO;
    case CollaborationTypeKey.CAMERA:
      return COLLABORATION_TYPE.CAMERA;
    case CollaborationTypeKey.VIDEO:
      return COLLABORATION_TYPE.VIDEO;
    case CollaborationTypeKey.DOCUMENT:
      return COLLABORATION_TYPE.DOCUMENT;
    default:
      return 0;
  }
};

CHAT_UTIL.getCoviewTypeFromCollaborationType = function (
  joinCollaborationType
) {
  switch (joinCollaborationType) {
    case COLLABORATION_TYPE.AUDIO:
      return CollaborationTypeKey.AUDIO;
    case COLLABORATION_TYPE.CAMERA:
      return CollaborationTypeKey.CAMERA;
    case COLLABORATION_TYPE.VIDEO:
      return CollaborationTypeKey.VIDEO;
    case COLLABORATION_TYPE.DOCUMENT:
      return CollaborationTypeKey.DOCUMENT;
    case COLLABORATION_TYPE.BOARD:
      return CollaborationTypeKey.BOARD;
    default:
      return 0;
  }
};
