import { NotificationMethod, DeviceType } from '@ucap-webmessenger/core';

import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  ProtocolDecoder,
  ProtocolMessage,
  PacketBodyValue,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';

export interface RegUpdateRequest extends ProtocolRequest {
  // 0 알림방법(n)
  notificationMethod: NotificationMethod;
  // 1 모바일알림(n)
  mobileNotification: boolean;
  // 2 폰트종류(s)
  fontFamily: string;
  // 3 폰트크기(n)
  fontSize: number;
  // 4 알림받음/받지않음(n)
  receiveNotification: boolean;
  // 5 알림방법(n)
  notificationMethod0: number;
  // 6 알림창 설정(n)
  notificationExposureTime: number;
  // 7 타임존(s)
  timeZone: string;
  // 8 타임존(s)
  timeZoneValue: string;
  // 9 부재중시간설정(n)
  absenceTime: number;
  // 10 메뉴 언어(n)
  menuLanguage: string;
  // 11 인사정보 언어(n)
  hrInformationLanguage: string;
  deviceType: DeviceType;
}

export interface RegUpdateResponse extends ProtocolResponse {
  // 0 알림방법(n)
  notificationMethod?: NotificationMethod;
  // 1 모바일알림(n)
  mobileNotification?: boolean;
  // 2 폰트종류(s)
  fontFamily?: string;
  // 3 폰트크기(n)
  fontSize?: number;
  // 4 알림받음/받지않음(n)
  receiveNotification?: boolean;
  // 5 알림방법(n)
  notificationMethod0?: number;
  // 6 알림창 설정(n)
  notificationExposureTime?: number;
  // 7 타임존(s)
  timeZone?: string;
  // 8 타임존값(s)
  timeZoneValue?: string;
  // 9 부재중시간설정(n)
  absenceTime?: number;
  // 10 메뉴 언어(n)
  menuLanguage?: string;
  // 11 인사정보 언어(n)
  hrInformationLanguage?: string;
  // 12 단말타입(s)
  deviceType?: DeviceType;
  // 13 번역키
  translationKey?: string;
}

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

  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.notificationMethod
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.mobileNotification ? 1 : 0
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.fontFamily
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.fontSize
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.receiveNotification ? 1 : 0
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.notificationMethod0
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.notificationExposureTime
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.timeZone
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.timeZoneValue
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.absenceTime
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.menuLanguage
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.hrInformationLanguage
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.deviceType
  });

  return bodyList;
};

export const decodeRegUpdate: ProtocolDecoder<RegUpdateResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    notificationMethod: message.bodyList[0] as NotificationMethod,
    mobileNotification: message.bodyList[1] === 1 ? true : false,
    fontFamily: message.bodyList[2],
    fontSize: message.bodyList[3],
    receiveNotification: message.bodyList[4] === 1 ? true : false,
    notificationMethod0: message.bodyList[5],
    notificationExposureTime: message.bodyList[6],
    timeZone: message.bodyList[7],
    timeZoneValue: message.bodyList[8],
    absenceTime: message.bodyList[9],
    menuLanguage: message.bodyList[10],
    hrInformationLanguage: message.bodyList[11],
    deviceType: message.bodyList[12],
    translationKey: message.bodyList[13]
  } as RegUpdateResponse);
};
