#include "TextEditorWrapper.h"
#include "TextEditor.h"
#include "WrapperConverter.h"


IggTextEditorLanguageDefinition IggNewLanguageDef()
{
  TextEditor::LanguageDefinition *ld = new TextEditor::LanguageDefinition();
  return static_cast<IggTextEditorLanguageDefinition>(ld);
}

void IggTextEditorLDSetName(IggTextEditorLanguageDefinition handle, const char *name)
{
  TextEditor::LanguageDefinition *ld = reinterpret_cast<TextEditor::LanguageDefinition*>(handle);
  ld->mName = name;
}

void IggTextEditorLDSetKeywords(IggTextEditorLanguageDefinition handle, const char **keywords, int length)
{
  TextEditor::LanguageDefinition *ld = reinterpret_cast<TextEditor::LanguageDefinition*>(handle);
  int i = 0;
  for (i = 0; i < length; i++)
    ld->mKeywords.insert(keywords[i]);
}

IggTextEditor IggNewTextEditor()
{
  TextEditor *editor = new TextEditor();
  return static_cast<IggTextEditor>(editor);
}

void IggTextEditorRender(IggTextEditor handle, const char* aTitle, IggVec2 const *size, int aBorder)
{
  Vec2Wrapper sizeArg(size);
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->Render(aTitle, *sizeArg, aBorder != 0);
}

void IggTextEditorSetShowWhitespaces(IggTextEditor handle, int aValue)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetShowWhitespaces(aValue != 0);
}

void IggTextEditorSetTabSize(IggTextEditor handle, int size)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetTabSize(size);
}

void IggTextEditorSetText(IggTextEditor handle, const char* text)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetText(text);
}

void IggTextEditorInsertText(IggTextEditor handle, const char *text)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->InsertText(text);
}

IggBool IggTextEditorHasSelection(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  return editor->HasSelection() ? 1 : 0; 
}

char const *IggTextEditorGetText(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  std::string str = editor->GetText();
  char* c = strcpy(new char[str.length() + 1], str.c_str());
  return c;
}


char const *IggTextEditorGetWordUnderCursor(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  std::string str = editor->GetWordUnderCursor();
  char* c = strcpy(new char[str.length() + 1], str.c_str());
  return c;
}

char const *IggTextEditorGetSelectedText(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  std::string str = editor->GetSelectedText();
  char* c = strcpy(new char[str.length() + 1], str.c_str());
  return c;
}

char const *IggTextEditorGetCurrentLineText(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  std::string str = editor->GetCurrentLineText();
  char* c = strcpy(new char[str.length() + 1], str.c_str());
  return c;
}

IggBool IggTextEditorIsTextChanged(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  return editor->IsTextChanged() ? 1 : 0;
}

void IggTextEditorGetScreenCursorPos(IggTextEditor handle, int *x, int* y)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::Coordinates col = editor->GetScreenCursorPosition();
  *x = (float)col.mColumn;
  *y = (float)col.mLine;
}

void IggTextEditorGetCursorPos(IggTextEditor handle, int* column, int* line)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::Coordinates col = editor->GetCursorPosition();
  *column = (float)col.mColumn;
  *line = (float)col.mLine;
}

void IggTextEditorSetCursorPos(IggTextEditor handle, int column, int line)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::Coordinates col(line, column);
  editor->SetCursorPosition(col);
}

void IggTextEditorGetSelectionStart(IggTextEditor handle, int* column, int* line)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::Coordinates col = editor->GetSelectionStart();
  *column = (float)col.mColumn;
  *line = (float)col.mLine;
}

void IggTextEditorSetLanguageDefinition(IggTextEditor handle, IggTextEditorLanguageDefinition defHandle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::LanguageDefinition *def = static_cast<TextEditor::LanguageDefinition*>(defHandle);
  editor->SetLanguageDefinition(*def);
}

void IggTextEditorSetLanguageDefinitionSQL(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetLanguageDefinition(TextEditor::LanguageDefinition::SQL());
}


void IggTextEditorSetLanguageDefinitionCPP(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetLanguageDefinition(TextEditor::LanguageDefinition::CPlusPlus());
}

void IggTextEditorSetLanguageDefinitionC(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetLanguageDefinition(TextEditor::LanguageDefinition::C());
}

void IggTextEditorSetLanguageDefinitionLua(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetLanguageDefinition(TextEditor::LanguageDefinition::Lua());
}

IggTextEditorErrorMarkers IggTextEditorNewErrorMarkers() 
{
  TextEditor::ErrorMarkers *markers = new TextEditor::ErrorMarkers();
  return static_cast<IggTextEditorErrorMarkers>(markers);
}

void IggTextEditorErrorMarkersInsert(IggTextEditorErrorMarkers handle, int pos, const char* errMsg)
{
  TextEditor::ErrorMarkers *markers = reinterpret_cast<TextEditor::ErrorMarkers*>(handle);
  markers->insert(std::pair<int, std::string>(pos, errMsg));
}

void IggTextEditorErrorMarkersClear(IggTextEditorErrorMarkers marker)
{
  TextEditor::ErrorMarkers *markers = reinterpret_cast<TextEditor::ErrorMarkers*>(marker);
  markers->clear();
}


unsigned int IggTextEditorErrorMarkersSize(IggTextEditorErrorMarkers handle)
{
  TextEditor::ErrorMarkers *markers = reinterpret_cast<TextEditor::ErrorMarkers*>(handle);
  return markers->size();
}

void IggTextEditorSetErrorMarkers(IggTextEditor handle, IggTextEditorErrorMarkers marker)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  TextEditor::ErrorMarkers *markers = reinterpret_cast<TextEditor::ErrorMarkers*>(marker);

  editor->SetErrorMarkers(*markers);
}

void IggTextEditorCopy(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->Copy();
}

void IggTextEditorCut(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->Cut();
}

void IggTextEditorPaste(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->Paste();
}

void IggTextEditorDelete(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->Delete();
}


void IggTextEditorSelectWordUnderCursor(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SelectWordUnderCursor();
}

void IggTextEditorSelectAll(IggTextEditor handle)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SelectAll();
}

void IggTextEditorSetHandleKeyboardInputs(IggTextEditor handle, int aValue)
{
  TextEditor *editor = reinterpret_cast<TextEditor*>(handle);
  editor->SetHandleKeyboardInputs(aValue);
}
