var FermiWebSocketMessageHandler = {};

FermiWebSocketMessageHandler.bindWebSocketMessage = function () {
  fw.socket.on("message", async function (data) {
    if (g_isMainMan) {
      FermiWebSocketMessageHandler.handleMessagesToHost(data);
    } else if (data.payload.loginId == globalUserInfo.loginId) {
      // 自分自身の場合
      FermiWebSocketMessageHandler.handleMessagesToSelf(data);
    } else {
      // global messages
      FermiWebSocketMessageHandler.handleMessagesToAll(data);
    }
  });
};

FermiWebSocketMessageHandler.handleMessagesToHost = function (data) {
  // HOSTの場合
  switch (data.type) {
    case "CAPTURE_REQUEST":
      FermiWebSocketMessageHandler.captureRequest(data);
      break;
    case "HOST_CHANGE_RESPONSE":
      FermiWebSocketMessageHandler.hostChangeResponse(data);
      break;
    case "GET_COLLABORATION_TYPE":
      FermiWebSocketMessageHandler.getCollaborationType(data);
      break;
    case "SHARE_FILE_HOST":
      if (globalUserInfo.collaborationType != COLLABORATION_TYPE.CAMERA) {
        FermiWebSocketMessageHandler.shareFileHost();
      }
      break;
    case "CHANGE_HOST_APPLY":
      FermiWebSocketMessageHandler.changeHostApplyForHost(data);
      break;
    default:
      break;
  }
};

FermiWebSocketMessageHandler.handleMessagesToSelf = function (data) {
  // 自分自身の場合
  switch (data.type) {
    case "HOST_CHANGE_REQUEST":
      FermiWebSocketMessageHandler.hostChangeRequest();
      break;
    case "PIP_END_REQUEST":
      if (globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT) {
        FermiWebSocketMessageHandler.pipEndRequest();
      }
      break;
    case "GET_COLLABORATION_TYPE_RESPONSE":
      FermiWebSocketMessageHandler.getCollaborationTypeResponse(data);
      break;
    default:
      break;
  }
};

FermiWebSocketMessageHandler.handleMessagesToAll = function (data) {
  switch (data.type) {
    case "CHANGE_COLLABORATION":
      FermiWebSocketMessageHandler.changeCollaboration(data);
      break;
    case "SHARE_FILE":
      if (globalUserInfo.collaborationType != COLLABORATION_TYPE.CAMERA) {
        FermiWebSocketMessageHandler.shareFile();
      }
      break;
    case "API_SEND_OWNER_CHANGE_COMPLETE":
      FermiWebSocketMessageHandler.apiSendOwnerChangeComplete();
      break;
    case "API_SEND_OWNER_CHANGE_CONFIRM":
      if (globalUserInfo.collaborationType == COLLABORATION_TYPE.CAMERA) {
        FermiWebSocketMessageHandler.apiSendOwnerChangeConfirm();
      }
      break;
    case "CHANGE_HOST_APPLY":
      FermiWebSocketMessageHandler.changeHostApplyForNotHost(data);
      break;
    case "HOST_REQUEST_DONE":
      FermiWebSocketMessageHandler.hostRequestDone();
      break;
    case "HOST_REQUEST_REJECT":
      FermiWebSocketMessageHandler.hostRequestReject(data);
      break;
    case "API_WELCOME":
      if (globalUserInfo.joinType != COLLABORATION_JOIN_TYPE.INVITED) {
        FermiWebSocketMessageHandler.apiWelcome();
      }
      break;
    default:
      break;
  }
};

// CHANGE_COLLABORATION
FermiWebSocketMessageHandler.changeCollaboration = function (data) {
  const newMeetingID = data.payload.newMeetingId;
  if (globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT) {
    NativeBridgeDelegate.exitMeetingRoom();
    NativeBridgeDelegate.finishPIPMode(true);

    if (deviceInfo.isAndroid()) {
      $(".pip_indicator").addClass("none");
    }

    waitMillisecond(500);
    updateCanvasSize();
  }

  FermiWebSocketMessageHandler.updateJoinChangeCollaboration(
    data.payload.collaborationType
  );

  globalUserInfo.meetingID = newMeetingID;

  CollaborationUI.updateScreen(globalUserInfo.collaborationType);

  NativeBridgeDelegate.joinChangedCollaboration(
    globalUserInfo.collaborationType,
    newMeetingID
  );

  if (globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT) {
    NativeBridgeDelegate.joinMeetingRoom(newMeetingID);
  }
};

// SHARE_FILE
FermiWebSocketMessageHandler.shareFile = function () {
  CollaborationUI.updateScreen(COLLABORATION_TYPE.CAMERA);
  globalUserInfo.collaborationType = COLLABORATION_TYPE.CAMERA;
  FermiWebSocketBridge.shareFileHost(COLLABORATION_TYPE.CAMERA);
};

// SHARE_FILE_HOST
FermiWebSocketMessageHandler.shareFileHost = function () {
  CollaborationUI.updateScreen(COLLABORATION_TYPE.CAMERA);
  globalUserInfo.collaborationType = COLLABORATION_TYPE.CAMERA;
};

