/*
 * Copyright (C) 2010 BloatIt. This file is part of BloatIt. BloatIt is free software: you
 * can redistribute it and/or modify it under the terms of the GNU Affero General Public
 * License as published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version. BloatIt is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details. You should have received a copy of the GNU Affero General
 * Public License along with BloatIt. If not, see <http://www.gnu.org/licenses/>.
 */
package com.bloatit.web.html.pages;

import com.bloatit.common.PageIterable;
import com.bloatit.framework.Demand;
import com.bloatit.framework.managers.DemandManager;
import com.bloatit.web.annotations.ParamContainer;
import com.bloatit.web.annotations.RequestParam;
import com.bloatit.web.html.HtmlNode;
import com.bloatit.web.html.components.custom.HtmlPagedList;
import com.bloatit.web.html.components.standard.HtmlDiv;
import com.bloatit.web.html.components.standard.HtmlListItem;
import com.bloatit.web.html.components.standard.HtmlRenderer;
import com.bloatit.web.html.components.standard.HtmlTitleBlock;
import com.bloatit.web.html.components.standard.form.HtmlForm;
import com.bloatit.web.html.components.standard.form.HtmlSubmit;
import com.bloatit.web.html.components.standard.form.HtmlTextField;
import com.bloatit.web.html.pages.master.Page;
import com.bloatit.web.server.Context;
import com.bloatit.web.utils.url.GlobalSearchPageUrl;
import com.bloatit.web.utils.url.IdeaPageUrl;

@ParamContainer("search")
public class GlobalSearchPage extends Page {

    public final static String SEARCH_CODE = "global_search";
    @RequestParam(defaultValue = "", name = SEARCH_CODE)
    private final String searchString;

    private HtmlPagedList<Demand> pagedMemberList;
    private final GlobalSearchPageUrl url;

    public GlobalSearchPage(final GlobalSearchPageUrl url) {
        super(url);
        this.url = url;
        this.searchString = url.getSearchString();

        generateContent();
    }

    private void generateContent() {

        final HtmlTitleBlock pageTitle = new HtmlTitleBlock(Context.tr("Search result"), 2);

        pageTitle.add(generateSearchBlock());

        final PageIterable<Demand> demandList = DemandManager.search(searchString);

        final HtmlRenderer<Demand> demandItemRenderer = new HtmlRenderer<Demand>() {

            @Override
            public HtmlNode generate(final Demand demand) {
                return new HtmlListItem(new IdeaPageUrl(demand).getHtmlLink(demand.getTitle()));
            }
        };

        final GlobalSearchPageUrl clonedUrl = url.clone();
        pagedMemberList = new HtmlPagedList<Demand>(demandItemRenderer, demandList, clonedUrl, clonedUrl.getPagedMemberListUrl());

        pageTitle.add(pagedMemberList);
        add(pageTitle);
    }

    @Override
    public String getTitle() {
        return "View all demands - search demands";
    }

    @Override
    public boolean isStable() {
        return true;
    }

    String getSearchCode() {
        return "search";
    }

    private HtmlDiv generateSearchBlock() {
        final HtmlDiv searchBlock = new HtmlDiv("global_search_block");

        final HtmlForm searchForm = new HtmlForm(new GlobalSearchPageUrl().urlString(), HtmlForm.Method.GET);

        final HtmlTextField searchField = new HtmlTextField(GlobalSearchPage.SEARCH_CODE);

        final HtmlSubmit searchButton = new HtmlSubmit(Context.tr("Search"));
        searchForm.add(searchField);
        searchForm.add(searchButton);
        searchBlock.add(searchForm);

        return searchBlock;
    }
}

@@ -1001,16 +1001,78 @@
 mlNode;%0A
+import com.bloatit.web.html.components.custom.HtmlIdeaSumary;%0A
 import c
@@ -1124,16 +1124,16 @@
 edList;%0A
-
 import c
@@ -1708,54 +1708,8 @@
 Url;
-%0Aimport com.bloatit.web.utils.url.IdeaPageUrl;
 %0A%0A@P
@@ -2645,22 +2645,20 @@
  Demand 
+i
 de
-mand
+a
 ) %7B%0A    
@@ -2701,57 +2701,27 @@
 new 
-IdeaPageUrl(demand).getHtmlLink(demand.getTitle()
+HtmlIdeaSumary(idea
 ));%0A
@@ -3362,16 +3362,64 @@
 block%22);
+%0A        searchBlock.setCssClass(%22padding_box%22);
 %0A%0A      
@@ -3519,24 +3519,24 @@
 thod.GET);%0A%0A
-
         fina
@@ -3613,16 +3613,119 @@
 H_CODE);
+%0A        searchField.setLabel(Context.tr(%22Search%22));%0A        searchField.setDefaultValue(searchString);
 %0A%0A      

Improve search result presentation