package com.noticeditorteam.noticeditor.model;

import com.noticeditorteam.noticeditor.io.IOUtil;
import java.io.File;
import java.nio.file.Files;

/**
 * Model representation of notice. Contains notice data or branch data
 *
 * @author naik, setser, annimon, kalter
 */
public class NoticeTreeItem extends FilterableTreeItem<NoticeItem> {

	/**
	 * Create branch node on tree.
	 *
	 * @param title a title of notice
	 */
	public NoticeTreeItem(String title) {
		this(title, null, 0);
	}

	/**
	 * Create leaf node on tree.
	 *
	 * @param title a title of notice
	 * @param content a content of notice
	 */
	public NoticeTreeItem(String title, String content) {
		this(title, content, NoticeItem.STATUS_NORMAL);
	}

	/**
	 * Create leaf node on tree.
	 *
	 * @param title a title of notice
	 * @param content a content of notice
	 * @param status a status of notice
	 */
	public NoticeTreeItem(String title, String content, int status) {
		super(new NoticeItem(title, content, status));
	}

	public void addChild(NoticeTreeItem item) {
		getInternalChildren().add(item);
		if (item != null) {
			getValue().addChild(item.getValue());
		} else {
			getValue().addChild(null);
		}
	}

	@Override
	public boolean isLeaf() {
		return getValue().isLeaf();
	}

	/**
	 * @return true if content == null
	 */
	public boolean isBranch() {
		return getValue().isBranch();
	}

	/**
	 * @return notice content or null if its a branch
	 */
	public String getContent() {
		return getValue().getContent();
	}

    /**
     * Content will be changed only when is a leaf node.
     *
     * @param content new content
     */
    public void changeContent(String content) {
        getValue().changeContent(content);
        fireChangeItem();
    }

	public String getTitle() {
		return getValue().getTitle();
	}

	public void setTitle(String title) {
		getValue().setTitle(title);
		fireChangeItem();
	}

	public int getStatus() {
		return getValue().getStatus();
	}

	public void setStatus(int status) {
		getValue().setStatus(status);
		fireChangeItem();
	}

    public void addAttachement(File file) {
        try {
            final byte[] content = Files.readAllBytes(file.toPath());
            final String name = IOUtil.sanitizeFilename(file.getName());
            final Attachment attachment = new Attachment(name, content);
            if (attachment.isImage()) {
                // TODO place attach code at the cursor place
                // and replace newline chars with space
                String newcontent = getContent() + "\n@att:" + name + "\n";
                changeContent(newcontent);
            }
            getValue().getAttachments().add(attachment);
        } catch (Exception e) {
            // TODO logger
            e.printStackTrace();
        }
    }

    public Attachments getAttachments() {
        return getValue().getAttachments();
    }

    public void setAttachments(Attachments attachments) {
        getValue().setAttachments(attachments);
        fireChangeItem();
    }
}

@@ -84,66 +84,194 @@
 tor.
-io.IOUtil;%0Aimport java.io.File;%0Aimport java.nio.file.Files
+controller.NoticeController;%0Aimport com.noticeditorteam.noticeditor.io.IOUtil;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.util.logging.Level
 ;%0A%0A/
@@ -2500,109 +2500,181 @@
     
-// TODO place attach code at the cursor place%0A                // and replace newline chars with space
+final int caret = NoticeController.getNoticeViewController()%0A                        .getEditor().getCaretPosition();%0A                final String allContent = getContent();
 %0A   
@@ -2686,16 +2686,22 @@
         
+final 
 String n
@@ -2702,17 +2702,17 @@
 ring new
-c
+C
 ontent =
@@ -2716,46 +2716,142 @@
 t = 
-get
+all
 Content
-() + %22%5Cn@att:%22 + name + %22%5Cn%22
+.substring(0, caret)%0A                        + %22%5Cn@att:%22 + name + %22%5Cn%22%0A                        + allContent.substring(caret)
 ;%0A  
@@ -2881,17 +2881,17 @@
 tent(new
-c
+C
 ontent);
@@ -3010,53 +3010,73 @@
     
-// TODO logger%0A            e.printStackTrace(
+NoticeController.getLogger().log(Level.SEVERE, %22addAttachment%22, e
 );%0A 

Put attachment code to caret position