package page.misc;

import java.util.HashMap;
import java.util.Map;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.handler.http.FormData;
import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.HTTPHandler;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import org.wikiwebserver.html.HTMLHelper;
import org.wikiwebserver.html.HTMLHelper.ContainerType;

import page.config.SiteTemplatedPage;

import page.tools.entity.Comment;
import page.tools.entity.User;
import static org.wikiwebserver.html.HTMLHelper.*;

public class CommentView extends SiteTemplatedPage implements HTTPResponder {
    
    public void generate() throws HTTPException {
        process(getHandler());
        
        if (getFormData() != null) {
            String commentID = getFormData().getFirst("commentID");
            
            if (commentID != null) {
                Comment comment = Comment.getCommentById(commentID);
                append(editComment(comment, this));                
            }    
        }
        
    }
  
    public Comment process(HTTPHandler conn) throws HTTPException {
        
        Comment comment = null;
        FormData formData = conn.getRequest().getFormData();
        if (formData != null) {
            String action = formData.getFirst("action");
            if (action != null) {
                String commentID = formData.getFirst("commentID");
                
                if (commentID != null) {
                    comment = Comment.getCommentById(commentID);
                }

                if (comment != null) {
                    if (action.equalsIgnoreCase("Update")) {
                        String text = formData.getFirst("comments");
                        comment.setText(WareHouse.unescapeHTMLEntities(text), conn);                      
                    }                 
                    else if (action.equalsIgnoreCase("Delete")) {
                        comment.clear(conn);                     
                    } 
                }
            }
        }
        
        return comment;
    }   
    
    public String editComment(Comment comment, SiteTemplatedPage page) {
        return editComment(comment, page, WareHouse.getUrlPathForClass(getClass().getName()));
    }    

    public String editComment(Comment comment, SiteTemplatedPage page, String base) {
        
        User user = page.getUser();
        if (comment == null && user == null) {
            return p("You must sign in before you can post comments");
        }
        
        String text = "";
        String hidden = "";
        String posted = "";
        String submit = submitbutton("action", "Post Comment");
        String editActions = "";
        if (comment != null) {
            hidden = hiddenfield("commentID", comment.getId());
            
            text = comment.getText();
            submit = submitbutton("action", "Update");
            editActions = submitbutton("action", "Delete") 
                        + submitbutton("action", "Cancel");
            posted = "Posted: " + WareHouse.formatStandardDate(comment.getCreatedTime());
        }
        
        String formHTML = p(SiteTemplatedPage.emailImage(user, 16)) + hidden +
                          textarea("comments", text, "style='width: 100%; height: 80px;'") + 
                          p(posted) + submit + editActions; 
        
        return HTMLHelper.form(formHTML, base);   
    }
    
    public String showComment(Comment comment, SiteTemplatedPage page) {
        return showComment(comment, page, WareHouse.getUrlPathForClass(getClass().getName()));
    }
    
    public String showComment(Comment comment, SiteTemplatedPage page, String base) {
        
        if (comment == null) return null;
        
        String modLinks = "";        
        if (page != null && comment.canModify(page.getHandler())) {
            String ref = "commentID=" + comment.getId();
            if (!base.contains("?")) base += "?";
            else base += "&";
            String edit = base + "action=edit&" + ref;      
            modLinks = "[ " + a(edit, "Modify") + " ]";        
        }
        
        String datePosted = WareHouse.formatStandardDate(comment.getCreatedTime());
        
        return div(ContainerType.CLASS, "commentBox", 
                   SiteTemplatedPage.avatarImage(comment.getAuthor(), "align='right'") + 
                   p(SiteTemplatedPage.emailImage(comment.getAuthor())) +
                   p(stripHTML(comment.getText())) +                    
                   p(datePosted + " " + modLinks));
    }    
    
    private String stripHTML(String unsafe) {
        Map<String, String> escapeMap = new HashMap<String, String>();
        escapeMap.put("<", "&lt;"); 
        escapeMap.put(">", "&gt;");
        
        return WareHouse.performFindReplace(unsafe, escapeMap);           
    }
}
