import { Conversation, Message } from '../models';

enum ActionKey {
  OPEN_CHATROOM_REQUEST = 'CHAT/OPEN_CHATROOM_REQUEST',
  CONVERSATION_ADDDED = 'CHAT/CONVERSATION_ADDDED',
  CONVERSATION_UPDATED = 'CHAT/CONVERSATION_UPDATED',

  FETCH_CHAT_HISTORY_REQUESTED = 'CHAT/FETCH_CHAT_HISTORY_REQUESTED',
  FETCH_CHAT_HISTORY_SUCCESS = 'CHAT/FETCH_CHAT_HISTORY_SUCCESS',
  FETCH_CHAT_HISTORY_FAIL = 'CHAT/FETCH_CHAT_HISTORY_FAIL',

  TOGGLE_MUTE_CONVERSATION_REQUESTED = 'CHAT/TOGGLE_MUTE_CONVERSATION_REQUESTED',
  TOGGLE_MUTE_CONVERSATION_SUCCESS = 'CHAT/TOGGLE_MUTE_CONVERSATION_SUCCESS',
  TOGGLE_MUTE_CONVERSATION_FAILED = 'CHAT/TOGGLE_MUTE_CONVERSATION_FAILED',

  SEND_MESSAGE_REQUESTED = 'CHAT/SEND_MESSAGE_REQUESTED',
  SEND_MESSAGE_SUCCESS = 'CHAT/SEND_MESSAGE_SUCCESS',
  SEND_MESSAGE_FAIL = 'CHAT/SEND_MESSAGE_FAIL',

  NEW_MESSAGE_RECEIVED = 'CHAT/NEW_MESSAGE_RECEIVED',
}

export type ConversationAdded = {
  type: ActionKey.CONVERSATION_ADDDED
  conversation: Conversation,
};
export function conversationAdded(conversation: Conversation): ConversationAdded {
  return {
    type: ActionKey.CONVERSATION_ADDDED,
    conversation,
  };
}

export type ConversationUpdated = {
  type: ActionKey.CONVERSATION_UPDATED
  conversation: Conversation,
};
export function conversationUpdated(conversation: Conversation): ConversationUpdated {
  return {
    type: ActionKey.CONVERSATION_UPDATED,
    conversation,
  };
}

export type ToggleMuteConversationRequested = {
  type: ActionKey.TOGGLE_MUTE_CONVERSATION_REQUESTED
  conversationId: string
  isMuted: boolean,
};
export function ToggleMuteConversationRequested(
  conversationId: string,
  isMuted: boolean,
): ToggleMuteConversationRequested {
  return {
    type: ActionKey.TOGGLE_MUTE_CONVERSATION_REQUESTED,
    conversationId,
    isMuted,
  };
}
export type ToggleMuteConversationSuccess = {
  type: ActionKey.TOGGLE_MUTE_CONVERSATION_SUCCESS
  conversationId: string,
};
export function ToggleMuteConversationSuccess(
  conversationId: string,
): ToggleMuteConversationSuccess {
  return {
    type: ActionKey.TOGGLE_MUTE_CONVERSATION_SUCCESS,
    conversationId,
  };
}
export type ToggleMuteConversationFail = {
  type: ActionKey.TOGGLE_MUTE_CONVERSATION_FAILED
  conversationId: string
  error: string,
};
export function ToggleMuteConversationFail(
  conversationId: string,
  error: string,
): ToggleMuteConversationFail {
  return {
    type: ActionKey.TOGGLE_MUTE_CONVERSATION_FAILED,
    conversationId,
    error,
  };
}

export type OpenChatRoomRequest = {
  type: ActionKey.OPEN_CHATROOM_REQUEST
  userId: string
  userName: string,
};
export function openChatRoomRequest(userId: string, userName: string): OpenChatRoomRequest {
  return {
    type: ActionKey.OPEN_CHATROOM_REQUEST,
    userId,
    userName,
  };
}

export type FetchChatHistoryRequested = {
  type: ActionKey.FETCH_CHAT_HISTORY_REQUESTED
  conversationId: string,
};
export function fetchChatHistoryRequested(conversationId: string): FetchChatHistoryRequested {
  return {
    type: ActionKey.FETCH_CHAT_HISTORY_REQUESTED,
    conversationId,
  };
}

export type FetchChatHistorySuccess = {
  type: ActionKey.FETCH_CHAT_HISTORY_SUCCESS
  conversationId: string
  messages: Array<Message>,
};
export function fetchChatHistorySuccess(
  conversationId: string,
  messages: Array<Message>,
): FetchChatHistorySuccess {
  return {
    type: ActionKey.FETCH_CHAT_HISTORY_SUCCESS,
    conversationId,
    messages,
  };
}

export type FetchChatHistoryFail = {
  type: ActionKey.FETCH_CHAT_HISTORY_FAIL
  conversationId: string
  error: string,
};
export function fetchChatHistoryFail(conversationId: string, error: string): FetchChatHistoryFail {
  return {
    type: ActionKey.FETCH_CHAT_HISTORY_FAIL,
    conversationId,
    error,
  };
}

export type SendMessageRequested = {
  type: ActionKey.SEND_MESSAGE_REQUESTED
  message: string
  conversationId: string
  recipientId: string,
};
export function sendMessageRequested(
  conversationId: string,
  recipientId: string,
  message: string,
): SendMessageRequested {
  return {
    type: ActionKey.SEND_MESSAGE_REQUESTED,
    conversationId,
    recipientId,
    message,
  };
}

export type SendMessageSuccess = {
  type: ActionKey.SEND_MESSAGE_SUCCESS
  conversationId: string
  message: Message,
};
export function sendMessageSuccess(conversationId: string, message): SendMessageSuccess {
  return {
    type: ActionKey.SEND_MESSAGE_SUCCESS,
    conversationId,
    message,
  };
}

export type SendMessageFail = {
  type: ActionKey.SEND_MESSAGE_FAIL
  conversationId: string
  error: string,
};
export function sendMessageFail(conversationId: string, error: string): SendMessageFail {
  return {
    type: ActionKey.SEND_MESSAGE_FAIL,
    conversationId,
    error,
  };
}

export type NewMessageReceived = {
  type: ActionKey.NEW_MESSAGE_RECEIVED
  conversationId: string
  message: Message,
};
export function newMessageReceived(conversationId: string, message): NewMessageReceived {
  return {
    type: ActionKey.NEW_MESSAGE_RECEIVED,
    conversationId,
    message,
  };
}

type ActionType =
  | FetchChatHistoryRequested
  | ToggleMuteConversationRequested
  | ToggleMuteConversationSuccess
  | ToggleMuteConversationFail
  | OpenChatRoomRequest
  | ConversationAdded
  | ConversationUpdated
  | FetchChatHistorySuccess
  | FetchChatHistoryFail
  | SendMessageRequested
  | SendMessageSuccess
  | SendMessageFail
  | NewMessageReceived;

export {ActionKey, ActionType};
