#include "TextEditorController.h"
#include "File.h"

TextEditorController::TextEditorController()
    : mModel(TextEditorModel::Create()),
      mSavePath(),
      mLoadPath()
{

}

std::unique_ptr<TextEditorController> TextEditorController::Create()
{
    return std::make_unique<TextEditorController>();
}

void TextEditorController::SetContent(const std::string& content)
{
    mModel->GetDocument()->SetContent(content);
}

std::string TextEditorController::GetContent() const
{
    return mModel->GetDocument()->GetContent();
}

void TextEditorController::OnSave()
{
    if(mSavePath.empty()) return;
    File outfile(mSavePath);

    outfile.open(File::AccessMode::Write);
    outfile.writeText(mModel->GetDocument()->GetContent());
}

void TextEditorController::OnLoad()
{
    if(mLoadPath.empty()) return;
    File infile(mLoadPath);

    infile.open(File::AccessMode::Read);
    mModel->GetDocument()->SetContent(infile.readText());
}

void TextEditorController::SetSavePath(const std::filesystem::path& path)
{
    mSavePath = path;
}

void TextEditorController::SetLoadPath(const std::filesystem::path& path)
{
    mLoadPath = path;
}

void TextEditorController::OnClear()
{
    mModel->GetDocument()->Clear();
}
