#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.SetAccessMode(File::AccessMode::Write);
    outfile.Open();
    outfile.WriteText(mModel->GetDocument()->GetContent());
    outfile.Close();
}

void TextEditorController::OnLoad()
{
    if(mLoadPath.empty()) return;
    File infile(mLoadPath);
    infile.SetAccessMode(File::AccessMode::Read);
    infile.Open();
    mModel->GetDocument()->SetContent(infile.ReadText());
    infile.Close();
}

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();
}
