import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  ProtocolDecoder,
  ProtocolMessage,
  PacketBodyValue,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import {
  DeviceType,
  FileTransferPermissions,
  OrganizationChartPermissions,
  DefaultScreen,
  VideoConferenceType
} from '@ucap-webmessenger/core';

export interface AuthRequest extends ProtocolRequest {
  deviceType: DeviceType;
}

export interface AuthResponse extends ProtocolResponse {
  // 0 단말타입(s)
  deviceType?: DeviceType;
  // 1 파일전송허용회사정보(s)
  fileTransferAllowedCompanyList?: string[];
  // 2 파일전송권한(s)
  fileTransferPermissions?: FileTransferPermissions;
  // 3 파일보관기간(n)
  fileRetentionPeriod?: number;
  // 4 비밀번호유효기간(n)
  passwordValidityPeriod?: number;
  // 5 화상회의권한(s)
  canVideoConference?: boolean;
  // 6 조직도권한(s)
  organizationChartPermissions?: OrganizationChartPermissions;
  // 7 외부전화권한(s)
  canExternalCall?: boolean;
  // 8 내/외부망 체크(s)
  checkNetwork?: boolean;
  // 9 아이폰사용여부(s)
  useIPhone?: boolean;
  // 10 메인화면(s)
  defaultScreen?: DefaultScreen;
  // 11 모바일 화면캡처사용여부(s)
  canCaptureMobileScreen?: boolean;
  // 12 마이톡권한(s)
  canMyTalk?: boolean;
  // 13 타이머챗권한(s)
  canTimerChat?: boolean;
  // 14 메일보내기권한(s)
  canSendEmail?: boolean;
  // 15 MDM권한(s)
  canMdm?: boolean;
  // 16 쿠폰선물권한(s)
  canCouponGift?: boolean;
  // 17 GAMS 사용여부(s)
  useGams?: boolean;
  // 18 PC 화면캡춰 사용여부(s)
  useCapturePcScreen?: boolean;
  // 19 화상회의타입(s)
  videoConferenceType?: VideoConferenceType;
  // 20 번역 권한(s)
  canTranslation?: boolean;
  // 21 스케쥴 보기 권한(s)
  scheduleViewAllowedCompanyList?: string[];
  // 22 챗봇 권한(s)
  canChatBot?: boolean;
  // 23 스트리밍 전송 허용회사정보(s)
  streamingTransmissionAllowedCompanyList?: string[];
  // 24 스트리밍 전송 권한(s)
  canStreamingTransmission?: boolean;
  // 25 I-Messenger 메시지함 권한
  canIMessengerMessageBox?: boolean;
}

export const encodeAuth: ProtocolEncoder<AuthRequest> = (req: AuthRequest) => {
  const bodyList: PacketBody[] = [];

  bodyList.push({
    type: PacketBodyValue.String,
    value: req.deviceType
  });

  return bodyList;
};

export const decodeAuth: ProtocolDecoder<AuthResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    deviceType: message.bodyList[0] as DeviceType,
    fileTransferAllowedCompanyList: (message.bodyList[1] as string).split(';'),
    fileTransferPermissions: message.bodyList[2] as FileTransferPermissions,
    fileRetentionPeriod: message.bodyList[3],
    passwordValidityPeriod: message.bodyList[4],
    canVideoConference: message.bodyList[5] === 'Y' ? true : false,
    organizationChartPermissions: message
      .bodyList[6] as OrganizationChartPermissions,
    canExternalCall: message.bodyList[7] === 'Y' ? true : false,
    checkNetwork: message.bodyList[8] === 'Y' ? true : false,
    useIPhone: message.bodyList[9] === 'Y' ? true : false,
    defaultScreen: message.bodyList[10] as DefaultScreen,
    canCaptureMobileScreen: message.bodyList[11] === 'Y' ? true : false,
    canMyTalk: message.bodyList[12] === 'Y' ? true : false,
    canTimerChat: message.bodyList[13] === 'Y' ? true : false,
    canSendEmail: message.bodyList[14] === 'Y' ? true : false,
    canMdm: message.bodyList[15] === 'Y' ? true : false,
    canCouponGift: message.bodyList[16] === 'Y' ? true : false,
    useGams: message.bodyList[17] === 'Y' ? true : false,
    useCapturePcScreen: message.bodyList[18] === 'Y' ? true : false,
    videoConferenceType: message.bodyList[19] as VideoConferenceType,
    canTranslation: message.bodyList[20] === 'Y' ? true : false,
    scheduleViewAllowedCompanyList: (message.bodyList[21] as string).split(';'),
    canChatBot: message.bodyList[22] === 'Y' ? true : false,
    streamingTransmissionAllowedCompanyList: (message
      .bodyList[23] as string).split(';'),
    canStreamingTransmission: message.bodyList[24] === 'Y' ? true : false,
    canIMessengerMessageBox: message.bodyList[25] === 'Y' ? true : false
  } as AuthResponse);
};