// CAPTURE_REQUEST
FermiWebSocketMessageHandler.captureRequest = function (data) {
  if (
    confirm(data.payload.name + getLocalizedString("request_capture")) == false
  ) {
    return;
  }

  if ($("#recordBtn .record").hasClass("disable")) {
    screenLock();
    recordStop(function () {
      $("#screenLock").remove();
      captureAndShareImage(
        serverInfo.cmsURL + "/chatapi/file/uploadArchive",
        data.payload.name
      );
    });
  } else {
    captureAndShareImage(
      serverInfo.cmsURL + "/chatapi/file/uploadArchive",
      data.payload.name
    );
  }
};

// HOST_CHANGE_REQUEST
FermiWebSocketMessageHandler.hostChangeRequest = function () {
  FermiWebSocketBridge.hostChangeRequest();
};

// HOST_CHANGE_RESPONSE
FermiWebSocketMessageHandler.hostChangeResponse = function (data) {
  const isAndroid = data.payload.isAndroid;
  const isAble = data.payload.isAble;
  if (
    isAndroid &&
    !isAble &&
    globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT
  ) {
    coview_api.HeartBeatUser(globalUserInfo.loginId);
    // 対象ユーザの端末バージョンでは文書協業が利用できません。
    alert(getLocalizedString("err_target_android_version_not_support"));
  } else {
    FermiWebSocketBridge.pipEndRequest(data.payload.loginId);
    waitMillisecond(500);
    CoviewBridge.changeHost(getFermiLoginId(data.payload.loginId));
  }
};

// PIP_END_REQUEST
FermiWebSocketMessageHandler.pipEndRequest = function () {
  NativeBridgeDelegate.finishPIPMode(true);
  if (deviceInfo.isAndroid()) {
    $(".pip_indicator").addClass("none");
  }
};

// API_SEND_OWNER_CHANGE_COMPLETE
FermiWebSocketMessageHandler.apiSendOwnerChangeComplete = function () {
  penOff();
  if ($("#recordBtn .record").hasClass("disable")) {
    recordStop(function () {});
  }
  if ($("#micBtn .voice").hasClass("disable")) {
    micOff();
  } else {
    micOn();
  }
};

// API_SEND_OWNER_CHANGE_CONFIRM
FermiWebSocketMessageHandler.apiSendOwnerChangeConfirm = function () {
  waitMillisecond(500);
  removeOldLocalVideo(g_localStream);
};

// CHANGE_HOST_APPLY
FermiWebSocketMessageHandler.changeHostApplyForNotHost = function (data) {
  NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DOING);
  waitMillisecond(500);
};

// CHANGE_HOST_APPLY
FermiWebSocketMessageHandler.changeHostApplyForHost = function (data) {
  coview_api.HeartBeatUser(globalUserInfo.loginId);
  NativeBridgeDelegate.finishPIPMode(true);
  if (deviceInfo.isAndroid()) {
    $(".pip_indicator").addClass("none");
  }

  NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DOING);

  waitMillisecond(500);

  if (
    confirm(
      getLocalizedString("norify_request_host_change", data.payload.hostId)
    )
  ) {
    CoviewBridge.changeHost(getFermiLoginId(data.payload.hostId));
    NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DONE);
  } else {
    FermiWebSocketBridge.hostRequestReject(data.payload.hostId);
  }
  FermiWebSocketBridge.hostRequestDone(data.payload.hostId);
};

// HOST_REQUEST_DONE
FermiWebSocketMessageHandler.hostRequestDone = function () {
  NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DONE);
};

// HOST_REQUEST_REJECT
FermiWebSocketMessageHandler.hostRequestReject = function (data) {
  if (data.payload.hostId == currentUserInfo.loginID) {
    alert("REJECT for owner user");
  }
  NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DONE);
};

// GET_COLLABORATION_TYPE
FermiWebSocketMessageHandler.getCollaborationType = function (data) {
  FermiWebSocketBridge.getCollaborationTypeResponse(data.payload.loginId);
};

// GET_COLLABORATION_TYPE_RESPONSE
FermiWebSocketMessageHandler.getCollaborationTypeResponse = function (data) {
  FermiWebSocketMessageHandler.updateJoinChangeCollaboration(
    data.payload.collaborationType
  );
  globalUserInfo.meetingID = data.payload.newMeetingId;
  CollaborationUI.updateScreen(globalUserInfo.collaborationType);

  NativeBridgeDelegate.joinChangedCollaboration(
    globalUserInfo.collaborationType,
    data.payload.newMeetingId
  );

  if (globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT) {
    NativeBridgeDelegate.joinMeetingRoom(data.payload.newMeetingId);
  }
};

// API_WELCOME
FermiWebSocketMessageHandler.apiWelcome = function () {
  FermiWebSocketBridge.getCollaborationType();
  globalUserInfo.joinType = COLLABORATION_JOIN_TYPE.ATTENDEE;
};

FermiWebSocketMessageHandler.updateJoinChangeCollaboration = function (
  collaborationType
) {
  if (
    deviceInfo.isAndroid() &&
    collaborationType == COLLABORATION_TYPE.DOCUMENT &&
    deviceInfo.androidVersion < ANDROID_SDK_VERSION.O
  ) {
    coview_api.HeartBeatUser(globalUserInfo.loginId);
    alert(getLocalizedString("not_support_version"));
    globalUserInfo.collaborationType = COLLABORATION_TYPE.CAMERA;
  } else {
    globalUserInfo.collaborationType = collaborationType;
  }
};
