var CollaborationUI = {};

document.addEventListener("DOMContentLoaded", function () {
  console.log("peacekim:: DOMContentLoaded on collaboration js");
  CollaborationUI.bindDisableScroll();

  // メニューオーバーレイ表示
  CollaborationUI.bindMenuButton();

  // ユーザーリストオーバーレイ表示
  CollaborationUI.bindDisplayUsersButton();

  // モーダルonモーダル（前のモーダルを非表示に）
  // ホスト変更ボタン押下イベント
  CollaborationUI.bindChangeHostButton();

  // 閉じるイベント
  CollaborationUI.bindCloseButton();

  // ユーザー追加イベント
  CollaborationUI.bindInviteButton();

  CollaborationUI.initialBindAddUserButton();
});

/*********************************
 *  Buttons binding events
 ********************************/
CollaborationUI.bindMenuButton = function () {
  $(".menu_btn").click(function () {
    $(this).toggleClass("hide");
    $("#overlay_menu .item").toggleClass("hide");
  });
};

CollaborationUI.bindDisplayUsersButton = function () {
  $(".user_btn").click(function () {
    var w = $(this).width();

    $(this).toggleClass("hide");
    $("#overlay_user_list.overlay")
      .toggleClass("slidein")
      .css({ transform: "translateX(" & -w & ")" });

    if ($(this).hasClass("hide")) {
      CollaborationUI.bindEnableScroll();
    } else {
      scrollTo(0, 0);
      CollaborationUI.bindDisableScroll();
    }
  });
};

CollaborationUI.userListSlideOut = function () {
  const w = $(".user_btn").width();
  $(".user_btn").removeClass("hide");
  $("#overlay_user_list.overlay")
    .removeClass("slidein")
    .css({ transform: "translateX(" & -w & ")" });
  scrollTo(0, 0);
  CollaborationUI.bindDisableScroll();
};

CollaborationUI.bindChangeHostButton = function () {
  $(".ch_host_btn").click(function () {
    const target = $(this).val();
    const beforeModal = $(target);
    const afterModal = $("#changeHostModal");
    /* モーダルの切り替え */
    beforeModal.modal("hide");
    afterModal.modal("show");
  });
};

CollaborationUI.bindCloseButton = function () {
  $(".close_btn").click(function () {
    $("#overlay_add_user_list").addClass("none");
    CollaborationUI.bindDisableScroll();
  });
};

CollaborationUI.bindInviteButton = function () {
  $(".inv_btn").click(function () {
    $("#overlay_add_user_list").addClass("none");
  });
};

/*********************************
 *  Scroll Controls
 ********************************/
CollaborationUI.bindDisableScroll = function () {
  console.log("peacekim:: CollaborationUI.bindDisableScroll");
  document.addEventListener("mousewheel", CollaborationUI.scrollControl, {
    passive: false,
  });
  document.addEventListener("touchmove", CollaborationUI.scrollControl, {
    passive: false,
  });
};

// スクロール禁止解除
CollaborationUI.bindEnableScroll = function () {
  console.log("peacekim:: CollaborationUI.bindEnableScroll");
  document.removeEventListener("mousewheel", CollaborationUI.scrollControl, {
    passive: false,
  });
  document.removeEventListener("touchmove", CollaborationUI.scrollControl, {
    passive: false,
  });
};

CollaborationUI.scrollControl = function (event) {
  console.log("peacekim:: CollaborationUI.scrollControl");
  if (event.cancelable) {
    event.preventDefault();
  }
};

/*********************************
 *  NameCard in Collaboration
 ********************************/
CollaborationUI.makeNameCard = function (shopMemberID) {
  console.log(
    "peacekim:: CollaborationUI.makeNameCard shopMemberID: " + shopMemberID
  );

  if (currentUserInfo.shopMemberID == shopMemberID) {
    return;
  }

  const namecardTemplate = getTemplate(TemplateURL.COLLABORATION_PROFILE);

  const nameCardInfo = NativeBridgeDataSource.getNameCardData(shopMemberID);
  nameCardInfo.profileUrl = Common.getProfileImgUrl(nameCardInfo.profileUrl);

  let isCollaborationHost = coview_api.getRoomUsers();

  const whosHost = $("#collaboration_user_" + nameCardInfo.loginId).hasClass(
    "host"
  );

  let namecardHTML = Mustache.render(namecardTemplate, {
    shopMemberId: nameCardInfo.shopMemberId,
    profileUrl: nameCardInfo.profileUrl,
    loginId: nameCardInfo.loginId,
    name: nameCardInfo.shopMemberName,
    groupPathList: nameCardInfo.groupPathList,
    isFavorite: nameCardInfo.isFavorite,
    isHost: isCollaborationHost,
    whosHost: whosHost,
    collaborationId: globalUserInfo.shopName + "_" + nameCardInfo.loginId,
  });

  let namecardObj = $(jQuery.parseHTML(namecardHTML)).on(
    "click",
    function () {}
  );

  $("#userProfileModalInCollaboration").html(namecardObj);
  $("#userNameCardInCollaboration").modal("show");
};

CollaborationUI.removeFavoriteUserInCollaboration = function (shopMemberID) {
  console.log(
    "peacekim:: CollaborationUI.removeFavoriteUserInCollaboration shopMemberID: " +
      shopMemberID
  );
  Common.showLoadingIndicator();
  $("#userNameCardInCollaboration").modal("hide");
  NativeBridgeDataSource.removeFavoriteUser(shopMemberID);
  Common.dismissLoadingIndicator();
};

CollaborationUI.insertFavoriteUserInCollaboration = function (shopMemberID) {
  console.log(
    "peacekim:: CollaborationUI.insertFavoriteUserInCollaboration shopMemberID: " +
      shopMemberID
  );
  $("#userNameCardInCollaboration").modal("hide");
  NativeBridgeDataSource.addFavoriteUser(shopMemberID);
  Common.dismissLoadingIndicator();
};

CollaborationUI.refreshForOffline = function () {
  console.log("peacekim:: CollaborationUI.refreshForOffline");
  serverInfo.isOnline = false;
  if (typeof coview_api == "undefined") {
    return;
  }
  const alertString = getLocalizedString("err_weak_network_exit_collaboration");
  alert(alertString);

  NativeBridgeDelegate.finishCollaboration();

  if (globalUserInfo.joinType != COLLABORATION_JOIN_TYPE.INVITED) {
    NativeBridgeDelegate.joinRoom(roomInfo.roomID, roomInfo.name);
  } else {
    NativeBridgeDelegate.openCommunicationHome();
  }
};

CollaborationUI.showLoadingIndicator = function () {
  console.log("peacekim:: CollaborationUI.showLoadingIndicator");
  $("#loadingIndicator").addClass("full_active");
};

CollaborationUI.hideLoadingIndicator = function () {
  console.log("peacekim:: CollaborationUI.hideLoadingIndicator");
  $("#loadingIndicator").removeClass("full_active");
};

CollaborationUI.displayAddUserButtonIfNeeded = function () {
  console.log(
    "peacekim:: CollaborationUI.displayAddUserButtonIfNeeded" +
      ", roomInfo.roomType == ChatRoomType.DM: " +
      roomInfo.roomType ==
      ChatRoomType.DM
  );
  if (roomInfo.roomType == ChatRoomType.DM) {
    $(".add_user_btn").removeClass("none");
  }
};
