import {
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  ProtocolNotification,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { SenderInfo } from '../models/sender-info';

export interface UmgNotiNotification extends ProtocolNotification {
  /** 송신자SEQ(n) */
  senderSeq: number;
  /** { 송신자정보 } */
  senderInfo: SenderInfo;
  /** 푸시타입(s) */
  pushType: string;
  /** keyID(s) */
  keyId: string;
  /** Tiltle(s) */
  title: string;
  /** 전달Text(s) */
  text: string;
}

export const decodeUmgNotiNotification: ProtocolDecoder<UmgNotiNotification> = (
  message: ProtocolMessage
) => {
  let senderInfo: SenderInfo;
  if (message.bodyList.length > 1) {
    const info = message.bodyList[1].split(BodyStringDivider);
    senderInfo = {
      seq: Number(info[0]),
      name: info[1],
      profileImageFile: info[2],
      grade: info[3]
    };
  }
  return decodeProtocolMessage(message, {
    /** 송신자SEQ(n) */
    senderSeq: Number(message.bodyList[0]),
    /** { 송신자정보 } */
    senderInfo,
    /** 푸시타입(s) */
    pushType: message.bodyList[2],
    /** keyID(s) */
    keyId: message.bodyList[3],
    /** Tiltle(s) */
    title: message.bodyList[4],
    /** 전달Text(s) */
    text: message.bodyList[5]
  } as UmgNotiNotification);
};
