/**
 * @author Ahn Hyeok Jun
 */

package controllers;

import java.io.File;
import java.util.List;

import models.*;
import play.data.Form;
import play.mvc.*;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Http.Request;
import views.html.board.*;

public class BoardApp extends Controller {

    public static Result boardList(int pageNum, String order, String key) {

        return ok(postList.render("게시판", Post.findOnePage(pageNum, order, key), order, key));
    }

    public static Result newPost() {
        return ok(newPost.render("새 게시물", new Form<Post>(Post.class)));
    }

    public static Result savePost() {
        Form<Post> postForm = new Form<Post>(Post.class).bindFromRequest();

        if (postForm.hasErrors()) {
            return ok("입력값이 잘못되었습니다.");
        } else {
            Post post = postForm.get();
            post.userId = UserApp.userId();
            post.commentCount = 0;
            post.filePath = saveFile(request());
            Post.write(post);
        }
        return redirect(routes.BoardApp.boardList(1, Post.ORDER_DESCENDING, Post.ORDERING_KEY_ID));
    }

    public static Result post(Long postId) {
        Post post = Post.findById(postId);
        List<Comment> comments = Comment.findCommentsByPostId(postId);
        if (post == null) {
            return ok(notExsitPage.render("존재하지 않는 게시물"));
        } else {
            Form<Comment> commentForm = new Form<Comment>(Comment.class);
            return ok(views.html.board.post.render(post, comments, commentForm));
        }
    }

    public static Result saveComment(Long postId) {
        Form<Comment> commentForm = new Form<Comment>(Comment.class).bindFromRequest();

        if (commentForm.hasErrors()) {
            return TODO;

        } else {
            Comment comment = commentForm.get();
            comment.postId = postId;
            comment.userId = UserApp.userId();
            comment.filePath = saveFile(request());

            Comment.write(comment);

            return redirect(routes.BoardApp.post(postId));
        }
    }

    public static Result delete(Long postId) {
        Post.delete(postId);
        return redirect(routes.BoardApp.boardList(1, Post.ORDER_DESCENDING, Post.ORDER_DESCENDING));
    }

    public static Result editPost(Long postId) {
        Post exsitPost = Post.findById(postId);
        Form<Post> editForm = new Form<Post>(Post.class).fill(exsitPost);
        return ok(editPost.render("게시물 수정", editForm, postId));
    }

    public static Result updatePost(Long postId) {
        Form<Post> postForm = new Form<Post>(Post.class).bindFromRequest();

        if (postForm.hasErrors()) {
            return ok("입력값이 잘못되었습니다.");
        } else {

            Post post = postForm.get();
            post.userId = UserApp.userId();
            post.id = postId;
            post.filePath = saveFile(request());

            Post.edit(post);
        }
        return redirect(routes.BoardApp.boardList(1, Post.ORDER_DESCENDING, Post.ORDER_DESCENDING));
    }

    private static String saveFile(Request request) {
        MultipartFormData body = request.body().asMultipartFormData();

        FilePart filePart = body.getFile("filePath");

        if (filePart != null) {
            File saveFile = new File("public/uploadFiles/" + filePart.getFilename());
            filePart.getFile().renameTo(saveFile);
            return filePart.getFilename();
        }
        return null;
    }
}

@@ -807,39 +807,110 @@
   return ok(
-%22%EC%9E%85%EB%A0%A5%EA%B0%92%EC%9D%B4 %EC%9E%98%EB%AA%BB%EB%90%98%EC%97%88%EC%8A%B5%EB%8B%88%EB%8B%A4.%22
+boardError.render(%22%EB%B3%B8%EB%AC%B8%EA%B3%BC %EC%A0%9C%EB%AA%A9%EC%9D%80 %EB%B0%98%EB%93%9C%EC%8B%9C %EC%8D%A8%EC%95%BC%ED%95%A9%EB%8B%88%EB%8B%A4.%22,%0A                    routes.BoardApp.newPost())
 );%0A        %7D
@@ -1871,12 +1871,79 @@
 urn 
-TODO
+ok(boardError.render(%22%EB%B3%B8%EB%AC%B8%EC%9D%80 %EB%B0%98%EB%93%9C%EC%8B%9C %EC%93%B0%EC%85%94%EC%95%BC %ED%95%A9%EB%8B%88%EB%8B%A4.%22, routes.BoardApp.post(postId)))
 ;%0A%0A 
@@ -2608,24 +2608,81 @@
 exsitPost);%0A
+%0A        if (UserApp.userId() == exsitPost.userId) %7B%0A    
         retu
@@ -2729,24 +2729,137 @@
 , postId));%0A
+        %7D else %7B%0A            return ok(boardError.render(%22%EA%B8%80%EC%93%B4%EC%9D%B4%EA%B0%80 %EC%95%84%EB%8B%99%EB%8B%88%EB%8B%A4.%22, routes.BoardApp.post(postId)));%0A        %7D%0A
     %7D%0A%0A    p

add Board's ErrorPage