{groovy:output=wiki|showErrorOutput=true}

import com.atlassian.confluence.pages.Page;
import bucket.container.ContainerManager;

def index = null //request.getSession(true).getServletContext().getAttribute("climb.index")

if (index  == null )
{
  out.println "Creating index..."
  
  index = new Index()

  String pageTitle="Climbing Guides"
  //String pageTitle="Hobart Craglets"
  Page page = ContainerManager.getComponent("pageManager").getPage(context.spaceKey,pageTitle);

  addChildren(page, index)

  request.getSession(true).getServletContext().setAttribute("climb.index", index)

//  for (c in index.climbs)
//    out.println c
    
}
def totalStars = ["","*","**","***"]
def totalGrades = ["10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","VE","V0","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15"]
def grades = ["17","18" ]
def stars = ["***"]
def pages = ["Northern Buttress"]

out.println '''
h2. Search Criteria:
{html}<table><tr>
<th>Stars</th>
<th>Grades</th>
<th>Areas</th>
</tr>
<tr>
<td><select multiple='true' name='climb.stars' >'''
for (a in totalStars)
{
  out.println "<option " + (stars.contains(a)?"selected":"") +  " value='" + a + "'>" + (a==""?"none":a) + "</option>"
}
out.println '''
</select>
</td>
<td><select multiple='true' size='10' name='climb.grades' >'''
for (a in totalGrades)
{
  out.println "<option " + (grades.contains(a)?"selected":"") +  " value='" + a + "'>" + a + "</option>"
}
out.println '''
</td>
<td><select multiple='true' size='10' name='climb.pages' >'''

for (a in index.pages)
{
  out.println "<option " + (pages.contains(a)?"selected":"") +  " value='" + a + "'>" + a + "</option>"
}

out.println '''
</select>
</td>
</tr>
</table>
{html}'''


def filtered = new ArrayList();
for (c in index.climbs)
{
  if ( pages.contains(c.page) && grades.contains(c.grade) && stars.contains(c.stars) )
    filtered.add c
}
out.println "h2. Results"
out.println "|| Stars || Climb || Length || Grade || Area ||"
for (c in filtered)
{
  out.println "|" + c.stars + "|" + c.name + "|" + c.length + "|" + c.grade + "| [" + c.page + "]|"
}

def addChildren(Page page, Index index)
{
  for (child in page.children)
  {
    doPage(child, index)
  }
}

def doPage (Page page, Index index)
{
  int index1 = page.content.indexOf("{guide}")
  
  if (index1 > -1)
  {
    index.pages.add page.title  
  
    int index2 = page.content.indexOf("{guide}",index1+1)
    String xml = page.content.substring(index1+7, index2)
    //out.println ("Page:" + page )

    def x = new XmlParser().parseText(xml)
    def climbs = x.climb

    for (climb in climbs)
    {
      Climb c = new Climb( name: climb.@name , 
grade: climb.@grade , 
stars: climb.@stars, 
length: climb.@length, 
page: page.title)
      c.stars = c.stars==null?"":c.stars
      c.stars = c.stars.trim()
      index.climbs.add( c )
    }

  }
  addChildren(page, index)
  

}

class Index
{
  List climbs = []
  List pages = []
}

class Climb
{
  String name
  String grade
  String stars
  String length
  //String extra
  //String number
  String page;  


  String toString()
  {
    return stars==null?"":stars + " " + "  " + length + "  " + grade 
  }
}

{groovy}