/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "talk.h"

void _vnConversationTalkStart(queue_t *queue, queueaction_t *action,uint8_t i) {
  vnconversationitemdata_t *data;
  data = (vnconversationitemdata_t *)action->data;

  vnTextBoxSetText(&data->conversation->textbox, data->text);

  if(data->character != NULL) {
    data->character->emotion = data->emotion;
    data->character->talking = true;
  }
}

void _vnConversationTalkUpdate(queue_t *queue,queueaction_t *action,uint8_t i) {
  vnconversationitemdata_t *data;
  data = (vnconversationitemdata_t *)action->data;

  
  if(data->conversation->textbox.state & VN_TEXTBOX_STATE_CLOSED) {
    if(data->character != NULL) data->character->talking = false;
    queueNext(queue);
  }
}

queueaction_t * vnConversationTalk(
  vnconversation_t *conversation,
  char *text,
  vncharacter_t *character,
  uint8_t emotion
) {
  queueaction_t *action;
  vnconversationitemdata_t *data;

  action = vnConversationAdd(conversation);
  action->onStart = &_vnConversationTalkStart;
  action->onUpdate = &_vnConversationTalkUpdate;

  data = (vnconversationitemdata_t *)action->data;
  data->text = text;
  data->character = character;
  data->emotion = emotion;

  return action;
}