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

export interface UserPasswordSetRequest extends ProtocolRequest {
  // LoginID(s)
  loginId: string;
  // LoginPW
  oldLoginPw: string;
  // NewPW
  newLoginPw: string;
  // 기관코드(s)
  companyCode: string;
}

export interface UserPasswordSetResponse extends ProtocolResponse {
  // LoginID(s)
  loginId: string;
  // LoginPW
  oldLoginPw: string;
  // NewPW
  newLoginPw: string;
  // 기관코드(s)
  companyCode: string;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.loginId });
  bodyList.push({ type: PacketBodyValue.String, value: req.oldLoginPw });
  bodyList.push({ type: PacketBodyValue.String, value: req.newLoginPw });
  bodyList.push({ type: PacketBodyValue.String, value: req.companyCode });
  return bodyList;
};

export const decodeUserPasswordSet: ProtocolDecoder<UserPasswordSetResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    loginId: message.bodyList[0],
    oldLoginPw: message.bodyList[1],
    newLoginPw: message.bodyList[2],
    companyCode: message.bodyList[3]
  } as UserPasswordSetResponse);
};
