<?php

/**
 * The note controller: Just an example of simple create, read, update and delete (CRUD) actions.
 */
require_once __DIR__ . '/../libs/SimpleMarkdown.php';

class NoteController extends Controller
{
    /**
     * Construct this object by extending the basic Controller class
     */
    public function __construct()
    {
        parent::__construct();

        // VERY IMPORTANT: All controllers/areas that should only be usable by logged-in users
        // need this line! Otherwise not-logged in users could do actions. If all of your pages should only
        // be usable by logged-in users: Put this line into libs/Controller->__construct
        Auth::checkAuthentication();
    }

    /**
     * This method controls what happens when you move to /note/index in your app.
     * Gets all notes (of the user).
     */
    public function index()
    {
        $this->View->render('note/index', array(
            'notes' => NoteModel::getAllNotes()
        ));
    }

    /**
     * Get note as JSON (AJAX endpoint)
     */
    public function getNote($note_id)
    {
        $note = NoteModel::getNote($note_id);
        
        header('Content-Type: application/json');
        if ($note) {
            // Add markdown version
            $note->note_html = SimpleMarkdown::parse($note->note_text);
            echo json_encode(['success' => true, 'note' => $note]);
        } else {
            echo json_encode(['success' => false, 'message' => 'Note not found']);
        }
    }
    
    /**
     * This method controls what happens when you move to /dashboard/create in your app.
     * Creates a new note. This is usually the target of form submit actions.
     * POST request.
     */
    public function create()
    {
        $success = NoteModel::createNote(Request::post('note_text'));
        
        if ($this->isAjaxRequest()) {
            header('Content-Type: application/json');
            if ($success) {
                echo json_encode(['success' => true, 'message' => 'Note created successfully']);
            } else {
                echo json_encode(['success' => false, 'message' => 'Failed to create note']);
            }
            return;
        }
        
        Redirect::to('note');
    }

    /**
     * This method controls what happens when you move to /note/edit(/XX) in your app.
     * Shows the current content of the note and an editing form.
     * @param $note_id int id of the note
     */
    public function edit($note_id)
    {
        $this->View->render('note/edit', array(
            'note' => NoteModel::getNote($note_id)
        ));
    }

    /**
     * This method controls what happens when you move to /note/editSave in your app.
     * Edits a note (performs the editing after form submit).
     * POST request.
     */
    public function editSave()
    {
        $success = NoteModel::updateNote(Request::post('note_id'), Request::post('note_text'));
        
        if ($this->isAjaxRequest()) {
            header('Content-Type: application/json');
            if ($success) {
                echo json_encode(['success' => true, 'message' => 'Note updated successfully']);
            } else {
                echo json_encode(['success' => false, 'message' => 'Failed to update note']);
            }
            return;
        }
        
        Redirect::to('note');
    }

    /**
     * This method controls what happens when you move to /note/delete(/XX) in your app.
     * Deletes a note. In a real application a deletion via GET/URL is not recommended, but for demo purposes it's
     * totally okay.
     * @param int $note_id id of the note
     */
    public function delete($note_id)
    {
        $success = NoteModel::deleteNote($note_id);
        
        if ($this->isAjaxRequest()) {
            header('Content-Type: application/json');
            if ($success) {
                echo json_encode(['success' => true, 'message' => 'Note deleted successfully']);
            } else {
                echo json_encode(['success' => false, 'message' => 'Failed to delete note']);
            }
            return;
        }
        
        Redirect::to('note');
    }
    
    /**
     * Check if the request is an AJAX request
     */
    private function isAjaxRequest()
    {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
               strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
    }
}
